blob: 533e08ecc98412334c572b0182baba71ded93bdb [file] [log] [blame]
Akron797e8072020-02-13 07:59:40 +01001use strict;
2use warnings;
3use File::Basename 'dirname';
4use File::Spec::Functions qw/catfile/;
Peter Harders1c5ce152020-07-22 18:02:50 +02005use Encode qw!encode_utf8 decode_utf8 encode!;
Akron797e8072020-02-13 07:59:40 +01006
7use Test::More;
8use Test::Output;
Peter Harders42e18a62020-07-21 02:43:26 +02009
10use FindBin;
11BEGIN {
12 unshift @INC, "$FindBin::Bin/../lib";
13};
Peter Harders1c5ce152020-07-22 18:02:50 +020014
Akron044bd922020-07-30 09:12:28 +020015use Test::KorAP::XML::TEI qw!korap_tempfile i5_template test_tei2korapxml!;
Peter Harders57c884e2020-07-16 01:28:52 +020016
Akron797e8072020-02-13 07:59:40 +010017my $f = dirname(__FILE__);
18my $script = catfile($f, '..', 'script', 'tei2korapxml');
19ok(-f $script, 'Script found');
20
Akrond949e182020-02-14 12:23:57 +010021stdout_like(
Akron797e8072020-02-13 07:59:40 +010022 sub { system('perl', $script, '--help') },
Akrond949e182020-02-14 12:23:57 +010023 qr!This\s*program\s*is\s*usually\s*called\s*from\s*inside\s*another\s*script\.!,
Akron797e8072020-02-13 07:59:40 +010024 'Help'
25);
26
Akron044bd922020-07-30 09:12:28 +020027
Akrond949e182020-02-14 12:23:57 +010028stdout_like(
29 sub { system('perl', $script, '--version') },
30 qr!tei2korapxml - v\d+?\.\d+?!,
31 'Version'
32);
33
Akron2a60c532020-02-13 15:52:18 +010034# Load example file
35my $file = catfile($f, 'data', 'goe_sample.i5.xml');
Peter Harders57c884e2020-07-16 01:28:52 +020036
Akron044bd922020-07-30 09:12:28 +020037subtest 'Basic processing' => sub {
Akron2a60c532020-02-13 15:52:18 +010038
Akron044bd922020-07-30 09:12:28 +020039 my $t = test_tei2korapxml(
40 tmp => 'script_out',
41 file => $file,
42 param => '-ti'
43 )->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!);
Akron2a60c532020-02-13 15:52:18 +010044
Akron68966082020-02-13 15:52:18 +010045
Akron044bd922020-07-30 09:12:28 +020046 # Uncompress GOE/header.xml from zip file
47 $t->unzip_xml('GOE/header.xml')
Akron2a60c532020-02-13 15:52:18 +010048
Akron044bd922020-07-30 09:12:28 +020049 # TODO: check wrong encoding in header-files (compare with input document)!
50 ->text_is('korpusSigle', 'GOE', 'korpusSigle')
51 ->text_is('h\.title[type=main]', 'Goethes Werke', 'h.title')
52 ->text_is('h\.author', 'Goethe, Johann Wolfgang von', 'h.author')
53 ->text_is('pubDate[type=year]', '1982', 'pubDate');
Akron2a60c532020-02-13 15:52:18 +010054
Akron044bd922020-07-30 09:12:28 +020055 # Uncompress GOE/AGA/header.xml from zip file
56 $t->unzip_xml('GOE/AGA/header.xml')
57 ->text_is('dokumentSigle', 'GOE/AGA', 'dokumentSigle')
58 ->text_is('d\.title', 'Goethe: Autobiographische Schriften II, (1817-1825, 1832)', 'd.title')
59 ->text_is('creatDate', '1820-1822', 'creatDate');
Akron2a60c532020-02-13 15:52:18 +010060
Akron044bd922020-07-30 09:12:28 +020061 # Uncompress GOE/AGA/00000/header.xml from zip file
62 $t->unzip_xml('GOE/AGA/00000/header.xml')
63 ->text_is('textSigle', 'GOE/AGA.00000', 'textSigle')
64 ->text_is('analytic > h\.title[type=main]', 'Campagne in Frankreich', 'h.title');
Akron2a60c532020-02-13 15:52:18 +010065
66# Uncompress GOE/AGA/00000/data.xml from zip file
Akron044bd922020-07-30 09:12:28 +020067 $t->unzip_xml('GOE/AGA/00000/data.xml')
68 ->attr_is('raw_text', 'docid', 'GOE_AGA.00000', 'text id')
69 ->text_like(
70 'raw_text > text',
71 qr!^Campagne in Frankreich 1792.*?uns allein begl.*cke\.$!,
72 'text content');
Akron2a60c532020-02-13 15:52:18 +010073
Akron044bd922020-07-30 09:12:28 +020074 $t->unzip_xml('GOE/AGA/00000/struct/structure.xml')
75 ->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content');
Akron2a60c532020-02-13 15:52:18 +010076
Akron044bd922020-07-30 09:12:28 +020077 $t->file_exists_not('GOE/AGA/00000/base/tokens.xml', 'External not generated');
Akron2a60c532020-02-13 15:52:18 +010078
Akron044bd922020-07-30 09:12:28 +020079 # Uncompress GOE/AGA/00000/base/tokens_aggressive.xml from zip file
80 $t->unzip_xml('GOE/AGA/00000/base/tokens_aggressive.xml')
81 ->attr_is('spanList span:nth-child(1)', 'to', 8)
82 ->attr_is('spanList span#t_1', 'from', 9)
83 ->attr_is('spanList span#t_1', 'to', 11)
Akron2a60c532020-02-13 15:52:18 +010084
Akron044bd922020-07-30 09:12:28 +020085 ->attr_is('spanList span#t_67', 'from', 427)
86 ->attr_is('spanList span#t_67', 'to', 430)
Akron2a60c532020-02-13 15:52:18 +010087
Akron044bd922020-07-30 09:12:28 +020088 ->attr_is('spanList span#t_214', 'from', 1209)
89 ->attr_is('spanList span#t_214', 'to', 1212)
Akron2a60c532020-02-13 15:52:18 +010090
Akron044bd922020-07-30 09:12:28 +020091 ->element_count_is('spanList span', 227);
Peter Harders57c884e2020-07-16 01:28:52 +020092
Akron044bd922020-07-30 09:12:28 +020093 # Uncompress GOE/AGA/00000/base/tokens_conservative.xml from zip file
94 $t->unzip_xml('GOE/AGA/00000/base/tokens_conservative.xml')
95 ->attr_is('spanList span:nth-child(1)', 'to', 8)
Akron2a60c532020-02-13 15:52:18 +010096
Akron044bd922020-07-30 09:12:28 +020097 ->attr_is('spanList span#t_1', 'from', 9)
98 ->attr_is('spanList span#t_1', 'to', 11)
Akron797e8072020-02-13 07:59:40 +010099
Akron044bd922020-07-30 09:12:28 +0200100 ->attr_is('spanList span#t_67', 'from', 427)
101 ->attr_is('spanList span#t_67', 'to', 430)
Akroneac374d2020-07-07 09:00:44 +0200102
Akron044bd922020-07-30 09:12:28 +0200103 ->attr_is('spanList span#t_214', 'from', 1209)
104 ->attr_is('spanList span#t_214', 'to', 1212)
Akroneac374d2020-07-07 09:00:44 +0200105
Akron044bd922020-07-30 09:12:28 +0200106 ->element_count_is('spanList span', 227);
107};
Akroneac374d2020-07-07 09:00:44 +0200108
Akroneac374d2020-07-07 09:00:44 +0200109
Akron044bd922020-07-30 09:12:28 +0200110subtest 'Tokenize with external tokenizer' => sub {
Akroneac374d2020-07-07 09:00:44 +0200111
Akron044bd922020-07-30 09:12:28 +0200112 my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
Akroneac374d2020-07-07 09:00:44 +0200113
Akron044bd922020-07-30 09:12:28 +0200114 test_tei2korapxml(
115 file => $file,
116 param => "-tc='perl $cmd'",
117 tmp => 'script_out2'
118 )
119 ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!)
120 ->file_readable('GOE/AGA/00000/base/tokens.xml')
Akroneac374d2020-07-07 09:00:44 +0200121
Akron044bd922020-07-30 09:12:28 +0200122 # Uncompress GOE/AGA/00000/base/tokens.xml from zip file
123 ->unzip_xml('GOE/AGA/00000/base/tokens.xml')
124 ->attr_is('spanList span:nth-child(1)', 'to', 8)
125 ->attr_is('spanList span#t_1', 'from', 9)
126 ->attr_is('spanList span#t_1', 'to', 11)
127 ->attr_is('spanList span#t_67', 'from', 427)
128 ->attr_is('spanList span#t_67', 'to', 430)
129 ->attr_is('spanList span#t_214', 'from', 1209)
130 ->attr_is('spanList span#t_214', 'to', 1212)
131 ->element_count_is('spanList span', 227);
132};
Akroneac374d2020-07-07 09:00:44 +0200133
134
Akron044bd922020-07-30 09:12:28 +0200135subtest 'Test Tokenizations' => sub {
Akroneac374d2020-07-07 09:00:44 +0200136
Akron044bd922020-07-30 09:12:28 +0200137 my $t = test_tei2korapxml(
138 file => catfile($f, 'data', 'text_with_blanks.i5.xml'),
139 tmp => 'script_out3',
140 param => '-ti'
141 )->stderr_like(qr!tei2korapxml: .*? text_id=CORP_DOC.00001!);
Akron8b511f92020-07-09 17:28:08 +0200142
Akron044bd922020-07-30 09:12:28 +0200143 # ~ test conservative tokenization ~
144 $t->unzip_xml('CORP/DOC/00001/base/tokens_conservative.xml')
145 ->attr_is('spanList span:nth-child(1)', 'to', 6)
Akron8b511f92020-07-09 17:28:08 +0200146
Akron044bd922020-07-30 09:12:28 +0200147 ->attr_is('spanList span#t_1', 'from', 7)
148 ->attr_is('spanList span#t_1', 'to', 9)
Akron8b511f92020-07-09 17:28:08 +0200149
Akron044bd922020-07-30 09:12:28 +0200150 ->attr_is('spanList span#t_3', 'from', 12)
151 ->attr_is('spanList span#t_3', 'to', 16)
Akron8b511f92020-07-09 17:28:08 +0200152
Akron044bd922020-07-30 09:12:28 +0200153 ->attr_is('spanList span#t_9', 'from', 36)
154 ->attr_is('spanList span#t_9', 'to', 37)
Akron8b511f92020-07-09 17:28:08 +0200155
Akron044bd922020-07-30 09:12:28 +0200156 ->attr_is('spanList span#t_13', 'from', 44)
157 ->attr_is('spanList span#t_13', 'to', 45) # "
Akron8b511f92020-07-09 17:28:08 +0200158
Akron044bd922020-07-30 09:12:28 +0200159 ->attr_is('spanList span#t_14', 'from', 45) # twenty-two
160 ->attr_is('spanList span#t_14', 'to', 55)
Peter Harders42e18a62020-07-21 02:43:26 +0200161
Akron044bd922020-07-30 09:12:28 +0200162 ->attr_is('spanList span#t_15', 'from', 55) # "
163 ->attr_is('spanList span#t_15', 'to', 56)
Akron8b511f92020-07-09 17:28:08 +0200164
Akron044bd922020-07-30 09:12:28 +0200165 ->attr_is('spanList span#t_19', 'from', 66)
166 ->attr_is('spanList span#t_19', 'to', 67)
Peter Hardersb1227172020-07-21 02:12:10 +0200167
Akron044bd922020-07-30 09:12:28 +0200168 ->element_count_is('spanList span', 20);
Peter Harders71f072b2020-07-15 14:15:01 +0200169
Peter Harders71f072b2020-07-15 14:15:01 +0200170
Akron044bd922020-07-30 09:12:28 +0200171 # ~ test aggressive tokenization ~
172 $t->unzip_xml('CORP/DOC/00001/base/tokens_aggressive.xml')
173 ->attr_is('spanList span:nth-child(1)', 'to', 6)
Peter Harders71f072b2020-07-15 14:15:01 +0200174
Akron044bd922020-07-30 09:12:28 +0200175 ->attr_is('spanList span#t_1', 'from', 7)
176 ->attr_is('spanList span#t_1', 'to', 9)
Peter Harders71f072b2020-07-15 14:15:01 +0200177
Akron044bd922020-07-30 09:12:28 +0200178 ->attr_is('spanList span#t_3', 'from', 12)
179 ->attr_is('spanList span#t_3', 'to', 16)
Peter Harders71f072b2020-07-15 14:15:01 +0200180
Akron044bd922020-07-30 09:12:28 +0200181 ->attr_is('spanList span#t_9', 'from', 36)
182 ->attr_is('spanList span#t_9', 'to', 37)
Peter Harders71f072b2020-07-15 14:15:01 +0200183
Akron044bd922020-07-30 09:12:28 +0200184 ->attr_is('spanList span#t_13', 'from', 44)
185 ->attr_is('spanList span#t_13', 'to', 45) # "
Peter Harders71f072b2020-07-15 14:15:01 +0200186
Akron044bd922020-07-30 09:12:28 +0200187 ->attr_is('spanList span#t_14', 'from', 45) # twenty
188 ->attr_is('spanList span#t_14', 'to', 51)
Peter Harders71f072b2020-07-15 14:15:01 +0200189
Akron044bd922020-07-30 09:12:28 +0200190 ->attr_is('spanList span#t_15', 'from', 51) # -
191 ->attr_is('spanList span#t_15', 'to', 52)
Peter Harders71f072b2020-07-15 14:15:01 +0200192
Akron044bd922020-07-30 09:12:28 +0200193 ->attr_is('spanList span#t_16', 'from', 52) # two
194 ->attr_is('spanList span#t_16', 'to', 55)
Peter Harders71f072b2020-07-15 14:15:01 +0200195
Akron044bd922020-07-30 09:12:28 +0200196 ->attr_is('spanList span#t_17', 'from', 55) # "
197 ->attr_is('spanList span#t_17', 'to', 56)
Peter Harders71f072b2020-07-15 14:15:01 +0200198
Akron044bd922020-07-30 09:12:28 +0200199 ->attr_is('spanList span#t_21', 'from', 66)
200 ->attr_is('spanList span#t_21', 'to', 67)
Peter Harders71f072b2020-07-15 14:15:01 +0200201
Akron044bd922020-07-30 09:12:28 +0200202 ->element_count_is('spanList span', 22);
203};
Peter Harders71f072b2020-07-15 14:15:01 +0200204
205
Peter Harders42e18a62020-07-21 02:43:26 +0200206subtest 'Check Tokenization Flags' => sub {
207
208 # Get external tokenizer
209 my $f = dirname(__FILE__);
210 my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
211
212 # Load example file
Akron044bd922020-07-30 09:12:28 +0200213 test_tei2korapxml(
214 file => catfile($f, 'data', 'goe_sample.i5.xml'),
215 param => "-ti -tc 'perl $cmd'",
216 tmp => 'script_tokflags'
217 )
218 ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!)
219 ->file_exists('GOE/AGA/00000/base/tokens_aggressive.xml')
220 ->file_exists('GOE/AGA/00000/base/tokens_conservative.xml')
221 ->file_exists('GOE/AGA/00000/base/tokens.xml')
222 ;
Peter Harders42e18a62020-07-21 02:43:26 +0200223};
224
Peter Harders1c5ce152020-07-22 18:02:50 +0200225
226subtest 'Test utf-8 handling' => sub {
Peter Harders1c5ce152020-07-22 18:02:50 +0200227 # Introduce invalid utf-8 characters
228 my $text_sigle;
Akron54e363c2020-07-29 11:01:03 +0200229 {
230 no warnings;
231 # $text_sigle printed to file, without encoding: Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C
232 # the utf8-sequence 'þƒ¿¿¿¿¿' encodes 32 bit of data (see 0x7FFF_FFFF in perlunicode)
233 $text_sigle = "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B.C\x{FFFF_FFFF}C"
234 }
235 # If CHECK is 0, encoding and decoding replace any malformed character
236 # with a substitution character.
Peter Harders1c5ce152020-07-22 18:02:50 +0200237 # � = substitution character
238 my $text_sigle_lax = encode_utf8($text_sigle);
239 my $text_sigle_esc = encode('UTF-8', $text_sigle);
240
241 is(length($text_sigle), 11); # A�A_B�B.C�C (char string => length(�) = 1)
242 is(length($text_sigle_lax), 29); # Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C (byte string)
243 is(length($text_sigle_esc), 17); # A�A_B�B.C�C (byte string => length(�) = 3)
244
Akron54e363c2020-07-29 11:01:03 +0200245
246 my $tpl;
247 {
248 no warnings;
249 $tpl = i5_template(
250 korpusSigle => "A\x{FFFF_FFFF}A",
251 dokumentSigle => "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B",
252 text => "<p>d\x{FFFF_FFFF}d e\x{FFFF_FFFF}e f\x{FFFF_FFFF}f</p>",
253 textSigle => $text_sigle
254 );
255 };
Peter Harders1c5ce152020-07-22 18:02:50 +0200256
257 my ($fh, $tplfile) = korap_tempfile('script_out4');
258 binmode($fh);
259 print $fh encode_utf8($tpl); # => text_id=Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C
260 close($fh);
261
262 my (undef, $outzip) = korap_tempfile('script_out5');
263
Akron54e363c2020-07-29 11:01:03 +0200264 # because output 'textid=...' goes to STDERR (see script/tei2korapxml)
265 binmode STDERR, qw{ :encoding(UTF-8) };
Peter Harders1c5ce152020-07-22 18:02:50 +0200266
267 stderr_like(
268 sub { `cat '$tplfile' | perl '$script' -ti > '$outzip'` },
269 qr!tei2korapxml: .*? text_id=$text_sigle_lax!, # see above: print $fh encode_utf8($tpl);
270 );
271};
272
Akrone68ec0c2020-07-28 18:06:19 +0200273
274subtest 'Check Inline annotations' => sub {
275
276 # Load example file
277 my $file = catfile($f, 'data', 'goe_sample_tagged.i5.xml');
278
Akron044bd922020-07-30 09:12:28 +0200279 my $t = test_tei2korapxml(
280 file => $file,
281 env => 'KORAPXMLTEI_INLINE=1',
282 tmp => 'script_tagged'
283 )
284 ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!)
Akrone68ec0c2020-07-28 18:06:19 +0200285
Akron044bd922020-07-30 09:12:28 +0200286 # Check zip using xml loy
287 ->unzip_xml('GOE/AGA/00000/tokens/morpho.xml')
Akrone68ec0c2020-07-28 18:06:19 +0200288
Akron044bd922020-07-30 09:12:28 +0200289 ->attr_is('layer', 'docid', 'GOE_AGA.00000')
Akrone68ec0c2020-07-28 18:06:19 +0200290 ->attr_is('spanList span:nth-child(1)', 'id', 's0')
291 ->attr_is('spanList span:nth-child(1)', 'from', '75')
292 ->attr_is('spanList span:nth-child(1)', 'to', '81')
293 ->attr_is('spanList span:nth-child(1)', 'l', '7')
294
295 ->attr_is('span#s0 > fs', 'type', 'lex')
296 ->attr_is('span#s0 > fs', 'xmlns', 'http://www.tei-c.org/ns/1.0')
297 ->attr_is('span#s0 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
298 ->text_is('span#s0 > fs > f > fs > f:nth-child(1)', 'A')
299 ->attr_is('span#s0 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
300 ->text_is('span#s0 > fs > f > fs > f:nth-child(2)', '@NH')
301
302 ->attr_is('span#s25', 'from', '259')
303 ->attr_is('span#s25', 'to', '263')
304 ->attr_is('span#s25', 'l', '7')
305 ->attr_is('span#s25 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
306 ->text_is('span#s25 > fs > f > fs > f:nth-child(1)', 'PRON')
307 ->attr_is('span#s25 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
308 ->text_is('span#s25 > fs > f > fs > f:nth-child(2)', '@NH')
309
310 ->attr_is('span#s58', 'from', '495')
311 ->attr_is('span#s58', 'to', '500')
312 ->attr_is('span#s58', 'l', '7')
313 ->attr_is('span#s58 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
314 ->text_is('span#s58 > fs > f > fs > f:nth-child(1)', 'N')
315 ->attr_is('span#s58 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
316 ->text_is('span#s58 > fs > f > fs > f:nth-child(2)', '@NH')
317
318 ->attr_is('span#s119', 'from', '914')
319 ->attr_is('span#s119', 'to', '925')
320 ->attr_is('span#s119', 'l', '7')
321 ->attr_is('span#s119 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
322 ->text_is('span#s119 > fs > f > fs > f:nth-child(1)', 'A')
323 ->attr_is('span#s119 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
324 ->text_is('span#s119 > fs > f > fs > f:nth-child(2)', '@NH')
325 ->element_exists_not('span#s120')
326 ;
327};
328
Akron09e0b2c2020-07-28 15:57:01 +0200329subtest 'Check Inline annotations with untagged file' => sub {
330
331 # Load example file
332 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
333
334 my ($fh, $outzip) = korap_tempfile('script_untagged');
335
336 # Generate zip file (unportable!)
337 stderr_like(
338 sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` },
339 qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!,
340 'Processing 1'
341 );
342
343 # TODO: there should be a better way to test this
344 stderr_unlike(
345 sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` },
346 qr!.*undefined value.*!,
347 'Processing 2'
348 );
349 #
350
351 ok(-e $outzip, "File $outzip exists");
352
353 my $zip = IO::Uncompress::Unzip->new(
354 $outzip,
355 Name => 'GOE/AGA/00000/tokens/morpho.xml'
356 );
357 ok((not $zip), 'missing morpho.xml');
358
359 $zip = IO::Uncompress::Unzip->new(
360 $outzip,
361 Name => 'GOE/AGA/00000/struct/structure.xml'
362 );
363 ok($zip, 'found structure.xml');
364};
Akrone68ec0c2020-07-28 18:06:19 +0200365
Akron797e8072020-02-13 07:59:40 +0100366done_testing;