blob: 48bd2b118d3eaea7947dff05e253627bde901d10 [file] [log] [blame]
Marc Kupietzbb739b02020-09-22 16:49:34 +02001use strict;
2use warnings;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +01003use Test::More tests => 19;
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 $test_tempdir = tempdir();
55my $expected;
Akron46f2c232021-02-12 17:21:39 +010056my $conllu_fname = "t/data/goe.morpho.conllu";
57if(open(my $fh, '<', $conllu_fname )) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +010058 local $/;
59 $expected = <$fh>;
Akron46f2c232021-02-12 17:21:39 +010060 close($fh);
61} else {
62 fail("cannot open file $conllu_fname");
63 }
64
65ok(length($expected) > 100, 'Output is not empty');
Marc Kupietz79ba1e52021-02-12 17:26:54 +010066
67my $zipfile = "$test_tempdir/goe.tree_tagger.zip";
68my $zipcontent;
69script_runs([ 'script/conllu2korapxml', "t/data/goe.morpho.conllu" ], {stdout => \$zipcontent},
70 "Converts t/data/goe.morpho.conllu to KorAP-XML zip");
71open(my $fh, ">", $zipfile) or fail("cannot open file $zipfile for writing");
72print $fh $zipcontent;
73close($fh);
Marc Kupietz79ba1e52021-02-12 17:26:54 +010074copy("t/data/goe.zip", $test_tempdir);
75script_runs([ 'script/korapxml2conllu', "$test_tempdir/goe.tree_tagger.zip" ],
76 "Converts $test_tempdir/goe.tree_tagger.zip to CoNLL-U");
77script_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 +010078
79script_runs([ 'script/korapxml2conllu', '-e', 'div/type', "t/data/goe.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and attribute extraction");
80script_stdout_like "\n# div/type = Autobiographie\n", "Extracts attributes from morpho zips";
81script_stdout_like "\n# div/type = section\n", "Extracts attributes from morpho zips";
82
83script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.zip" ], "Runs korapxml2conllu with base input and regex attribute extraction");
84script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from base zips (1)";
85script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from base zips (2)";
86script_stdout_like "\n# posting/id = i.14548_9_1\n3\tbonjour", "Extracts attributes in the right place";
87script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from base zips (1)";
88script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from base zips (2)";
Marc Kupietz79ba1e52021-02-12 17:26:54 +010089done_testing;