Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 5 | # 2020-05-20 |
| 6 | # Preliminary support for C2 def-files. |
| 7 | |
| 8 | |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 9 | our @ARGV; |
| 10 | |
Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 11 | unless (@ARGV) { |
| 12 | print <<'HELP'; |
| 13 | Convert a line-separated list of corpus sigles, doc sigles or |
| 14 | text sigles into a virtual corpus query. |
| 15 | |
| 16 | $ perl list2vc.pl my_vc.txt | gzip -vc > my_vc.jsonld.gz |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 17 | $ cat my_vc.txt | perl list2vc.pl - | gzip -vc > my_vc.jsonld.gz |
Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 18 | |
| 19 | HELP |
| 20 | exit 0; |
| 21 | }; |
| 22 | |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 23 | |
| 24 | sub shorten ($) { |
| 25 | my $line = shift; |
| 26 | if (length($line) < 20) { |
| 27 | return $line; |
| 28 | } |
| 29 | else { |
| 30 | return substr($line,0,17) . '...'; |
| 31 | }; |
| 32 | }; |
| 33 | |
| 34 | |
Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 35 | my $fh; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 36 | if ($ARGV[0] eq '-') { |
| 37 | $fh = *STDIN; |
| 38 | } elsif (!open($fh, '<' . $ARGV[0])) { |
Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 39 | warn $ARGV[0] . " can't be opened"; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 40 | exit(0); |
Akron | 3f875be | 2020-05-11 14:57:19 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 43 | |
| 44 | my %data = ( |
| 45 | corpus => [], |
| 46 | doc => [], |
| 47 | text => [] |
| 48 | ); |
| 49 | |
| 50 | # Iterate over the whole list |
| 51 | while (!eof $fh) { |
| 52 | my $line = readline($fh); |
| 53 | chomp $line; |
| 54 | |
Akron | e2645ec | 2020-05-20 12:37:25 +0200 | [diff] [blame] | 55 | |
| 56 | # Skip empty lines |
| 57 | if (!$line || length($line) == 0 || $line =~ /^[\s\t\n]*$/) { |
| 58 | # empty |
| 59 | next; |
| 60 | }; |
| 61 | |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 62 | my ($key, $value, $desc); |
| 63 | |
| 64 | # Line-Type: <e>c</a> |
| 65 | if ($line =~ /^\s*<([^>]+)>\s*([^<]*)\s*<\/\1>\s*$/) { |
| 66 | $key = $1; |
| 67 | $value = $2 // undef; |
| 68 | } |
| 69 | |
| 70 | # Line-Type: <e>c |
| 71 | elsif($line =~ /^\s*<([^>]+)>\s*([^<]+)\s*$/) { |
| 72 | $key = $1; |
| 73 | $value = $2; |
| 74 | } |
| 75 | |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 76 | # Get text sigles |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 77 | elsif ($line =~ m!^(?:[^\/\s]+\/){2}[^\/\s]+$!) { |
| 78 | $key = 'text'; |
| 79 | $value = $line; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | # Get doc sigles |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 83 | elsif ($line =~ m!^([^\/\s]+\/[^\/\s]+?)(?:\s.+?)?$!) { |
| 84 | $key = 'doc'; |
| 85 | $value = $1; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | # Get corpus sigles |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 89 | elsif ($line !~ m!(?:\/|\s)!) { |
| 90 | $key = 'corpus'; |
| 91 | $value = $line; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 94 | # Not known |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 95 | else { |
| 96 | warn shorten($line) . q! isn't a valid sigle!; |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 97 | next; |
| 98 | }; |
| 99 | |
| 100 | if ($key eq 'text') { |
Akron | 68746a1 | 2020-05-20 15:19:55 +0200 | [diff] [blame^] | 101 | |
| 102 | # Convert C2 sigle to KorAP form |
| 103 | $value =~ s!^([^/]+?/[^\.]+?)\.(.+?)$!$1\/$2!; |
Akron | 340a9cb | 2020-05-20 12:55:22 +0200 | [diff] [blame] | 104 | push @{$data{text}}, $value; |
| 105 | } |
| 106 | |
| 107 | elsif ($key eq 'doc') { |
| 108 | push @{$data{doc}}, $value; |
| 109 | } |
| 110 | |
| 111 | elsif ($key eq 'corpus') { |
| 112 | push @{$data{corpus}}, $value; |
Akron | 26b5970 | 2020-05-19 12:14:41 +0200 | [diff] [blame] | 113 | }; |
| 114 | }; |
| 115 | |
| 116 | # Create collection object |
| 117 | my $json = '{'; |
| 118 | $json .= '"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",'; |
| 119 | $json .= '"collection":{'; |
| 120 | |
| 121 | unless (@{$data{corpus}} || @{$data{doc}} || @{$data{text}}) { |
| 122 | $json .= '}}'; |
| 123 | close($fh); |
| 124 | print $json; |
| 125 | exit(0); |
| 126 | }; |
| 127 | |
| 128 | $json .= '"@type":"koral:docGroup",'; |
| 129 | $json .= '"operation":"operation:or",'; |
| 130 | $json .= '"operands":['; |
| 131 | |
| 132 | foreach my $type (qw/corpus doc text/) { |
| 133 | unless (@{$data{$type}}) { |
| 134 | next; |
| 135 | }; |
| 136 | $json .= '{'; |
| 137 | $json .= '"@type":"koral:doc",'; |
| 138 | $json .= '"key":"' . $type . 'Sigle",'; |
| 139 | $json .= '"match":"match:eq",'; |
| 140 | $json .= '"value":['; |
| 141 | $json .= join ',', map { '"' . $_ . '"' } @{$data{$type}}; |
| 142 | $json .= ']'; |
| 143 | $json .= '},'; |
| 144 | }; |
| 145 | |
| 146 | # Remove the last comma |
| 147 | chop $json; |
| 148 | |
| 149 | $json .= ']}}'; |
| 150 | |
| 151 | close($fh); |
| 152 | |
| 153 | print $json; |
| 154 | |