blob: 41389bb4661a6e56fbaafcf327bbe8265d85d63e [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
Akronfab17d32020-07-31 14:38:29 +020017if ($ENV{SKIP_SCRIPT}) {
18 plan skip_all => 'Skip script tests';
19};
20
Akron7d4cdd82016-08-17 21:39:45 +020021my $f = dirname(__FILE__);
22my $script = catfile($f, '..', '..', 'script', 'korapxml2krill');
23
24my $call = join(
25 ' ',
26 'perl', $script,
27 'archive'
28);
29
Nils Diewaldb3e9ccd2016-10-24 15:16:52 +020030unless (KorAP::XML::Archive::test_unzip) {
31 plan skip_all => 'unzip not found';
32};
33
Akron7d4cdd82016-08-17 21:39:45 +020034# Test without parameters
35stdout_like(
36 sub {
37 system($call);
38 },
Akrona76d8352016-10-27 16:27:32 +020039 qr!archive.+?\$ korapxml2krill!s,
Akron7d4cdd82016-08-17 21:39:45 +020040 $call
41);
42
43my $input = catfile($f, '..', 'corpus', 'archive.zip');
44ok(-f $input, 'Input archive found');
Akron3ec48972016-08-17 23:24:52 +020045my $output = File::Temp->newdir(CLEANUP => 0);
46$output->unlink_on_destroy(0);
47
Akrond5bb4342017-06-19 11:50:49 +020048my $cache = tmpnam();
49
Akron7d4cdd82016-08-17 21:39:45 +020050ok(-d $output, 'Output directory exists');
51
52$call = join(
53 ' ',
54 'perl', $script,
55 'archive',
Akron821db3d2017-04-06 21:19:31 +020056 '--input' => '' . $input,
Akron7d4cdd82016-08-17 21:39:45 +020057 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +020058 '--cache' => $cache,
Akron7d4cdd82016-08-17 21:39:45 +020059 '-t' => 'Base#tokens_aggr',
60 '-m' => 'Sgbr'
61);
62
63# Test without compression
64my $json;
65{
66 local $SIG{__WARN__} = sub {};
67 my $out = stdout_from(sub { system($call); });
68
69 like($out, qr!TEST-BSP-1\.json!s, $call);
70
71 $out =~ m!Processed (.+?\.json)!;
72 $json = $1;
73};
74
75ok(-f $json, 'Json file exists');
Akron3ec0a1c2017-01-18 14:41:55 +010076ok((my $file = Mojo::File->new($json)->slurp), 'Slurp data');
Akron7d4cdd82016-08-17 21:39:45 +020077ok(($json = decode_json $file), 'decode json');
78
79is($json->{data}->{tokenSource}, 'base#tokens_aggr', 'Title');
80is($json->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure sgbr sgbr/lemma sgbr/morpho', 'Foundries');
81is($json->{sgbrKodex}, 'M', 'Kodex meta data');
82
Akron3ec48972016-08-17 23:24:52 +020083
84# Use directory
85$input = catdir($f, '..', 'annotation', 'corpus');
86
87$call = join(
88 ' ',
89 'perl', $script,
90 'archive',
91 '--input' => $input,
92 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +020093 '--cache' => $cache,
Akron3ec48972016-08-17 23:24:52 +020094 '-t' => 'Tree_Tagger#Tokens',
95 '-j' => 4 # 4 jobs!
96);
97
98my ($json_1, $json_2);
99
100{
101 local $SIG{__WARN__} = sub {};
102
103 # That's not really stable on slow machines!
104 my $out = stdout_from(sub { system($call); });
105
106 ok($out =~ m!\[\$(\d+?):1\/2\]!s, $call . ' pid 1');
107 my $pid1 = $1;
108 ok($out =~ m!\[\$(\d+?):2\/2\]!s, $call . ' pid 2');
109 my $pid2 = $1;
110
111 isnt($pid1, $pid2, 'No PID match');
112
113 ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call);
114 ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call);
115
116 ok(-d $output, 'Temporary directory still exists');
117 my $json_1 = catfile($output, 'corpus-doc-0001.json');
118 ok(-f $json_1, 'Json file exists 1');
119 my $json_2 = catfile($output, 'corpus-doc-0002.json');
120 ok(-f $json_2, 'Json file exists 2');
121
Akron3ec0a1c2017-01-18 14:41:55 +0100122 ok(($file = Mojo::File->new($json_1)->slurp), 'Slurp data');
Akron3ec48972016-08-17 23:24:52 +0200123 ok(($json_1 = decode_json $file), 'decode json');
124
125 is($json_1->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource');
Marc Kupietzb8c53822024-03-16 18:54:08 +0100126 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 spacy spacy/morpho treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries');
Akron3ec48972016-08-17 23:24:52 +0200127 is($json_1->{textSigle}, 'Corpus/Doc/0001', 'Sigle');
128
129 ok(-f $json_2, 'Json file exists');
Akron3ec0a1c2017-01-18 14:41:55 +0100130 ok(($file = Mojo::File->new($json_2)->slurp), 'Slurp data');
Akron3ec48972016-08-17 23:24:52 +0200131 ok(($json_2 = decode_json $file), 'decode json');
132
133 is($json_2->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource');
134 is($json_2->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure malt malt/dependency treetagger treetagger/morpho treetagger/sentences', 'Foundries');
135 is($json_2->{textSigle}, 'Corpus/Doc/0002', 'Sigle');
136};
137
138ok(-d $output, 'Ouput directory exists');
Akron89df4fa2016-11-04 14:35:37 +0100139
140
Akron63d03ee2019-02-13 18:49:38 +0100141my $temp_extract = tmpnam();
142
143# Ignore -te when archive is a directory
144$call = join(
145 ' ',
146 'perl', $script,
147 'archive',
148 '--input' => $input,
149 '--output' => $output,
150 '--cache' => $cache,
151 '-t' => 'Tree_Tagger#Tokens',
152 '-j' => 4, # 4 jobs!
153 '-te' => $temp_extract
154);
155
156{
157 local $SIG{__WARN__} = sub {};
158
159 my $out = combined_from(sub { system($call); });
160
161 ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call);
162 ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call);
163};
164
165
Akronf6240842017-02-17 23:45:26 +0100166my $input_quotes = catfile($f, '..', 'corpus', 'archive_quotes.zip');
167$call = join(
168 ' ',
169 'perl', $script,
170 'archive',
171 '--input' => $input_quotes,
172 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +0200173 '--cache' => $cache,
Akronf6240842017-02-17 23:45:26 +0100174 '-t' => 'Base#tokens_aggr'
175);
176
177# Test without parameters
178stdout_like(
179 sub {
180 system($call);
181 },
182 qr!Done\.!is,
183 $call
184);
185
Akron89df4fa2016-11-04 14:35:37 +0100186
Akron3ec48972016-08-17 23:24:52 +0200187unlink($output);
188
Akron821db3d2017-04-06 21:19:31 +0200189$input_quotes = "'".catfile($f, '..', 'corpus', 'archives', 'wpd15*.zip') . "'";
190
191$call = join(
192 ' ',
193 'perl', $script,
194 'archive',
195 '--input' => $input_quotes,
196 '--output' => $output,
Akrond5bb4342017-06-19 11:50:49 +0200197 '--cache' => $cache,
Akron821db3d2017-04-06 21:19:31 +0200198 '-t' => 'Base#tokens_aggr'
199);
200
201# Test without parameters
202stdout_like(
203 sub {
204 system($call);
205 },
Akron63f20d42017-04-10 23:40:29 +0200206 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 +0200207 $call
208);
209
Akron31a08cb2019-02-20 20:43:26 +0100210
Akron31a08cb2019-02-20 20:43:26 +0100211# Test with sigles
212$input = catfile($f, '..', 'corpus', 'archive.zip');
213ok(-f $input, 'Input archive found');
214
215unlink($output);
216
217$call = join(
218 ' ',
219 'perl', $script,
220 'archive',
221 '--input' => '' . $input,
222 '--output' => $output,
223 '--sigle' => 'TEST/BSP/2',
224 '--sigle' => 'TEST/BSP/5',
225 '-t' => 'Base#tokens_aggr',
226 '-m' => 'Sgbr'
227);
228
229{
230 local $SIG{__WARN__} = sub {};
231 my $out = stdout_from(sub { system($call); });
232
233 like($out, qr!TEST-BSP-1\.json!s, $call);
234
235 $out =~ m!Processed (.+?\.json)!;
236 $json = $1;
237};
238
239ok(-f $json, 'Json file exists');
240
Akrona3518372024-01-22 23:29:00 +0100241# Test quiet
242
243# my $input = catfile($f, '..', 'corpus', 'archive.zip');
244# ok(-f $input, 'Input archive found');
245
246$call = join(
247 ' ',
248 'perl', $script,
249 'archive',
250 '--input' => '' . $input,
251 '--quiet',
252 '--output' => $output,
253 '--sigle' => 'TEST/BSP/2',
254 '--sigle' => 'TEST/BSP/5',
255 '-t' => 'Base#tokens_aggr',
256 '-m' => 'Sgbr'
257);
258
259{
260 local $SIG{__WARN__} = sub {};
261 my $out = stdout_from(sub { system($call); });
262
263 is($out, "\n", $call);
264};
Akron31a08cb2019-02-20 20:43:26 +0100265
Akron7d4cdd82016-08-17 21:39:45 +0200266done_testing;
267__END__