Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame^] | 3 | use Test::More tests => 37; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 4 | use Test::Script; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 5 | use Test::TempDir::Tiny; |
| 6 | use File::Copy; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 7 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 8 | script_runs([ 'script/korapxml2conllu', '-h' ], { exit => 1 }); |
| 9 | script_stdout_like "Description", "Can print help message"; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 10 | |
| 11 | for my $morpho_fname (glob("t/data/*\.*\.zip")) { |
| 12 | my $base_fname = $morpho_fname =~ s/(.*)\..*\.zip/$1.zip/r; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 13 | if (!-e $base_fname) { |
| 14 | fail("cannot find $base_fname"); |
| 15 | next; |
| 16 | }; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 17 | |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 18 | my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.morpho.conllu/r; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 19 | if (!-e $conllu_fname) { |
| 20 | fail("cannot find $conllu_fname"); |
| 21 | next; |
| 22 | }; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 23 | |
| 24 | my $expected; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 25 | if (open(my $fh, '<', $conllu_fname)) { |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 26 | local $/; |
| 27 | $expected = <$fh>; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 28 | close($fh); |
| 29 | } else { |
| 30 | fail("cannot open file $conllu_fname"); |
| 31 | next; |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 32 | } |
Marc Kupietz | 13994d1 | 2021-02-12 17:25:36 +0100 | [diff] [blame] | 33 | script_runs([ 'script/korapxml2conllu', $morpho_fname ], "Runs korapxml2conllu with pos and lemma annotated input"); |
Marc Kupietz | bb739b0 | 2020-09-22 16:49:34 +0200 | [diff] [blame] | 34 | script_stdout_is $expected, "Converts $morpho_fname correctly"; |
| 35 | } |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 36 | |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame^] | 37 | for 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 Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 63 | for my $base_fname (glob("t/data/*\.zip")) { |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 64 | my $conllu_fname = $base_fname =~ s/(.*)\.zip/$1.conllu/r; |
Marc Kupietz | 628893e | 2021-02-12 15:50:29 +0100 | [diff] [blame] | 65 | next if (!-e $conllu_fname); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 66 | |
| 67 | my $expected; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 68 | if (open(my $fh, '<', $conllu_fname)) { |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 69 | local $/; |
| 70 | $expected = <$fh>; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 71 | close($fh); |
| 72 | } else { |
| 73 | fail("cannot open file $conllu_fname"); |
| 74 | next; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 75 | } |
Marc Kupietz | 13994d1 | 2021-02-12 17:25:36 +0100 | [diff] [blame] | 76 | script_runs([ 'script/korapxml2conllu', $base_fname ], "Runs korapxml2conllu with base input"); |
| 77 | script_stdout_is $expected, "Converts $base_fname correctly to CoNLL-U"; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 80 | my $expected; |
Marc Kupietz | d7d5d6a | 2021-10-11 17:52:58 +0200 | [diff] [blame] | 81 | if (open(my $fh, '<', 't/data/goe.1c.txt')) { |
| 82 | local $/; |
| 83 | $expected = <$fh>; |
| 84 | close($fh); |
| 85 | } else { |
| 86 | fail("cannot open file."); |
| 87 | } |
| 88 | script_runs([ 'script/korapxml2conllu', '-c', '1', 't/data/goe.zip' ], "Runs korapxml2conllu in one column mode"); |
| 89 | script_stdout_is $expected, "Converts correctly in one column mode."; |
| 90 | |
| 91 | my $test_tempdir = tempdir(); |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 92 | my $conllu_fname = "t/data/goe.morpho.conllu"; |
| 93 | if(open(my $fh, '<', $conllu_fname )) { |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 94 | local $/; |
| 95 | $expected = <$fh>; |
Akron | 46f2c23 | 2021-02-12 17:21:39 +0100 | [diff] [blame] | 96 | close($fh); |
| 97 | } else { |
| 98 | fail("cannot open file $conllu_fname"); |
| 99 | } |
| 100 | |
| 101 | ok(length($expected) > 100, 'Output is not empty'); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 102 | |
| 103 | my $zipfile = "$test_tempdir/goe.tree_tagger.zip"; |
| 104 | my $zipcontent; |
| 105 | script_runs([ 'script/conllu2korapxml', "t/data/goe.morpho.conllu" ], {stdout => \$zipcontent}, |
| 106 | "Converts t/data/goe.morpho.conllu to KorAP-XML zip"); |
| 107 | open(my $fh, ">", $zipfile) or fail("cannot open file $zipfile for writing"); |
| 108 | print $fh $zipcontent; |
| 109 | close($fh); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 110 | copy("t/data/goe.zip", $test_tempdir); |
| 111 | script_runs([ 'script/korapxml2conllu', "$test_tempdir/goe.tree_tagger.zip" ], |
| 112 | "Converts $test_tempdir/goe.tree_tagger.zip to CoNLL-U"); |
| 113 | script_stdout_is $expected, "Full round trip: Converts goe.morpho.conllu to KorAP-XML and back to CoNLL-U correctly"; |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 114 | |
| 115 | script_runs([ 'script/korapxml2conllu', '-e', 'div/type', "t/data/goe.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and attribute extraction"); |
| 116 | script_stdout_like "\n# div/type = Autobiographie\n", "Extracts attributes from morpho zips"; |
| 117 | script_stdout_like "\n# div/type = section\n", "Extracts attributes from morpho zips"; |
| 118 | |
| 119 | script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.zip" ], "Runs korapxml2conllu with base input and regex attribute extraction"); |
| 120 | script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from base zips (1)"; |
| 121 | script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from base zips (2)"; |
| 122 | script_stdout_like "\n# posting/id = i.14548_9_1\n3\tbonjour", "Extracts attributes in the right place"; |
| 123 | script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from base zips (1)"; |
| 124 | script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from base zips (2)"; |
Marc Kupietz | ab15023 | 2021-07-31 23:41:47 +0200 | [diff] [blame] | 125 | script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in base zip"; |
| 126 | |
| 127 | script_runs([ 'script/korapxml2conllu', '-e', '(posting/id|div/id)', "t/data/wdf19.tree_tagger.zip" ], "Runs korapxml2conllu with morpho input and regex attribute extraction"); |
| 128 | script_stdout_like "\n# posting/id = i.13075_11_45", "Extracts multiple attributes from morpho zips (1)"; |
| 129 | script_stdout_like "\n# div/id = i.13075_14", "Extracts multiple attributes from morpho zips (2)"; |
| 130 | script_stdout_like "\n# posting/id = i.12610_4_4", "Extracts directly adjacent postings from morpho zips (1)"; |
| 131 | script_stdout_like "\n# posting/id = i.12610_4_5", "Extracts directly adjacent postings from morpho zips (2)"; |
| 132 | script_stdout_like "\n# posting/id = i.14548_9_1", "Extracts last postings in morpho zip"; |
| 133 | |
Marc Kupietz | 97ba2ba | 2021-10-11 17:55:47 +0200 | [diff] [blame] | 134 | $zipfile = "$test_tempdir/without_lemma.zip"; |
| 135 | script_runs([ 'script/conllu2korapxml', "t/data/without_lemma.tsv" ], {stdout => \$zipcontent}, |
| 136 | "Converts t/data/without_lemma.tsv to KorAP-XML zip"); |
| 137 | open($fh, ">", $zipfile) or fail("cannot open file $zipfile for writing"); |
| 138 | print $fh $zipcontent; |
| 139 | close($fh); |
| 140 | my $UNZIP = `sh -c 'command -v unzip'`; |
| 141 | chomp $UNZIP; |
| 142 | |
| 143 | if ($UNZIP eq '') { |
| 144 | warn('No unzip executable found in PATH.'); |
| 145 | return 0; |
| 146 | }; |
| 147 | $zipcontent = `$UNZIP -c $zipfile`; |
| 148 | unlike($zipcontent, qr/.*name ="lemma".*/, "conllu2korapxml igores _ lemmas."); |
| 149 | like($zipcontent, qr/.*<f name="pos">NN|NN<\/f>.*/, "conllu2korapxml does not ignore pos for _ lemmas."); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 150 | done_testing; |