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 | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 104 | # Initial VC group |
| 105 | my $vc = KorAP::VirtualCorpus::Group->new('or'); |
| 106 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 107 | |
| 108 | # Iterate over the whole list |
| 109 | while (!eof $fh) { |
| 110 | my $line = readline($fh); |
| 111 | chomp $line; |
| 112 | |
Akron | 23e9e3c | 2020-05-20 12:37:25 +0200 | [diff] [blame] | 113 | |
| 114 | # Skip empty lines |
| 115 | if (!$line || length($line) == 0 || $line =~ /^[\s\t\n]*$/) { |
| 116 | # empty |
| 117 | next; |
| 118 | }; |
| 119 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 120 | my ($key, $value, $desc); |
| 121 | |
| 122 | # Line-Type: <e>c</a> |
| 123 | if ($line =~ /^\s*<([^>]+)>\s*([^<]*)\s*<\/\1>\s*$/) { |
| 124 | $key = $1; |
| 125 | $value = $2 // undef; |
| 126 | } |
| 127 | |
| 128 | # Line-Type: <e>c |
| 129 | elsif($line =~ /^\s*<([^>]+)>\s*([^<]+)\s*$/) { |
| 130 | $key = $1; |
| 131 | $value = $2; |
| 132 | } |
| 133 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 134 | # Get text sigles |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 135 | elsif ($line =~ m!^(?:[^\/\s]+\/){2}[^\/\s]+$!) { |
| 136 | $key = 'text'; |
| 137 | $value = $line; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | # Get doc sigles |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 141 | elsif ($line =~ m!^([^\/\s]+\/[^\/\s]+?)(?:\s.+?)?$!) { |
| 142 | $key = 'doc'; |
| 143 | $value = $1; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | # Get corpus sigles |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 147 | elsif ($line !~ m!(?:\/|\s)!) { |
| 148 | $key = 'corpus'; |
| 149 | $value = $line; |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 152 | # Not known |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 153 | else { |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 154 | warn _shorten($line) . q! isn't a valid VC definition!; |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 155 | next; |
| 156 | }; |
| 157 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 158 | # Add text field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 159 | if ($key eq 'text') { |
Akron | ee2073d | 2020-05-20 15:19:55 +0200 | [diff] [blame] | 160 | |
| 161 | # Convert C2 sigle to KorAP form |
| 162 | $value =~ s!^([^/]+?/[^\.]+?)\.(.+?)$!$1\/$2!; |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 163 | $vc->add_field(textSigle => $value); |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 166 | # Add doc field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 167 | elsif ($key eq 'doc') { |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 168 | $vc->add_field(docSigle => $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 corpus field |
Akron | 1839cb1 | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 172 | elsif ($key eq 'corpus') { |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 173 | $vc->add_field(corpusSigle => $value); |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 174 | }; |
| 175 | }; |
| 176 | |
Akron | 1e6f4d4 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 177 | close($fh); |
| 178 | |
Akron | fe58a6c | 2020-05-20 16:41:22 +0200 | [diff] [blame^] | 179 | # Stringify virtual corpus |
| 180 | print $vc->to_string; |