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 | |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 174 | # Koral version |
| 175 | $input = catdir($f, '..', 'annotation', 'corpus', 'doc', '0001'); |
| 176 | $call = join( |
| 177 | ' ', |
| 178 | 'perl', $script, |
| 179 | '--input' => $input, |
| 180 | '--output' => $output, |
| 181 | '--cache' => $cache, |
| 182 | '-t' => 'OpenNLP#Tokens', |
| 183 | '-k' => 0.4, |
| 184 | '-l' => 'INFO' |
| 185 | ); |
| 186 | |
| 187 | $call .= ' -w '; |
| 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'); |
| 198 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 199 | ok(($json = decode_json $file), 'decode json'); |
| 200 | ok(!$json->{textType}, 'text type'); |
| 201 | ok(!$json->{title}, 'Title'); |
| 202 | |
| 203 | is($json->{fields}->[0]->{key}, 'corpusSigle'); |
| 204 | is($json->{fields}->[0]->{type}, 'type:string'); |
| 205 | is($json->{fields}->[0]->{value}, 'Corpus'); |
| 206 | is($json->{fields}->[0]->{'@type'}, 'koral:field'); |
| 207 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 208 | is($json->{fields}->[4]->{key}, 'distributor'); |
| 209 | is($json->{fields}->[4]->{value}, 'data:,Institut für Deutsche Sprache'); |
| 210 | is($json->{fields}->[4]->{type}, 'type:attachement'); |
| 211 | is($json->{fields}->[4]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 212 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 213 | is($json->{fields}->[9]->{key}, 'textClass'); |
| 214 | is($json->{fields}->[9]->{value}->[0], 'freizeit-unterhaltung'); |
| 215 | is($json->{fields}->[9]->{value}->[1], 'vereine-veranstaltungen'); |
| 216 | is($json->{fields}->[9]->{type}, 'type:keywords'); |
| 217 | is($json->{fields}->[9]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 218 | |
Akron | 0d68a4b | 2019-11-13 15:42:11 +0100 | [diff] [blame] | 219 | is($json->{fields}->[14]->{key}, 'textType'); |
| 220 | is($json->{fields}->[14]->{value}, 'Zeitung: Tageszeitung'); |
| 221 | is($json->{fields}->[14]->{type}, 'type:string'); |
| 222 | is($json->{fields}->[14]->{'@type'}, 'koral:field'); |
| 223 | |
| 224 | is($json->{fields}->[22]->{key}, 'title'); |
| 225 | is($json->{fields}->[22]->{value}, 'Beispiel Text'); |
| 226 | is($json->{fields}->[22]->{type}, 'type:text'); |
| 227 | is($json->{fields}->[22]->{'@type'}, 'koral:field'); |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 228 | |
| 229 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
| 230 | 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'); |
| 231 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 232 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 233 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
| 234 | |
| 235 | # Delete output |
| 236 | unlink $output; |
| 237 | ok(!-f $output, 'Output does not exist'); |
| 238 | |
| 239 | |
Akron | 64f7fae | 2022-07-27 12:45:33 +0200 | [diff] [blame] | 240 | # Koral version |
| 241 | $input = catdir($f, '..', 'real', 'corpus', 'NKJP', 'NKJP', 'KOT'); |
| 242 | $call = join( |
| 243 | ' ', |
| 244 | 'perl', $script, |
| 245 | '--input' => $input, |
| 246 | '--output' => $output, |
| 247 | '--cache' => $cache, |
| 248 | '-t' => 'NKJP#Morpho', |
| 249 | '-l' => 'INFO', |
| 250 | '--lang' => 'en' |
| 251 | ); |
| 252 | |
| 253 | $call .= ' -w '; |
| 254 | |
| 255 | stderr_like( |
| 256 | sub { |
| 257 | system($call); |
| 258 | }, |
| 259 | qr!The code took!, |
| 260 | $call |
| 261 | ); |
| 262 | |
| 263 | ok(-f $output, 'Output does exist'); |
| 264 | ok(($file = Mojo::File->new($output)->slurp), 'Slurp data'); |
| 265 | ok(($json = decode_json $file), 'decode json'); |
| 266 | is($json->{corpusTitle}, 'National Corpus of Polish -- the 1 million word subcorpus', 'Title'); |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 272 | done_testing; |
| 273 | __END__ |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 274 | |