Akron | 7d4cdd8 | 2016-08-17 21:39:45 +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 | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 8 | use Mojo::Util qw/quote/; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 9 | use Mojo::JSON qw/decode_json/; |
| 10 | use IO::Uncompress::Gunzip; |
| 11 | use Test::More; |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 12 | use Test::Output qw/:stdout :stderr :combined :functions/; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 13 | use Data::Dumper; |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 14 | use KorAP::XML::Archive; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 15 | use utf8; |
| 16 | |
| 17 | my $f = dirname(__FILE__); |
| 18 | my $script = catfile($f, '..', '..', 'script', 'korapxml2krill'); |
| 19 | |
| 20 | my $call = join( |
| 21 | ' ', |
| 22 | 'perl', $script, |
| 23 | 'archive' |
| 24 | ); |
| 25 | |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 26 | unless (KorAP::XML::Archive::test_unzip) { |
| 27 | plan skip_all => 'unzip not found'; |
| 28 | }; |
| 29 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 30 | # Test without parameters |
| 31 | stdout_like( |
| 32 | sub { |
| 33 | system($call); |
| 34 | }, |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 35 | qr!archive.+?\$ korapxml2krill!s, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 36 | $call |
| 37 | ); |
| 38 | |
| 39 | my $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 40 | ok(-f $input, 'Input archive found'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 41 | my $output = File::Temp->newdir(CLEANUP => 0); |
| 42 | $output->unlink_on_destroy(0); |
| 43 | |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 44 | my $cache = tmpnam(); |
| 45 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 46 | ok(-d $output, 'Output directory exists'); |
| 47 | |
| 48 | $call = join( |
| 49 | ' ', |
| 50 | 'perl', $script, |
| 51 | 'archive', |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 52 | '--input' => '' . $input, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 53 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 54 | '--cache' => $cache, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 55 | '-t' => 'Base#tokens_aggr', |
| 56 | '-m' => 'Sgbr' |
| 57 | ); |
| 58 | |
| 59 | # Test without compression |
| 60 | my $json; |
| 61 | { |
| 62 | local $SIG{__WARN__} = sub {}; |
| 63 | my $out = stdout_from(sub { system($call); }); |
| 64 | |
| 65 | like($out, qr!TEST-BSP-1\.json!s, $call); |
| 66 | |
| 67 | $out =~ m!Processed (.+?\.json)!; |
| 68 | $json = $1; |
| 69 | }; |
| 70 | |
| 71 | ok(-f $json, 'Json file exists'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 72 | ok((my $file = Mojo::File->new($json)->slurp), 'Slurp data'); |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 73 | ok(($json = decode_json $file), 'decode json'); |
| 74 | |
| 75 | is($json->{data}->{tokenSource}, 'base#tokens_aggr', 'Title'); |
| 76 | is($json->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure sgbr sgbr/lemma sgbr/morpho', 'Foundries'); |
| 77 | is($json->{sgbrKodex}, 'M', 'Kodex meta data'); |
| 78 | |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 79 | |
| 80 | # Use directory |
| 81 | $input = catdir($f, '..', 'annotation', 'corpus'); |
| 82 | |
| 83 | $call = join( |
| 84 | ' ', |
| 85 | 'perl', $script, |
| 86 | 'archive', |
| 87 | '--input' => $input, |
| 88 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 89 | '--cache' => $cache, |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 90 | '-t' => 'Tree_Tagger#Tokens', |
| 91 | '-j' => 4 # 4 jobs! |
| 92 | ); |
| 93 | |
| 94 | my ($json_1, $json_2); |
| 95 | |
| 96 | { |
| 97 | local $SIG{__WARN__} = sub {}; |
| 98 | |
| 99 | # That's not really stable on slow machines! |
| 100 | my $out = stdout_from(sub { system($call); }); |
| 101 | |
| 102 | ok($out =~ m!\[\$(\d+?):1\/2\]!s, $call . ' pid 1'); |
| 103 | my $pid1 = $1; |
| 104 | ok($out =~ m!\[\$(\d+?):2\/2\]!s, $call . ' pid 2'); |
| 105 | my $pid2 = $1; |
| 106 | |
| 107 | isnt($pid1, $pid2, 'No PID match'); |
| 108 | |
| 109 | ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call); |
| 110 | ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call); |
| 111 | |
| 112 | ok(-d $output, 'Temporary directory still exists'); |
| 113 | my $json_1 = catfile($output, 'corpus-doc-0001.json'); |
| 114 | ok(-f $json_1, 'Json file exists 1'); |
| 115 | my $json_2 = catfile($output, 'corpus-doc-0002.json'); |
| 116 | ok(-f $json_2, 'Json file exists 2'); |
| 117 | |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 118 | ok(($file = Mojo::File->new($json_1)->slurp), 'Slurp data'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 119 | ok(($json_1 = decode_json $file), 'decode json'); |
| 120 | |
| 121 | is($json_1->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource'); |
| 122 | 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'); |
| 123 | is($json_1->{textSigle}, 'Corpus/Doc/0001', 'Sigle'); |
| 124 | |
| 125 | ok(-f $json_2, 'Json file exists'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 126 | ok(($file = Mojo::File->new($json_2)->slurp), 'Slurp data'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 127 | ok(($json_2 = decode_json $file), 'decode json'); |
| 128 | |
| 129 | is($json_2->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource'); |
| 130 | is($json_2->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure malt malt/dependency treetagger treetagger/morpho treetagger/sentences', 'Foundries'); |
| 131 | is($json_2->{textSigle}, 'Corpus/Doc/0002', 'Sigle'); |
| 132 | }; |
| 133 | |
| 134 | ok(-d $output, 'Ouput directory exists'); |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 135 | |
| 136 | |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 137 | my $temp_extract = tmpnam(); |
| 138 | |
| 139 | # Ignore -te when archive is a directory |
| 140 | $call = join( |
| 141 | ' ', |
| 142 | 'perl', $script, |
| 143 | 'archive', |
| 144 | '--input' => $input, |
| 145 | '--output' => $output, |
| 146 | '--cache' => $cache, |
| 147 | '-t' => 'Tree_Tagger#Tokens', |
| 148 | '-j' => 4, # 4 jobs! |
| 149 | '-te' => $temp_extract |
| 150 | ); |
| 151 | |
| 152 | { |
| 153 | local $SIG{__WARN__} = sub {}; |
| 154 | |
| 155 | my $out = combined_from(sub { system($call); }); |
| 156 | |
| 157 | ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call); |
| 158 | ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call); |
| 159 | }; |
| 160 | |
| 161 | |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 162 | $input = catfile($f, '..', 'corpus', 'WDD15', 'A79', '83946'); |
| 163 | $call = join( |
| 164 | ' ', |
| 165 | 'perl', $script, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 166 | '--input' => $input, |
| 167 | '--cache' => $cache |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 168 | ); |
| 169 | |
| 170 | # Test without compression |
| 171 | { |
| 172 | local $SIG{__WARN__} = sub {}; |
| 173 | my $out = stderr_from(sub { system($call); }); |
| 174 | |
| 175 | like($out, qr!no base tokenization!s, $call); |
| 176 | }; |
| 177 | |
Akron | f624084 | 2017-02-17 23:45:26 +0100 | [diff] [blame] | 178 | my $input_quotes = catfile($f, '..', 'corpus', 'archive_quotes.zip'); |
| 179 | $call = join( |
| 180 | ' ', |
| 181 | 'perl', $script, |
| 182 | 'archive', |
| 183 | '--input' => $input_quotes, |
| 184 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 185 | '--cache' => $cache, |
Akron | f624084 | 2017-02-17 23:45:26 +0100 | [diff] [blame] | 186 | '-t' => 'Base#tokens_aggr' |
| 187 | ); |
| 188 | |
| 189 | # Test without parameters |
| 190 | stdout_like( |
| 191 | sub { |
| 192 | system($call); |
| 193 | }, |
| 194 | qr!Done\.!is, |
| 195 | $call |
| 196 | ); |
| 197 | |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 198 | |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 199 | unlink($output); |
| 200 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 201 | |
| 202 | $input_quotes = "'".catfile($f, '..', 'corpus', 'archives', 'wpd15*.zip') . "'"; |
| 203 | |
| 204 | $call = join( |
| 205 | ' ', |
| 206 | 'perl', $script, |
| 207 | 'archive', |
| 208 | '--input' => $input_quotes, |
| 209 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 210 | '--cache' => $cache, |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 211 | '-t' => 'Base#tokens_aggr' |
| 212 | ); |
| 213 | |
| 214 | # Test without parameters |
| 215 | stdout_like( |
| 216 | sub { |
| 217 | system($call); |
| 218 | }, |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 219 | 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, |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 220 | $call |
| 221 | ); |
| 222 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 223 | |
| 224 | |
| 225 | # Test with sigles |
| 226 | $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 227 | ok(-f $input, 'Input archive found'); |
| 228 | |
| 229 | unlink($output); |
| 230 | |
| 231 | $call = join( |
| 232 | ' ', |
| 233 | 'perl', $script, |
| 234 | 'archive', |
| 235 | '--input' => '' . $input, |
| 236 | '--output' => $output, |
| 237 | '--sigle' => 'TEST/BSP/2', |
| 238 | '--sigle' => 'TEST/BSP/5', |
| 239 | '-t' => 'Base#tokens_aggr', |
| 240 | '-m' => 'Sgbr' |
| 241 | ); |
| 242 | |
| 243 | { |
| 244 | local $SIG{__WARN__} = sub {}; |
| 245 | my $out = stdout_from(sub { system($call); }); |
| 246 | |
| 247 | like($out, qr!TEST-BSP-1\.json!s, $call); |
| 248 | |
| 249 | $out =~ m!Processed (.+?\.json)!; |
| 250 | $json = $1; |
| 251 | }; |
| 252 | |
| 253 | ok(-f $json, 'Json file exists'); |
| 254 | |
| 255 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 256 | done_testing; |
| 257 | __END__ |