Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 2 | package KorAP::VirtualCorpus::Group; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | # Construct a new VC group |
| 7 | sub new { |
| 8 | my $class = shift; |
| 9 | bless { |
| 10 | op => shift, |
| 11 | fields => {} |
| 12 | }, $class; |
| 13 | }; |
| 14 | |
| 15 | |
| 16 | # Add field information to group |
| 17 | sub add_field { |
| 18 | my $self = shift; |
| 19 | my $field = shift; |
| 20 | push @{$self->{fields}->{$field}}, shift; |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | # Stringify |
| 25 | sub to_string { |
| 26 | my $self = shift; |
| 27 | ## Create collection object |
| 28 | my $json = '{'; |
| 29 | $json .= '"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",'; |
| 30 | $json .= '"collection":{'; |
| 31 | |
| 32 | unless (keys %{$self->{fields}}) { |
| 33 | return $json . '}}'; |
| 34 | }; |
| 35 | |
| 36 | $json .= '"@type":"koral:docGroup",'; |
| 37 | $json .= '"operation":"operation:' . $self->{op} . '",'; |
| 38 | $json .= '"operands":['; |
| 39 | |
| 40 | foreach my $field (sort keys %{$self->{fields}}) { |
| 41 | unless (@{$self->{fields}->{$field}}) { |
| 42 | next; |
| 43 | }; |
| 44 | $json .= '{'; |
| 45 | $json .= '"@type":"koral:doc",'; |
| 46 | $json .= '"key":"' . $field . '",'; |
| 47 | $json .= '"match":"match:eq",'; |
| 48 | $json .= '"value":['; |
| 49 | $json .= join ',', map { '"' . $_ . '"' } @{$self->{fields}->{$field}}; |
| 50 | $json .= ']'; |
| 51 | $json .= '},'; |
| 52 | }; |
| 53 | |
| 54 | # Remove the last comma |
| 55 | chop $json; |
| 56 | |
| 57 | $json .= ']}}'; |
| 58 | return $json; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | package main; |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 63 | use strict; |
| 64 | use warnings; |
| 65 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 66 | # 2020-05-20 |
| 67 | # Preliminary support for C2 def-files. |
| 68 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 69 | our @ARGV; |
| 70 | |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 71 | unless (@ARGV) { |
| 72 | print <<'HELP'; |
| 73 | Convert a line-separated list of corpus sigles, doc sigles or |
| 74 | text sigles into a virtual corpus query. |
| 75 | |
| 76 | $ perl list2vc.pl my_vc.txt | gzip -vc > my_vc.jsonld.gz |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 77 | $ cat my_vc.txt | perl list2vc.pl - | gzip -vc > my_vc.jsonld.gz |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 78 | |
| 79 | HELP |
| 80 | exit 0; |
| 81 | }; |
| 82 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 83 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 84 | sub _shorten ($) { |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 85 | my $line = shift; |
| 86 | if (length($line) < 20) { |
| 87 | return $line; |
| 88 | } |
| 89 | else { |
| 90 | return substr($line,0,17) . '...'; |
| 91 | }; |
| 92 | }; |
| 93 | |
| 94 | |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 95 | my $fh; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 96 | if ($ARGV[0] eq '-') { |
| 97 | $fh = *STDIN; |
| 98 | } elsif (!open($fh, '<' . $ARGV[0])) { |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 99 | warn $ARGV[0] . " can't be opened"; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 100 | exit(0); |
Akron | 18e407a | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 103 | |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 104 | # Create an intensional and an extensional VC |
| 105 | my $vc_ext = KorAP::VirtualCorpus::Group->new('or'); |
| 106 | my $vc_int = KorAP::VirtualCorpus::Group->new('or'); |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 107 | |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 108 | # Initial VC group |
| 109 | my $vc = \$vc_ext; |
| 110 | |
| 111 | my $frozen = 0; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 112 | |
| 113 | # Iterate over the whole list |
| 114 | while (!eof $fh) { |
| 115 | my $line = readline($fh); |
| 116 | chomp $line; |
| 117 | |
Akron | 23e9e3c | 2020-05-20 12:37:25 +0200 | [diff] [blame] | 118 | |
| 119 | # Skip empty lines |
| 120 | if (!$line || length($line) == 0 || $line =~ /^[\s\t\n]*$/) { |
| 121 | # empty |
| 122 | next; |
| 123 | }; |
| 124 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 125 | my ($key, $value, $desc); |
| 126 | |
| 127 | # Line-Type: <e>c</a> |
| 128 | if ($line =~ /^\s*<([^>]+)>\s*([^<]*)\s*<\/\1>\s*$/) { |
| 129 | $key = $1; |
| 130 | $value = $2 // undef; |
| 131 | } |
| 132 | |
| 133 | # Line-Type: <e>c |
| 134 | elsif($line =~ /^\s*<([^>]+)>\s*([^<]+)\s*$/) { |
| 135 | $key = $1; |
| 136 | $value = $2; |
| 137 | } |
| 138 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 139 | # Get text sigles |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 140 | elsif ($line =~ m!^(?:\w+\/){2}\w+$!) { |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 141 | $key = 'text'; |
| 142 | $value = $line; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | # Get doc sigles |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 146 | elsif ($line =~ m!^(\w+\/\w+?)(?:\s.+?)?$!) { |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 147 | $key = 'doc'; |
| 148 | $value = $1; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | # Get corpus sigles |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 152 | elsif ($line !~ m!(?:\/|\s)!) { |
| 153 | $key = 'corpus'; |
| 154 | $value = $line; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 157 | # Not known |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 158 | else { |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 159 | warn _shorten($line) . q! isn't a valid VC definition!; |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 160 | next; |
| 161 | }; |
| 162 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 163 | # Add text field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 164 | if ($key eq 'text') { |
Akron | ee2073d | 2020-05-20 15:19:55 +0200 | [diff] [blame] | 165 | |
| 166 | # Convert C2 sigle to KorAP form |
| 167 | $value =~ s!^([^/]+?/[^\.]+?)\.(.+?)$!$1\/$2!; |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 168 | ${$vc}->add_field(textSigle => $value); |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 171 | # Add doc field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 172 | elsif ($key eq 'doc') { |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 173 | ${$vc}->add_field(docSigle => $value); |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame] | 176 | # Add corpus field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 177 | elsif ($key eq 'corpus') { |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 178 | ${$vc}->add_field(corpusSigle => $value); |
| 179 | } |
| 180 | |
| 181 | # Mark the vc as frozen |
| 182 | # This means that an extended VC area is expected |
| 183 | elsif ($key eq 'frozen') { |
| 184 | $frozen = 1; |
| 185 | } |
| 186 | |
| 187 | # Start/End intended VC area |
| 188 | elsif ($key eq 'intended') { |
| 189 | if ($value eq 'start') { |
| 190 | $$vc = $vc_int; |
| 191 | } |
| 192 | elsif ($value ne 'end') { |
| 193 | warn 'Unknown intension value ' . $value; |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | # Start/End extended VC area |
| 198 | elsif ($key eq 'extended') { |
| 199 | if ($value eq 'start') { |
| 200 | $$vc = $vc_ext; |
| 201 | } |
| 202 | elsif ($value ne 'end') { |
| 203 | warn 'Unknown extension value ' . $value; |
| 204 | }; |
| 205 | } |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 208 | close($fh); |
| 209 | |
Akron | 1d3bd4a | 2020-05-20 17:15:42 +0200 | [diff] [blame^] | 210 | # Stringify current (extended) virtual corpus |
| 211 | print $$vc->to_string; |