blob: 52ac9d0fdf44234f241465223eb76d3c773a5e1a [file] [log] [blame]
Marc Kupietzbb739b02020-09-22 16:49:34 +02001use strict;
2use warnings;
Akronf2b0bba2022-12-16 18:00:08 +01003use Test::More tests => 61;
Marc Kupietzbb739b02020-09-22 16:49:34 +02004use Test::Script;
Marc Kupietz79ba1e52021-02-12 17:26:54 +01005use Test::TempDir::Tiny;
6use File::Copy;
Marc Kupietzbb739b02020-09-22 16:49:34 +02007
Marc Kupietz6a79cad2021-03-19 16:26:58 +01008script_runs([ 'script/korapxml2conllu', '-h' ], { exit => 1 });
9script_stdout_like "Description", "Can print help message";
Marc Kupietzbb739b02020-09-22 16:49:34 +020010
11for my $morpho_fname (glob("t/data/*\.*\.zip")) {
12 my $base_fname = $morpho_fname =~ s/(.*)\..*\.zip/$1.zip/r;
Akron46f2c232021-02-12 17:21:39 +010013 if (!-e $base_fname) {
14 fail("cannot find $base_fname");
15 next;
16 };
Marc Kupietzbb739b02020-09-22 16:49:34 +020017
Marc Kupietzd8455832021-02-11 17:30:29 +010018 my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.morpho.conllu/r;
Akron46f2c232021-02-12 17:21:39 +010019 if (!-e $conllu_fname) {
20 fail("cannot find $conllu_fname");
21 next;
22 };
Marc Kupietzbb739b02020-09-22 16:49:34 +020023
24 my $expected;
Akron46f2c232021-02-12 17:21:39 +010025 if (open(my $fh, '<', $conllu_fname)) {
Marc Kupietzbb739b02020-09-22 16:49:34 +020026 local $/;
27 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +010028 close($fh);
29 } else {
30 fail("cannot open file $conllu_fname");
31 next;
Marc Kupietzbb739b02020-09-22 16:49:34 +020032 }
Marc Kupietz13994d12021-02-12 17:25:36 +010033 script_runs([ 'script/korapxml2conllu', $morpho_fname ], "Runs korapxml2conllu with pos and lemma annotated input");
Marc Kupietzbb739b02020-09-22 16:49:34 +020034 script_stdout_is $expected, "Converts $morpho_fname correctly";
35}
Marc Kupietzd8455832021-02-11 17:30:29 +010036
Marc Kupietz15c84fd2021-10-12 12:20:27 +020037for my $morpho_fname (glob("t/data/*\.*\.zip")) {
38 my $base_fname = $morpho_fname =~ s/(.*)\..*\.zip/$1.zip/r;
39 if (!-e $base_fname) {
40 fail("cannot find $base_fname");
41 next;
42 };
43
44 my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.morpho.sbfm.conllu/r;
45 if (!-e $conllu_fname) {
46 fail("cannot find $conllu_fname");
47 next;
48 };
49
50 my $expected;
51 if (open(my $fh, '<', $conllu_fname)) {
52 local $/;
53 $expected = <$fh>;
54 close($fh);
55 } else {
56 fail("cannot open file $conllu_fname");
57 next;
58 }
59 script_runs([ 'script/korapxml2conllu', '--s-bounds-from-morpho', $morpho_fname ], "Runs korapxml2conllu with --s-bounds-from-morpho correctly");
60 script_stdout_is $expected, "Converts $morpho_fname correctly";
61}
62
Marc Kupietzd8455832021-02-11 17:30:29 +010063for my $base_fname (glob("t/data/*\.zip")) {
Marc Kupietzd8455832021-02-11 17:30:29 +010064 my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.conllu/r;
Marc Kupietz628893e2021-02-12 15:50:29 +010065 next if (!-e $conllu_fname);
Marc Kupietzd8455832021-02-11 17:30:29 +010066
67 my $expected;
Akron46f2c232021-02-12 17:21:39 +010068 if (open(my $fh, '<', $conllu_fname)) {
Marc Kupietzd8455832021-02-11 17:30:29 +010069 local $/;
70 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +010071 close($fh);
72 } else {
73 fail("cannot open file $conllu_fname");
74 next;
Marc Kupietzd8455832021-02-11 17:30:29 +010075 }
Marc Kupietz13994d12021-02-12 17:25:36 +010076 script_runs([ 'script/korapxml2conllu', $base_fname ], "Runs korapxml2conllu with base input");
77 script_stdout_is $expected, "Converts $base_fname correctly to CoNLL-U";
Marc Kupietzd8455832021-02-11 17:30:29 +010078}
79
Marc Kupietzd0bf2772022-06-26 19:27:58 +020080for my $w2v_fname (glob("t/data/*\.w2v_simple")) {
81 my $base_fname = $w2v_fname =~ s/(.*)\.w2v_simple/$1.zip/r;
82 next if (!-e $base_fname);
83
84 my $expected;
85 if (open(my $fh, '<', $w2v_fname)) {
86 local $/;
87 $expected = <$fh>;
88 close($fh);
89 } else {
90 fail("cannot open file $w2v_fname");
91 next;
92 }
93 script_runs([ 'script/korapxml2conllu', '--word2vec', $base_fname ], "Runs korapxml2conllu with base input and w2v output");
94 script_stdout_is $expected, "Converts $base_fname correctly to word2vec input format";
95}
96
97for my $w2v_fname (glob("t/data/*\.w2v")) {
98 my $base_fname = $w2v_fname =~ s/(.*)\.w2v/$1.zip/r;
99 next if (!-e $base_fname);
100
101 my $expected;
102 if (open(my $fh, '<', $w2v_fname)) {
103 local $/;
104 $expected = <$fh>;
105 close($fh);
106 } else {
107 fail("cannot open file $w2v_fname");
108 next;
109 }
Marc Kupietz07a88952022-07-01 08:55:53 +0200110 script_runs([ 'script/korapxml2conllu', '-m', '<textSigle>([^<.]+)', '-m', '<creatDate>([^<]{4,7})', '--word2vec', $base_fname ], "Runs korapxml2conllu with base input and w2v and metadata output");
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200111 script_stdout_is $expected, "Converts $base_fname correctly to word2vec input format together with some metadata";
112}
113
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100114my $expected;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200115if (open(my $fh, '<', 't/data/goe.1c.txt')) {
116 local $/;
117 $expected = <$fh>;
118 close($fh);
119} else {
120 fail("cannot open file.");
121}
122script_runs([ 'script/korapxml2conllu', '-c', '1', 't/data/goe.zip' ], "Runs korapxml2conllu in one column mode");
123script_stdout_is $expected, "Converts correctly in one column mode.";
124
125my $test_tempdir = tempdir();
Akron46f2c232021-02-12 17:21:39 +0100126my $conllu_fname = "t/data/goe.morpho.conllu";
127if(open(my $fh, '<', $conllu_fname )) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100128 local $/;
129 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +0100130 close($fh);
131} else {
132 fail("cannot open file $conllu_fname");
133 }
134
135ok(length($expected) > 100, 'Output is not empty');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100136
137my $zipfile = "$test_tempdir/goe.tree_tagger.zip";
138my $zipcontent;
139script_runs([ 'script/conllu2korapxml', "t/data/goe.morpho.conllu" ], {stdout => \$zipcontent},
140 "Converts t/data/goe.morpho.conllu to KorAP-XML zip");
141open(my $fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
142print $fh $zipcontent;
143close($fh);
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100144copy("t/data/goe.zip", $test_tempdir);
145script_runs([ 'script/korapxml2conllu', "$test_tempdir/goe.tree_tagger.zip" ],
146 "Converts $test_tempdir/goe.tree_tagger.zip to CoNLL-U");
147script_stdout_is $expected, "Full round trip: Converts goe.morpho.conllu to KorAP-XML and back to CoNLL-U correctly";
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100148
149script_runs([ 'script/korapxml2conllu', '-e', 'div/type', "t/data/goe.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and attribute extraction");
150script_stdout_like "\n# div/type = Autobiographie\n", "Extracts attributes from morpho zips";
151script_stdout_like "\n# div/type = section\n", "Extracts attributes from morpho zips";
152
153script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.zip" ], "Runs korapxml2conllu with base input and regex attribute extraction");
154script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from base zips (1)";
155script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from base zips (2)";
156script_stdout_like "\n# posting/id = i.14548_9_1\n3\tbonjour", "Extracts attributes in the right place";
157script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from base zips (1)";
158script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from base zips (2)";
Marc Kupietzab150232021-07-31 23:41:47 +0200159script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in base zip";
160
161script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and regex attribute extraction");
162script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from morpho zips (1)";
163script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from morpho zips (2)";
164script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from morpho zips (1)";
165script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from morpho zips (2)";
166script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in morpho zip";
167
Marc Kupietz97ba2ba2021-10-11 17:55:47 +0200168$zipfile = "$test_tempdir/without_lemma.zip";
169script_runs([ 'script/conllu2korapxml', "t/data/without_lemma.tsv" ], {stdout => \$zipcontent},
170 "Converts t/data/without_lemma.tsv to KorAP-XML zip");
171open($fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
172print $fh $zipcontent;
173close($fh);
174my $UNZIP = `sh -c 'command -v unzip'`;
175chomp $UNZIP;
176
177if ($UNZIP eq '') {
178 warn('No unzip executable found in PATH.');
179 return 0;
180};
181$zipcontent = `$UNZIP -c $zipfile`;
182unlike($zipcontent, qr/.*name ="lemma".*/, "conllu2korapxml igores _ lemmas.");
183like($zipcontent, qr/.*<f name="pos">NN|NN<\/f>.*/, "conllu2korapxml does not ignore pos for _ lemmas.");
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200184
185script_runs([ 'script/conllu2korapxml', '-l', 'debug', 't/data/goe.ud.conllu' ], {stdout => \$zipcontent}, "Runs conllu2korap with UDPipe and unparsable comments");
186script_stderr_like "Foundry:\\s+ud", "Found generator based foundry";
187script_stderr_like "Ignored\\s+foundry\\s+name:\\s+base", "Ignore defined foundry";
188
189$zipfile = "$test_tempdir/goe.ud.zip";
190open($fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
191print $fh $zipcontent;
192close($fh);
193
194$zipcontent = `$UNZIP -l $zipfile`;
195like($zipcontent, qr@GOE/AGA/00000/ud/morpho\.xml@, "conllu2korapxml UDPipe input conversion contains morpho layer with foundry name 'ud'");
196like($zipcontent, qr@GOE/AGA/00000/ud/dependency\.xml@, "conllu2korapxml UDPipe input conversion contains dependency layer with foundry name 'ud'");
197
Akron49f333b2022-09-27 17:03:49 +0200198script_runs([ 'script/conllu2korapxml', 't/data/deu-deps.conllu' ], "Runs conllu2korap with UDPipe input");
199script_stderr_unlike "fileparse(): need a valid pathname", "Ignore sent_id and newdoc id";
200script_stderr_like "WARNING: No valid input document.*token offsets missing", "Warn on missing token offsets";
201script_stderr_like qr@WARNING: No valid input document.*text.id .*missing@, "Warn on missing text ids";
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200202
Marc Kupietz7e4cd6c2022-12-15 18:34:37 +0100203script_runs([ 'script/korapxml2conllu', "t/data/nkjp.zip" ], "Runs korapxml2conllu on nkjp test data");
204script_stderr_unlike("Use of uninitialized value", "Handles lonely docid parameters (line separated from layer elements)");
205script_stdout_like("\n9\twesołości\twesołość\tsubst\tsubst\tsg:gen:f", "Correctly converts nkjp annotations");
Marc Kupietz534df182022-12-16 15:00:30 +0100206
207script_runs([ 'script/korapxml2conllu', "--sigle-pattern", "KOT", "t/data/nkjp.zip" ], "Runs korapxml2conllu with --sigle-pattern option on combined base/morpho files");
208script_stdout_like("NKJP/NKJP/KOT/nkjp/morpho.xml", "--sigle-pattern to specify a doc sigle pattern extracts the right texts");
209script_stdout_unlike("NKJP/NKJP/KolakowskiOco/nkjp/morpho.xml", "--sigle-pattern to specify a doc sigle pattern does not extract the wrong texts");
210
211script_runs([ 'script/korapxml2conllu', "--sigle-pattern", "13072", "t/data/wdf19.tree_tagger.zip" ], "Runs korapxml2conllu with --sigle-pattern option on seprate base/morpho files");
212script_stdout_like("WDF19/A0000/13072/tree_tagger/morpho.xml", "--sigle-pattern to specify a text sigle pattern extracts the right texts");
213script_stdout_unlike("WDF19/A0000/14247/tree_tagger/morpho.xml", "--sigle-pattern to specify a text sigle pattern does not extract the wrong texts");
214
Akronf2b0bba2022-12-16 18:00:08 +0100215script_runs([ 'script/korapxml2conllu', "t/data/nkjp-fail.zip" ], "Runs korapxml2conllu on nkjp-fail test data");
216script_stderr_like("could not retrieve token at 1297-1298/ 1297 - ending with: e! upadku.", "Offset error");
217
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100218done_testing;