blob: 648bf2032fa0d63370e415bf35d275b32ec3ad48 [file] [log] [blame]
Marc Kupietzbb739b02020-09-22 16:49:34 +02001use strict;
2use warnings;
Marc Kupietz97ba2ba2021-10-11 17:55:47 +02003use Test::More tests => 33;
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
37for my $base_fname (glob("t/data/*\.zip")) {
Marc Kupietzd8455832021-02-11 17:30:29 +010038 my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.conllu/r;
Marc Kupietz628893e2021-02-12 15:50:29 +010039 next if (!-e $conllu_fname);
Marc Kupietzd8455832021-02-11 17:30:29 +010040
41 my $expected;
Akron46f2c232021-02-12 17:21:39 +010042 if (open(my $fh, '<', $conllu_fname)) {
Marc Kupietzd8455832021-02-11 17:30:29 +010043 local $/;
44 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +010045 close($fh);
46 } else {
47 fail("cannot open file $conllu_fname");
48 next;
Marc Kupietzd8455832021-02-11 17:30:29 +010049 }
Marc Kupietz13994d12021-02-12 17:25:36 +010050 script_runs([ 'script/korapxml2conllu', $base_fname ], "Runs korapxml2conllu with base input");
51 script_stdout_is $expected, "Converts $base_fname correctly to CoNLL-U";
Marc Kupietzd8455832021-02-11 17:30:29 +010052}
53
Marc Kupietz79ba1e52021-02-12 17:26:54 +010054my $expected;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +020055if (open(my $fh, '<', 't/data/goe.1c.txt')) {
56 local $/;
57 $expected = <$fh>;
58 close($fh);
59} else {
60 fail("cannot open file.");
61}
62script_runs([ 'script/korapxml2conllu', '-c', '1', 't/data/goe.zip' ], "Runs korapxml2conllu in one column mode");
63script_stdout_is $expected, "Converts correctly in one column mode.";
64
65my $test_tempdir = tempdir();
Akron46f2c232021-02-12 17:21:39 +010066my $conllu_fname = "t/data/goe.morpho.conllu";
67if(open(my $fh, '<', $conllu_fname )) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +010068 local $/;
69 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +010070 close($fh);
71} else {
72 fail("cannot open file $conllu_fname");
73 }
74
75ok(length($expected) > 100, 'Output is not empty');
Marc Kupietz79ba1e52021-02-12 17:26:54 +010076
77my $zipfile = "$test_tempdir/goe.tree_tagger.zip";
78my $zipcontent;
79script_runs([ 'script/conllu2korapxml', "t/data/goe.morpho.conllu" ], {stdout => \$zipcontent},
80 "Converts t/data/goe.morpho.conllu to KorAP-XML zip");
81open(my $fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
82print $fh $zipcontent;
83close($fh);
Marc Kupietz79ba1e52021-02-12 17:26:54 +010084copy("t/data/goe.zip", $test_tempdir);
85script_runs([ 'script/korapxml2conllu', "$test_tempdir/goe.tree_tagger.zip" ],
86 "Converts $test_tempdir/goe.tree_tagger.zip to CoNLL-U");
87script_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 +010088
89script_runs([ 'script/korapxml2conllu', '-e', 'div/type', "t/data/goe.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and attribute extraction");
90script_stdout_like "\n# div/type = Autobiographie\n", "Extracts attributes from morpho zips";
91script_stdout_like "\n# div/type = section\n", "Extracts attributes from morpho zips";
92
93script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.zip" ], "Runs korapxml2conllu with base input and regex attribute extraction");
94script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from base zips (1)";
95script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from base zips (2)";
96script_stdout_like "\n# posting/id = i.14548_9_1\n3\tbonjour", "Extracts attributes in the right place";
97script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from base zips (1)";
98script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from base zips (2)";
Marc Kupietzab150232021-07-31 23:41:47 +020099script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in base zip";
100
101script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and regex attribute extraction");
102script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from morpho zips (1)";
103script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from morpho zips (2)";
104script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from morpho zips (1)";
105script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from morpho zips (2)";
106script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in morpho zip";
107
Marc Kupietz97ba2ba2021-10-11 17:55:47 +0200108$zipfile = "$test_tempdir/without_lemma.zip";
109script_runs([ 'script/conllu2korapxml', "t/data/without_lemma.tsv" ], {stdout => \$zipcontent},
110 "Converts t/data/without_lemma.tsv to KorAP-XML zip");
111open($fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
112print $fh $zipcontent;
113close($fh);
114my $UNZIP = `sh -c 'command -v unzip'`;
115chomp $UNZIP;
116
117if ($UNZIP eq '') {
118 warn('No unzip executable found in PATH.');
119 return 0;
120};
121$zipcontent = `$UNZIP -c $zipfile`;
122unlike($zipcontent, qr/.*name ="lemma".*/, "conllu2korapxml igores _ lemmas.");
123like($zipcontent, qr/.*<f name="pos">NN|NN<\/f>.*/, "conllu2korapxml does not ignore pos for _ lemmas.");
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100124done_testing;