Akron | e1dbc38 | 2016-07-08 22:24:52 +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/; |
| 6 | use File::Temp qw/ :POSIX /; |
| 7 | use Mojo::Util qw/slurp/; |
| 8 | use Mojo::JSON qw/decode_json/; |
| 9 | use IO::Uncompress::Gunzip; |
| 10 | use Test::More; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 11 | use Test::Output; |
| 12 | use Data::Dumper; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 13 | |
| 14 | my $f = dirname(__FILE__); |
| 15 | my $script = catfile($f, '..', '..', 'script', 'korapxml2krill'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 16 | |
| 17 | my $input = catdir($f, '..', 'annotation', 'corpus', 'doc', '0001'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 18 | ok(-d $input, 'Input directory found'); |
| 19 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 20 | my $output = tmpnam(); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 21 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 22 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 23 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 24 | my $call = join( |
| 25 | ' ', |
| 26 | 'perl', $script, |
| 27 | '--input' => $input, |
| 28 | '--output' => $output, |
| 29 | '-t' => 'OpenNLP#Tokens', |
| 30 | '-l' => 'INFO' |
| 31 | ); |
| 32 | |
| 33 | # Test without compression |
| 34 | stderr_like( |
| 35 | sub { |
| 36 | system($call); |
| 37 | }, |
| 38 | qr!The code took!, |
| 39 | $call |
| 40 | ); |
| 41 | |
| 42 | ok(-f $output, 'Output does exist'); |
| 43 | ok((my $file = slurp $output), 'Slurp data'); |
| 44 | ok((my $json = decode_json $file), 'decode json'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 45 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 46 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 47 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'Title'); |
| 48 | is($json->{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'); |
| 49 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 50 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 51 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 52 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 53 | # Delete output |
| 54 | unlink $output; |
| 55 | ok(!-f $output, 'Output does not exist'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 56 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 57 | $call .= ' -z'; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 58 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 59 | # Test with compression |
| 60 | stderr_like( |
| 61 | sub { system($call); }, |
| 62 | qr!The code took!, |
| 63 | $call |
| 64 | ); |
| 65 | |
| 66 | ok(-f $output, 'Output does exist'); |
| 67 | |
| 68 | # Uncompress the data using a buffer |
| 69 | my $gz = IO::Uncompress::Gunzip->new($output, Transparent => 0); |
| 70 | ($file, my $buffer) = ''; |
| 71 | while ($gz->read($buffer)) { |
| 72 | $file .= $buffer; |
| 73 | }; |
| 74 | |
| 75 | ok($json = decode_json($file), 'decode json'); |
| 76 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 77 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 78 | is($json->{title}, 'Beispiel Text', 'Title'); |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 79 | is($json->{data}->{tokenSource}, 'opennlp#tokens', 'TokenSource'); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 80 | is($json->{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'); |
| 81 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 82 | is($json->{data}->{stream}->[0]->[0], '-:base/paragraphs$<i>1', 'Paragraphs'); |
| 83 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 84 | # Delete output |
| 85 | unlink $output; |
| 86 | ok(!-f $output, 'Output does not exist'); |
| 87 | |
| 88 | # Use a different token source and skip all annotations, |
| 89 | # except for DeReKo#Structure and Mate#Dependency |
| 90 | $call = join( |
| 91 | ' ', |
| 92 | 'perl', $script, |
| 93 | '--input' => $input, |
| 94 | '--output' => $output, |
| 95 | '-t' => 'CoreNLP#Tokens', |
| 96 | '-s' => '#all', |
| 97 | '-a' => 'DeReKo#Structure', |
| 98 | '-a' => 'Mate#Dependency', |
| 99 | '-l' => 'INFO' |
| 100 | ); |
| 101 | |
| 102 | stderr_like( |
| 103 | sub { |
| 104 | system($call); |
| 105 | }, |
| 106 | qr!The code took!, |
| 107 | $call |
| 108 | ); |
| 109 | |
| 110 | ok(-f $output, 'Output does exist'); |
| 111 | ok(($file = slurp $output), 'Slurp data'); |
| 112 | ok(($json = decode_json $file), 'decode json'); |
| 113 | |
| 114 | is($json->{textType}, 'Zeitung: Tageszeitung', 'text type'); |
| 115 | |
| 116 | is($json->{title}, 'Beispiel Text', 'Title'); |
| 117 | is($json->{data}->{tokenSource}, 'corenlp#tokens', 'TokenSource'); |
| 118 | is($json->{data}->{foundries}, 'dereko dereko/structure mate mate/dependency', 'Foundries'); |
| 119 | |
| 120 | like($json->{data}->{text}, qr/^Zum letzten kulturellen/, 'Foundries'); |
| 121 | is($json->{data}->{stream}->[0]->[0], '-:tokens$<i>20', 'Tokens'); |
| 122 | |
Akron | e2b902d | 2016-08-16 16:50:11 +0200 | [diff] [blame^] | 123 | |
| 124 | # Check overwrite |
| 125 | $call = join( |
| 126 | ' ', |
| 127 | 'perl', $script, |
| 128 | '--input' => $input, |
| 129 | '--output' => $output, |
| 130 | '-t' => 'CoreNLP#Tokens', |
| 131 | '-s' => '#all', |
| 132 | '-a' => 'DeReKo#Structure', |
| 133 | '-a' => 'Mate#Dependency', |
| 134 | '-l' => 'DEBUG' |
| 135 | ); |
| 136 | |
| 137 | ok(-f $output, 'Output does exist'); |
| 138 | stderr_like( |
| 139 | sub { |
| 140 | system($call); |
| 141 | }, |
| 142 | qr!already exists!, |
| 143 | $call |
| 144 | ); |
| 145 | |
| 146 | $call .= ' -w '; |
| 147 | |
| 148 | stderr_unlike( |
| 149 | sub { |
| 150 | system($call); |
| 151 | }, |
| 152 | qr!already exists!, |
| 153 | $call |
| 154 | ); |
| 155 | |
| 156 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 157 | # Test meta |
| 158 | # Test sigle! |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 159 | |
| 160 | done_testing; |
| 161 | __END__ |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 162 | |