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