Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 1 | #/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use File::Basename 'dirname'; |
| 5 | use File::Spec::Functions qw/catdir catfile/; |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 6 | use File::Temp qw/:POSIX/; |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 7 | use Mojo::File; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 8 | use Mojo::JSON qw/decode_json/; |
| 9 | use IO::Uncompress::Gunzip; |
| 10 | use Test::More; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 11 | use Test::Output; |
| 12 | use Data::Dumper; |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 13 | use utf8; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 14 | |
Akron | fab17d3 | 2020-07-31 14:38:29 +0200 | [diff] [blame] | 15 | if ($ENV{SKIP_SCRIPT}) { |
| 16 | plan skip_all => 'Skip script tests'; |
| 17 | }; |
| 18 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 19 | my $f = dirname(__FILE__); |
| 20 | my $script = catfile($f, '..', '..', 'script', 'korapxml2krill'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 21 | |
| 22 | my $input = catdir($f, '..', 'annotation', 'corpus', 'doc', '0001'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 23 | ok(-d $input, 'Input directory found'); |
| 24 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 25 | my $output = tmpnam(); |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 26 | my $cache = tmpnam(); |
| 27 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 28 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 29 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 30 | my $call = join( |
| 31 | ' ', |
| 32 | 'perl', $script, |
| 33 | '--input' => $input, |
| 34 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 35 | '--cache' => $cache, |
Akron | c0ac4ff | 2024-04-15 18:03:15 +0200 | [diff] [blame^] | 36 | '--no-cache-delete', |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 37 | '-k' => 0.03, |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 38 | '-t' => 'OpenNLP#Tokens', |
| 39 | '-l' => 'INFO' |
| 40 | ); |
| 41 | |
| 42 | # Test without compression |
| 43 | stderr_like( |
| 44 | sub { |
| 45 | system($call); |
| 46 | }, |
| 47 | qr!The code took!, |
| 48 | $call |
| 49 | ); |
| 50 | |
Akron | c0ac4ff | 2024-04-15 18:03:15 +0200 | [diff] [blame^] | 51 | ok(-f $cache, 'Cache does exist'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 52 | ok(-f $output, 'Output does exist'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 53 | ok((my $file = Mojo::File->new($output)->slurp), 'Slurp data'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 54 | ok((my $json = decode_json $file), 'decode json'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 55 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 56 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 57 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
Marc Kupietz | b8c5382 | 2024-03-16 18:54:08 +0100 | [diff] [blame] | 58 | is($json->{data}->{foundries}, 'base base/paragraphs base/sentences connexor connexor/morpho connexor/phrase connexor/sentences connexor/syntax corenlp corenlp/constituency corenlp/morpho corenlp/sentences dereko dereko/structure glemm glemm/morpho mate mate/dependency mate/morpho opennlp opennlp/morpho opennlp/sentences spacy spacy/morpho treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 59 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 60 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 61 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 62 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 63 | # Delete output |
| 64 | unlink $output; |
Akron | c0ac4ff | 2024-04-15 18:03:15 +0200 | [diff] [blame^] | 65 | unlink $cache; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 66 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 67 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 68 | $call .= ' -z'; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 69 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 70 | # Test with compression |
| 71 | stderr_like( |
| 72 | sub { system($call); }, |
| 73 | qr!The code took!, |
| 74 | $call |
| 75 | ); |
| 76 | |
| 77 | ok(-f $output, 'Output does exist'); |
| 78 | |
| 79 | # Uncompress the data using a buffer |
| 80 | my $gz = IO::Uncompress::Gunzip->new($output, Transparent => 0); |
| 81 | ($file, my $buffer) = ''; |
| 82 | while ($gz->read($buffer)) { |
| 83 | $file .= $buffer; |
| 84 | }; |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 85 | $gz->close; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 86 | |
| 87 | ok($json = decode_json($file), 'decode json'); |
| 88 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 89 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 90 | is($json->{title}, 'Beispiel Text', 'Title'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 91 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Marc Kupietz | b8c5382 | 2024-03-16 18:54:08 +0100 | [diff] [blame] | 92 | is($json->{data}->{foundries}, 'base base/paragraphs base/sentences connexor connexor/morpho connexor/phrase connexor/sentences connexor/syntax corenlp corenlp/constituency corenlp/morpho corenlp/sentences dereko dereko/structure glemm glemm/morpho mate mate/dependency mate/morpho opennlp opennlp/morpho opennlp/sentences spacy spacy/morpho treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 93 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 94 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 95 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 96 | # Delete output |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 97 | is(unlink($output), 1, 'Unlink successful'); |
| 98 | ok(!-e $output, 'Output does not exist'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 99 | |
| 100 | # Use a different token source and skip all annotations, |
| 101 | # except for DeReKo#Structure and Mate#Dependency |
| 102 | $call = join( |
| 103 | ' ', |
| 104 | 'perl', $script, |
| 105 | '--input' => $input, |
| 106 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 107 | '--cache' => $cache, |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 108 | '-t' => 'CoreNLP#Tokens', |
| 109 | '-s' => '#all', |
| 110 | '-a' => 'DeReKo#Structure', |
| 111 | '-a' => 'Mate#Dependency', |
| 112 | '-l' => 'INFO' |
| 113 | ); |
| 114 | |
| 115 | stderr_like( |
| 116 | sub { |
| 117 | system($call); |
| 118 | }, |
| 119 | qr!The code took!, |
| 120 | $call |
| 121 | ); |
| 122 | |
| 123 | ok(-f $output, 'Output does exist'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 124 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 125 | ok(($json = decode_json $file), 'decode json'); |
| 126 | |
| 127 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 128 | |
| 129 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 130 | is($json->{data}->{tokenSource}, 'corenlp#tokens', 'TokenSource'); |
| 131 | is($json->{data}->{foundries}, 'dereko dereko/structure mate mate/dependency', 'Foundries'); |
| 132 | |
| 133 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 134 | is($json->{data}->{stream}->[0]->[0], '-:tokens$<i>20', 'Tokens'); |
| 135 | |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 136 | |
| 137 | # Check overwrite |
| 138 | $call = join( |
| 139 | ' ', |
| 140 | 'perl', $script, |
| 141 | '--input' => $input, |
| 142 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 143 | '--cache' => $cache, |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 144 | '-t' => 'CoreNLP#Tokens', |
| 145 | '-s' => '#all', |
| 146 | '-a' => 'DeReKo#Structure', |
| 147 | '-a' => 'Mate#Dependency', |
| 148 | '-l' => 'DEBUG' |
| 149 | ); |
| 150 | |
| 151 | ok(-f $output, 'Output does exist'); |
| 152 | stderr_like( |
| 153 | sub { |
| 154 | system($call); |
| 155 | }, |
| 156 | qr!already exists!, |
| 157 | $call |
| 158 | ); |
| 159 | |
| 160 | $call .= ' -w '; |
| 161 | |
| 162 | stderr_unlike( |
| 163 | sub { |
| 164 | system($call); |
| 165 | }, |
| 166 | qr!already exists!, |
| 167 | $call |
| 168 | ); |
| 169 | |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 170 | # Check meta data switch |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 171 | |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 172 | # Delete output |
| 173 | unlink $output; |
| 174 | ok(!-f $output, 'Output does not exist'); |
| 175 | |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 176 | # Koral version |
| 177 | $input = catdir($f, '..', 'annotation', 'corpus', 'doc', '0001'); |
| 178 | $call = join( |
| 179 | ' ', |
| 180 | 'perl', $script, |
| 181 | '--input' => $input, |
| 182 | '--output' => $output, |
| 183 | '--cache' => $cache, |
| 184 | '-t' => 'OpenNLP#Tokens', |
| 185 | '-k' => 0.4, |
| 186 | '-l' => 'INFO' |
| 187 | ); |
| 188 | |
| 189 | $call .= ' -w '; |
| 190 | |
| 191 | stderr_like( |
| 192 | sub { |
| 193 | system($call); |
| 194 | }, |
| 195 | qr!The code took!, |
| 196 | $call |
| 197 | ); |
| 198 | |
| 199 | ok(-f $output, 'Output does exist'); |
| 200 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 201 | ok(($json = decode_json $file), 'decode json'); |
| 202 | ok(!$json->{textType}, 'text type'); |
| 203 | ok(!$json->{title}, 'Title'); |
| 204 | |
| 205 | is($json->{fields}->[0]->{key}, 'corpusSigle'); |
| 206 | is($json->{fields}->[0]->{type}, 'type:string'); |
| 207 | is($json->{fields}->[0]->{value}, 'Corpus'); |
| 208 | is($json->{fields}->[0]->{'@type'}, 'koral:field'); |
| 209 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 210 | is($json->{fields}->[4]->{key}, 'distributor'); |
| 211 | is($json->{fields}->[4]->{value}, 'data:,Institut für Deutsche Sprache'); |
| 212 | is($json->{fields}->[4]->{type}, 'type:attachement'); |
| 213 | is($json->{fields}->[4]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 214 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 215 | is($json->{fields}->[9]->{key}, 'textClass'); |
| 216 | is($json->{fields}->[9]->{value}->[0], 'freizeit-unterhaltung'); |
| 217 | is($json->{fields}->[9]->{value}->[1], 'vereine-veranstaltungen'); |
| 218 | is($json->{fields}->[9]->{type}, 'type:keywords'); |
| 219 | is($json->{fields}->[9]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 220 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 221 | is($json->{fields}->[14]->{key}, 'textType'); |
| 222 | is($json->{fields}->[14]->{value}, 'Zeitung: Tageszeitung'); |
| 223 | is($json->{fields}->[14]->{type}, 'type:string'); |
| 224 | is($json->{fields}->[14]->{'@type'}, 'koral:field'); |
| 225 | |
| 226 | is($json->{fields}->[22]->{key}, 'title'); |
| 227 | is($json->{fields}->[22]->{value}, 'Beispiel Text'); |
| 228 | is($json->{fields}->[22]->{type}, 'type:text'); |
| 229 | is($json->{fields}->[22]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 230 | |
| 231 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
Marc Kupietz | b8c5382 | 2024-03-16 18:54:08 +0100 | [diff] [blame] | 232 | is($json->{data}->{foundries}, 'base base/paragraphs base/sentences connexor connexor/morpho connexor/phrase connexor/sentences connexor/syntax corenlp corenlp/constituency corenlp/morpho corenlp/sentences dereko dereko/structure glemm glemm/morpho mate mate/dependency mate/morpho opennlp opennlp/morpho opennlp/sentences spacy spacy/morpho treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 233 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 234 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 235 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
| 236 | |
Marc Kupietz | b8c5382 | 2024-03-16 18:54:08 +0100 | [diff] [blame] | 237 | my $token = join(',',@{$json->{data}->{stream}->[1]}); |
| 238 | |
| 239 | like($token, qr!<>:xip\/c:AP\$<b>64<i>4<i>11<i>2<b>5!); |
| 240 | like($token, qr!<>:xip\/c:ADJ\$<b>64<i>4<i>11<i>2<b>6!); |
| 241 | like($token, qr!<>:cnx\/c:np\$<b>64<i>4<i>30<i>4<b>0!); |
| 242 | like($token, qr!<>:xip\/c:NP\$<b>64<i>4<i>30<i>4<b>3!); |
| 243 | like($token, qr!<>:xip\/c:NPA\$<b>64<i>4<i>30<i>4<b>4!); |
| 244 | like($token, qr!>:mate\/d:NK\$<b>32<i>3!); |
| 245 | like($token, qr!_1\$<i>4<i>11!); |
| 246 | like($token, qr!cnx\/l:letzt!); |
| 247 | like($token, qr!cnx\/p:A!); |
| 248 | like($token, qr!cnx\/syn:\@PREMOD!); |
| 249 | like($token, qr!corenlp\/p:ADJ!); |
| 250 | like($token, qr!glemm\/l:__letzt-!); |
| 251 | like($token, qr!i:letzten!); |
| 252 | like($token, qr!mate\/l:letzter!); |
| 253 | like($token, qr!mate\/m:case:dat!); |
| 254 | like($token, qr!mate\/m:degree:pos!); |
| 255 | like($token, qr!mate\/m:gender:neut!); |
| 256 | like($token, qr!mate\/m:number:sg!); |
| 257 | like($token, qr!mate\/p:ADJA!); |
| 258 | like($token, qr!opennlp\/p:ADJA!); |
| 259 | like($token, qr!s:letzten!); |
| 260 | like($token, qr!spacy\/l:letzter!); |
| 261 | like($token, qr!spacy\/p:ADJ!); |
| 262 | like($token, qr!tt\/l:letzt!); |
| 263 | like($token, qr!tt\/p:ADJA!); |
| 264 | like($token, qr!xip\/l:letzt!); |
| 265 | like($token, qr!xip\/p:ADJ!); |
| 266 | |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 267 | # Delete output |
| 268 | unlink $output; |
| 269 | ok(!-f $output, 'Output does not exist'); |
| 270 | |
| 271 | |
Akron | 64f7fae | 2022-07-27 12:45:33 +0200 | [diff] [blame] | 272 | # Koral version |
| 273 | $input = catdir($f, '..', 'real', 'corpus', 'NKJP', 'NKJP', 'KOT'); |
| 274 | $call = join( |
| 275 | ' ', |
| 276 | 'perl', $script, |
| 277 | '--input' => $input, |
| 278 | '--output' => $output, |
| 279 | '--cache' => $cache, |
| 280 | '-t' => 'NKJP#Morpho', |
| 281 | '-l' => 'INFO', |
| 282 | '--lang' => 'en' |
| 283 | ); |
| 284 | |
| 285 | $call .= ' -w '; |
| 286 | |
| 287 | stderr_like( |
| 288 | sub { |
| 289 | system($call); |
| 290 | }, |
| 291 | qr!The code took!, |
| 292 | $call |
| 293 | ); |
| 294 | |
| 295 | ok(-f $output, 'Output does exist'); |
| 296 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 297 | ok(($json = decode_json $file), 'decode json'); |
| 298 | is($json->{corpusTitle}, 'National Corpus of Polish -- the 1 million word subcorpus', 'Title'); |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 304 | done_testing; |
| 305 | __END__ |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 306 | |