blob: 8d0fba1a1b3d85d82c908c08e1089bc81864308d [file] [log] [blame]
Akron7d4cdd82016-08-17 21:39:45 +02001#/usr/bin/env perl
2use strict;
3use warnings;
4use File::Basename 'dirname';
5use File::Spec::Functions qw/catdir catfile/;
Akrond5bb4342017-06-19 11:50:49 +02006use File::Temp qw/:POSIX/;
Akron3ec0a1c2017-01-18 14:41:55 +01007use Mojo::File;
Akron821db3d2017-04-06 21:19:31 +02008use Mojo::Util qw/quote/;
Akron7d4cdd82016-08-17 21:39:45 +02009use Mojo::JSON qw/decode_json/;
10use IO::Uncompress::Gunzip;
11use Test::More;
Akron63d03ee2019-02-13 18:49:38 +010012use Test::Output qw/:stdout :stderr :combined :functions/;
Akron7d4cdd82016-08-17 21:39:45 +020013use Data::Dumper;
Nils Diewaldb3e9ccd2016-10-24 15:16:52 +020014use KorAP::XML::Archive;
Akron7d4cdd82016-08-17 21:39:45 +020015use utf8;
16
17my $f = dirname(__FILE__);
18my $script = catfile($f, '..', '..', 'script', 'korapxml2krill');
19
20my $call = join(
21 ' ',
22 'perl', $script,
23 'archive'
24);
25
Nils Diewaldb3e9ccd2016-10-24 15:16:52 +020026unless (KorAP::XML::Archive::test_unzip) {
27 plan skip_all => 'unzip not found';
28};
29
Akron7d4cdd82016-08-17 21:39:45 +020030# Test without parameters
31stdout_like(
32 sub {
33 system($call);
34 },
Akrona76d8352016-10-27 16:27:32 +020035 qr!archive.+?\$ korapxml2krill!s,
Akron7d4cdd82016-08-17 21:39:45 +020036 $call
37);
38
39my $input = catfile($f, '..', 'corpus', 'archive.zip');
40ok(-f $input, 'Input archive found');
41
Akron3ec48972016-08-17 23:24:52 +020042my $output = File::Temp->newdir(CLEANUP => 0);
43$output->unlink_on_destroy(0);
44
Akrond5bb4342017-06-19 11:50:49 +020045my $cache = tmpnam();
46
Akron7d4cdd82016-08-17 21:39:45 +020047ok(-d $output, 'Output directory exists');
48
49$call = join(
50 ' ',
51 'perl', $script,
52 'archive',
Akron821db3d2017-04-06 21:19:31 +020053 '--input' => '' . $input,
Akron7d4cdd82016-08-17 21:39:45 +020054 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +020055 '--cache' => $cache,
Akron7d4cdd82016-08-17 21:39:45 +020056 '-t' => 'Base#tokens_aggr',
57 '-m' => 'Sgbr'
58);
59
60# Test without compression
61my $json;
62{
63 local $SIG{__WARN__} = sub {};
64 my $out = stdout_from(sub { system($call); });
65
66 like($out, qr!TEST-BSP-1\.json!s, $call);
67
68 $out =~ m!Processed (.+?\.json)!;
69 $json = $1;
70};
71
72ok(-f $json, 'Json file exists');
Akron3ec0a1c2017-01-18 14:41:55 +010073ok((my $file = Mojo::File->new($json)->slurp), 'Slurp data');
Akron7d4cdd82016-08-17 21:39:45 +020074ok(($json = decode_json $file), 'decode json');
75
76is($json->{data}->{tokenSource}, 'base#tokens_aggr', 'Title');
77is($json->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure sgbr sgbr/lemma sgbr/morpho', 'Foundries');
78is($json->{sgbrKodex}, 'M', 'Kodex meta data');
79
Akron3ec48972016-08-17 23:24:52 +020080
81# Use directory
82$input = catdir($f, '..', 'annotation', 'corpus');
83
84$call = join(
85 ' ',
86 'perl', $script,
87 'archive',
88 '--input' => $input,
89 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +020090 '--cache' => $cache,
Akron3ec48972016-08-17 23:24:52 +020091 '-t' => 'Tree_Tagger#Tokens',
92 '-j' => 4 # 4 jobs!
93);
94
95my ($json_1, $json_2);
96
97{
98 local $SIG{__WARN__} = sub {};
99
100 # That's not really stable on slow machines!
101 my $out = stdout_from(sub { system($call); });
102
103 ok($out =~ m!\[\$(\d+?):1\/2\]!s, $call . ' pid 1');
104 my $pid1 = $1;
105 ok($out =~ m!\[\$(\d+?):2\/2\]!s, $call . ' pid 2');
106 my $pid2 = $1;
107
108 isnt($pid1, $pid2, 'No PID match');
109
110 ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call);
111 ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call);
112
113 ok(-d $output, 'Temporary directory still exists');
114 my $json_1 = catfile($output, 'corpus-doc-0001.json');
115 ok(-f $json_1, 'Json file exists 1');
116 my $json_2 = catfile($output, 'corpus-doc-0002.json');
117 ok(-f $json_2, 'Json file exists 2');
118
Akron3ec0a1c2017-01-18 14:41:55 +0100119 ok(($file = Mojo::File->new($json_1)->slurp), 'Slurp data');
Akron3ec48972016-08-17 23:24:52 +0200120 ok(($json_1 = decode_json $file), 'decode json');
121
122 is($json_1->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource');
123 is($json_1->{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');
124 is($json_1->{textSigle}, 'Corpus/Doc/0001', 'Sigle');
125
126 ok(-f $json_2, 'Json file exists');
Akron3ec0a1c2017-01-18 14:41:55 +0100127 ok(($file = Mojo::File->new($json_2)->slurp), 'Slurp data');
Akron3ec48972016-08-17 23:24:52 +0200128 ok(($json_2 = decode_json $file), 'decode json');
129
130 is($json_2->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource');
131 is($json_2->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure malt malt/dependency treetagger treetagger/morpho treetagger/sentences', 'Foundries');
132 is($json_2->{textSigle}, 'Corpus/Doc/0002', 'Sigle');
133};
134
135ok(-d $output, 'Ouput directory exists');
Akron89df4fa2016-11-04 14:35:37 +0100136
137
Akron63d03ee2019-02-13 18:49:38 +0100138my $temp_extract = tmpnam();
139
140# Ignore -te when archive is a directory
141$call = join(
142 ' ',
143 'perl', $script,
144 'archive',
145 '--input' => $input,
146 '--output' => $output,
147 '--cache' => $cache,
148 '-t' => 'Tree_Tagger#Tokens',
149 '-j' => 4, # 4 jobs!
150 '-te' => $temp_extract
151);
152
153{
154 local $SIG{__WARN__} = sub {};
155
156 my $out = combined_from(sub { system($call); });
157
158 ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call);
159 ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call);
160};
161
162
Akron89df4fa2016-11-04 14:35:37 +0100163$input = catfile($f, '..', 'corpus', 'WDD15', 'A79', '83946');
164$call = join(
165 ' ',
166 'perl', $script,
Akrond5bb4342017-06-19 11:50:49 +0200167 '--input' => $input,
168 '--cache' => $cache
Akron89df4fa2016-11-04 14:35:37 +0100169);
170
171# Test without compression
172{
173 local $SIG{__WARN__} = sub {};
174 my $out = stderr_from(sub { system($call); });
175
176 like($out, qr!no base tokenization!s, $call);
177};
178
Akronf6240842017-02-17 23:45:26 +0100179my $input_quotes = catfile($f, '..', 'corpus', 'archive_quotes.zip');
180$call = join(
181 ' ',
182 'perl', $script,
183 'archive',
184 '--input' => $input_quotes,
185 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +0200186 '--cache' => $cache,
Akronf6240842017-02-17 23:45:26 +0100187 '-t' => 'Base#tokens_aggr'
188);
189
190# Test without parameters
191stdout_like(
192 sub {
193 system($call);
194 },
195 qr!Done\.!is,
196 $call
197);
198
Akron89df4fa2016-11-04 14:35:37 +0100199
Akron3ec48972016-08-17 23:24:52 +0200200unlink($output);
201
Akron821db3d2017-04-06 21:19:31 +0200202
203$input_quotes = "'".catfile($f, '..', 'corpus', 'archives', 'wpd15*.zip') . "'";
204
205$call = join(
206 ' ',
207 'perl', $script,
208 'archive',
209 '--input' => $input_quotes,
210 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +0200211 '--cache' => $cache,
Akron821db3d2017-04-06 21:19:31 +0200212 '-t' => 'Base#tokens_aggr'
213);
214
215# Test without parameters
216stdout_like(
217 sub {
218 system($call);
219 },
Akron63f20d42017-04-10 23:40:29 +0200220 qr!Input is .+?wpd15-single\.zip,.+?wpd15-single\.malt\.zip,.+?wpd15-single\.corenlp\.zip,.+?wpd15-single\.opennlp\.zip,.+?wpd15-single\.mdparser\.zip,.+?wpd15-single\.tree_tagger\.zip!is,
Akron821db3d2017-04-06 21:19:31 +0200221 $call
222);
223
Akron7d4cdd82016-08-17 21:39:45 +0200224done_testing;
225__END__