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 | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 28 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 29 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 30 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 31 | my $call = join( |
| 32 | ' ', |
| 33 | 'perl', $script, |
| 34 | '--input' => $input, |
| 35 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 36 | '--cache' => $cache, |
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 | |
| 51 | ok(-f $output, 'Output does exist'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 52 | ok((my $file = Mojo::File->new($output)->slurp), 'Slurp data'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 53 | ok((my $json = decode_json $file), 'decode json'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 54 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 55 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 56 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
| 57 | 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 treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
| 58 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 59 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 60 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 61 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 62 | # Delete output |
| 63 | unlink $output; |
| 64 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 65 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 66 | $call .= ' -z'; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 67 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 68 | # Test with compression |
| 69 | stderr_like( |
| 70 | sub { system($call); }, |
| 71 | qr!The code took!, |
| 72 | $call |
| 73 | ); |
| 74 | |
| 75 | ok(-f $output, 'Output does exist'); |
| 76 | |
| 77 | # Uncompress the data using a buffer |
| 78 | my $gz = IO::Uncompress::Gunzip->new($output, Transparent => 0); |
| 79 | ($file, my $buffer) = ''; |
| 80 | while ($gz->read($buffer)) { |
| 81 | $file .= $buffer; |
| 82 | }; |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 83 | $gz->close; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 84 | |
| 85 | ok($json = decode_json($file), 'decode json'); |
| 86 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 87 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 88 | is($json->{title}, 'Beispiel Text', 'Title'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 89 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 90 | 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 treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
| 91 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 92 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 93 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 94 | # Delete output |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 95 | is(unlink($output), 1, 'Unlink successful'); |
| 96 | ok(!-e $output, 'Output does not exist'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 97 | |
| 98 | # Use a different token source and skip all annotations, |
| 99 | # except for DeReKo#Structure and Mate#Dependency |
| 100 | $call = join( |
| 101 | ' ', |
| 102 | 'perl', $script, |
| 103 | '--input' => $input, |
| 104 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 105 | '--cache' => $cache, |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 106 | '-t' => 'CoreNLP#Tokens', |
| 107 | '-s' => '#all', |
| 108 | '-a' => 'DeReKo#Structure', |
| 109 | '-a' => 'Mate#Dependency', |
| 110 | '-l' => 'INFO' |
| 111 | ); |
| 112 | |
| 113 | stderr_like( |
| 114 | sub { |
| 115 | system($call); |
| 116 | }, |
| 117 | qr!The code took!, |
| 118 | $call |
| 119 | ); |
| 120 | |
| 121 | ok(-f $output, 'Output does exist'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 122 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 123 | ok(($json = decode_json $file), 'decode json'); |
| 124 | |
| 125 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 126 | |
| 127 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 128 | is($json->{data}->{tokenSource}, 'corenlp#tokens', 'TokenSource'); |
| 129 | is($json->{data}->{foundries}, 'dereko dereko/structure mate mate/dependency', 'Foundries'); |
| 130 | |
| 131 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 132 | is($json->{data}->{stream}->[0]->[0], '-:tokens$<i>20', 'Tokens'); |
| 133 | |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 134 | |
| 135 | # Check overwrite |
| 136 | $call = join( |
| 137 | ' ', |
| 138 | 'perl', $script, |
| 139 | '--input' => $input, |
| 140 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 141 | '--cache' => $cache, |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 142 | '-t' => 'CoreNLP#Tokens', |
| 143 | '-s' => '#all', |
| 144 | '-a' => 'DeReKo#Structure', |
| 145 | '-a' => 'Mate#Dependency', |
| 146 | '-l' => 'DEBUG' |
| 147 | ); |
| 148 | |
| 149 | ok(-f $output, 'Output does exist'); |
| 150 | stderr_like( |
| 151 | sub { |
| 152 | system($call); |
| 153 | }, |
| 154 | qr!already exists!, |
| 155 | $call |
| 156 | ); |
| 157 | |
| 158 | $call .= ' -w '; |
| 159 | |
| 160 | stderr_unlike( |
| 161 | sub { |
| 162 | system($call); |
| 163 | }, |
| 164 | qr!already exists!, |
| 165 | $call |
| 166 | ); |
| 167 | |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 168 | # Check meta data switch |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame] | 169 | |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 170 | # Delete output |
| 171 | unlink $output; |
| 172 | ok(!-f $output, 'Output does not exist'); |
| 173 | |
| 174 | $input = catdir($f, '..', 'sgbr', 'PRO-DUD', 'BSP-2013-01', '32'); |
| 175 | |
| 176 | # Use a different token source and skip all annotations, |
| 177 | # except for DeReKo#Structure and Mate#Dependency |
| 178 | $call = join( |
| 179 | ' ', |
| 180 | 'perl', $script, |
| 181 | '--input' => $input, |
| 182 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 183 | '--cache' => $cache, |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 184 | '-m' => 'Sgbr', |
| 185 | '-t' => 'Base#Tokens_aggr', |
| 186 | '-l' => 'INFO' |
| 187 | ); |
| 188 | |
| 189 | stderr_like( |
| 190 | sub { |
| 191 | system($call); |
| 192 | }, |
| 193 | qr!The code took!, |
| 194 | $call |
| 195 | ); |
| 196 | |
| 197 | ok(-f $output, 'Output does exist'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 198 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
Akron | f98b669 | 2016-08-16 19:17:44 +0200 | [diff] [blame] | 199 | ok(($json = decode_json $file), 'decode json'); |
| 200 | |
| 201 | is($json->{data}->{text}, 'Selbst ist der Jeck', 'Text'); |
| 202 | is($json->{data}->{tokenSource}, 'base#tokens_aggr', 'TokenSource'); |
| 203 | is($json->{pubPlace}, 'Stadtingen', 'pubPlace'); |
| 204 | is($json->{textSigle}, 'PRO-DUD/BSP-2013-01/32', 'textSigle'); |
| 205 | is($json->{docSigle}, 'PRO-DUD/BSP-2013-01', 'docSigle'); |
| 206 | is($json->{corpusSigle}, 'PRO-DUD', 'corpusSigle'); |
| 207 | is($json->{sgbrKodex}, 'T', 'sgbrKodex'); |
| 208 | is($json->{author}, 'unbekannt', 'Author'); |
| 209 | is($json->{language}, 'de', 'Language'); |
| 210 | is($json->{docTitle}, 'Korpus zur Beobachtung des Schreibgebrauchs im Deutschen', 'docTitle'); |
| 211 | is($json->{funder}, 'Bundesministerium für Bildung und Forschung', 'docTitle'); |
| 212 | is($json->{title}, 'Nur Platt, kein Deutsch', 'title'); |
| 213 | is($json->{pubDate}, '20130126', 'pubDate'); |
| 214 | is($json->{docSubTitle}, 'Subkorpus Ortsblatt, Jahrgang 2013, Monat Januar', 'docSubTitle'); |
| 215 | is($json->{keywords}, 'sgbrKodex:T', 'keywords'); |
| 216 | is($json->{publisher}, 'Dorfblatt GmbH', 'publisher'); |
| 217 | |
Akron | 636bd9c | 2017-02-09 17:13:00 +0100 | [diff] [blame] | 218 | |
| 219 | |
| 220 | # AGA with base info |
| 221 | unlink $output; |
| 222 | ok(!-f $output, 'Output does not exist'); |
| 223 | $input = catdir($f, '..', 'corpus', 'GOE2', 'AGA', '03828'); |
| 224 | ok(-d $input, 'Input directory found'); |
| 225 | |
| 226 | ok(!-f $output, 'Output does not exist'); |
| 227 | |
| 228 | $call = join( |
| 229 | ' ', |
| 230 | 'perl', $script, |
| 231 | '--input' => $input, |
| 232 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 233 | '--cache' => $cache, |
Akron | 636bd9c | 2017-02-09 17:13:00 +0100 | [diff] [blame] | 234 | '-t' => 'base#tokens_aggr', |
| 235 | '-bs' => 'DeReKo#Structure', |
| 236 | '-bp' => 'DeReKo#Structure', |
| 237 | '-bpb' => 'DeReKo#Structure', |
| 238 | '-l' => 'INFO' |
| 239 | ); |
| 240 | |
| 241 | stderr_like( |
| 242 | sub { |
| 243 | system($call); |
| 244 | }, |
| 245 | qr!The code took!, |
| 246 | $call |
| 247 | ); |
| 248 | ok(-f $output, 'Output does exist'); |
| 249 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 250 | ok(($json = decode_json $file), 'decode json'); |
| 251 | |
| 252 | is($json->{title}, 'Autobiographische Einzelheiten', 'title'); |
| 253 | is($json->{data}->{stream}->[0]->[-1], '~:base/s:pb$<i>529<i>0', 'Pagebreak annotation'); |
| 254 | |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 255 | |
| 256 | |
| 257 | # Koral version |
| 258 | $input = catdir($f, '..', 'annotation', 'corpus', 'doc', '0001'); |
| 259 | $call = join( |
| 260 | ' ', |
| 261 | 'perl', $script, |
| 262 | '--input' => $input, |
| 263 | '--output' => $output, |
| 264 | '--cache' => $cache, |
| 265 | '-t' => 'OpenNLP#Tokens', |
| 266 | '-k' => 0.4, |
| 267 | '-l' => 'INFO' |
| 268 | ); |
| 269 | |
| 270 | $call .= ' -w '; |
| 271 | |
| 272 | stderr_like( |
| 273 | sub { |
| 274 | system($call); |
| 275 | }, |
| 276 | qr!The code took!, |
| 277 | $call |
| 278 | ); |
| 279 | |
| 280 | ok(-f $output, 'Output does exist'); |
| 281 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 282 | ok(($json = decode_json $file), 'decode json'); |
| 283 | ok(!$json->{textType}, 'text type'); |
| 284 | ok(!$json->{title}, 'Title'); |
| 285 | |
| 286 | is($json->{fields}->[0]->{key}, 'corpusSigle'); |
| 287 | is($json->{fields}->[0]->{type}, 'type:string'); |
| 288 | is($json->{fields}->[0]->{value}, 'Corpus'); |
| 289 | is($json->{fields}->[0]->{'@type'}, 'koral:field'); |
| 290 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 291 | is($json->{fields}->[4]->{key}, 'distributor'); |
| 292 | is($json->{fields}->[4]->{value}, 'data:,Institut für Deutsche Sprache'); |
| 293 | is($json->{fields}->[4]->{type}, 'type:attachement'); |
| 294 | is($json->{fields}->[4]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 295 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 296 | is($json->{fields}->[9]->{key}, 'textClass'); |
| 297 | is($json->{fields}->[9]->{value}->[0], 'freizeit-unterhaltung'); |
| 298 | is($json->{fields}->[9]->{value}->[1], 'vereine-veranstaltungen'); |
| 299 | is($json->{fields}->[9]->{type}, 'type:keywords'); |
| 300 | is($json->{fields}->[9]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 301 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 302 | is($json->{fields}->[14]->{key}, 'textType'); |
| 303 | is($json->{fields}->[14]->{value}, 'Zeitung: Tageszeitung'); |
| 304 | is($json->{fields}->[14]->{type}, 'type:string'); |
| 305 | is($json->{fields}->[14]->{'@type'}, 'koral:field'); |
| 306 | |
| 307 | is($json->{fields}->[22]->{key}, 'title'); |
| 308 | is($json->{fields}->[22]->{value}, 'Beispiel Text'); |
| 309 | is($json->{fields}->[22]->{type}, 'type:text'); |
| 310 | is($json->{fields}->[22]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 311 | |
| 312 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
| 313 | 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 treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
| 314 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 315 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 316 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
| 317 | |
| 318 | # Delete output |
| 319 | unlink $output; |
| 320 | ok(!-f $output, 'Output does not exist'); |
| 321 | |
| 322 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 323 | done_testing; |
| 324 | __END__ |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 325 | |