blob: 6f1d2d1069757f6ac912555f1c130bf6816317b3 [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
Akronb3649472020-09-29 08:24:46 +020037subtest 'Debugging' => sub {
38
39 my $t = test_tei2korapxml(
40 tmp => 'script_out',
41 file => $file,
42 param => '-ti',
43 env => 'KORAPXMLTEI_DEBUG=1'
Akron91577922021-02-19 10:32:54 +010044 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akronb3649472020-09-29 08:24:46 +020045 ->stderr_like(qr!Debugging is activated!);
46};
47
Akron044bd922020-07-30 09:12:28 +020048subtest 'Basic processing' => sub {
Akron2a60c532020-02-13 15:52:18 +010049
Akron044bd922020-07-30 09:12:28 +020050 my $t = test_tei2korapxml(
51 tmp => 'script_out',
52 file => $file,
53 param => '-ti'
Akron91577922021-02-19 10:32:54 +010054 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akronb3649472020-09-29 08:24:46 +020055 ->stderr_unlike(qr!Debugging is activated!);
Akron2a60c532020-02-13 15:52:18 +010056
Akron68966082020-02-13 15:52:18 +010057
Akron044bd922020-07-30 09:12:28 +020058 # Uncompress GOE/header.xml from zip file
59 $t->unzip_xml('GOE/header.xml')
Akron2a60c532020-02-13 15:52:18 +010060
Akron044bd922020-07-30 09:12:28 +020061 # TODO: check wrong encoding in header-files (compare with input document)!
62 ->text_is('korpusSigle', 'GOE', 'korpusSigle')
63 ->text_is('h\.title[type=main]', 'Goethes Werke', 'h.title')
64 ->text_is('h\.author', 'Goethe, Johann Wolfgang von', 'h.author')
65 ->text_is('pubDate[type=year]', '1982', 'pubDate');
Akron2a60c532020-02-13 15:52:18 +010066
Akron044bd922020-07-30 09:12:28 +020067 # Uncompress GOE/AGA/header.xml from zip file
68 $t->unzip_xml('GOE/AGA/header.xml')
69 ->text_is('dokumentSigle', 'GOE/AGA', 'dokumentSigle')
70 ->text_is('d\.title', 'Goethe: Autobiographische Schriften II, (1817-1825, 1832)', 'd.title')
71 ->text_is('creatDate', '1820-1822', 'creatDate');
Akron2a60c532020-02-13 15:52:18 +010072
Akron044bd922020-07-30 09:12:28 +020073 # Uncompress GOE/AGA/00000/header.xml from zip file
74 $t->unzip_xml('GOE/AGA/00000/header.xml')
75 ->text_is('textSigle', 'GOE/AGA.00000', 'textSigle')
76 ->text_is('analytic > h\.title[type=main]', 'Campagne in Frankreich', 'h.title');
Akron2a60c532020-02-13 15:52:18 +010077
78# Uncompress GOE/AGA/00000/data.xml from zip file
Akron044bd922020-07-30 09:12:28 +020079 $t->unzip_xml('GOE/AGA/00000/data.xml')
80 ->attr_is('raw_text', 'docid', 'GOE_AGA.00000', 'text id')
Marc Kupietzfd0e6a92020-09-09 18:07:29 +020081 ->text_like('raw_text > text', qr!^Campagne in Frankreich 1792.*?uns allein begl.*cke\.$!, 'text content')
82 ->text_like('raw_text > text', qr!unter dem Titel "Kriegstheater"!, 'text content');
Akron2a60c532020-02-13 15:52:18 +010083
Akron417ed2f2020-09-28 12:43:17 +020084 my $content = $t->get_content_of('GOE/AGA/00000/data.xml');
85 like($content, qr!unter dem Titel "Kriegstheater"!, 'raw text content');
86
Akron044bd922020-07-30 09:12:28 +020087 $t->unzip_xml('GOE/AGA/00000/struct/structure.xml')
Akronc57625b2020-07-30 11:46:06 +020088 ->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content')
89 ->text_is('#s3 *[name=type]', 'Autobiographie', 'text content')
90 ->attr_is('#s0','to','1266')
91 ->attr_is('#s0','l','1')
92
93 ->attr_is('#s18','from','925')
94 ->attr_is('#s18','to','1266')
95 ->attr_is('#s18','l','5')
96 ->attr_is('#s18 > fs','type', 'struct')
97 ->attr_is('#s18 > fs > f','name','name')
98 ->text_is('#s18 > fs > f','poem')
99
100 ->attr_is('#s19','from','925')
101 ->attr_is('#s19','to','1098')
102 ->attr_is('#s19','l','6')
103 ->attr_is('#s19 > fs','type','struct')
104 ->text_is('#s19 > fs > f[name=name]','lg')
105 ->text_is('#s19 > fs > f[name=attr] > fs[type=attr] >f[name=part]','u')
106
107 ->attr_is('#s37','from','1229')
108 ->attr_is('#s37','to','1266')
109 ->attr_is('#s37','l','8')
110 ->attr_is('#s37 > fs','type','struct')
111 ->text_is('#s37 > fs > f[name=name]','s')
112 ->text_is('#s37 > fs > f[name=attr] > fs[type=attr] > f[name=type]','manual')
113
114 ->attr_is('#s38','from','1266')
115 ->attr_is('#s38','to','1266')
116 ->attr_is('#s38','l','2')
117 ->attr_is('#s38 > fs','type','struct')
118 ->text_is('#s38 > fs > f[name=name]','back')
Akronf7084c42021-01-07 10:25:22 +0100119 ->element_count_is('*', 196);
Akron2a60c532020-02-13 15:52:18 +0100120
Akron044bd922020-07-30 09:12:28 +0200121 $t->file_exists_not('GOE/AGA/00000/base/tokens.xml', 'External not generated');
Akron2a60c532020-02-13 15:52:18 +0100122
Akron044bd922020-07-30 09:12:28 +0200123 # Uncompress GOE/AGA/00000/base/tokens_aggressive.xml from zip file
124 $t->unzip_xml('GOE/AGA/00000/base/tokens_aggressive.xml')
125 ->attr_is('spanList span:nth-child(1)', 'to', 8)
126 ->attr_is('spanList span#t_1', 'from', 9)
127 ->attr_is('spanList span#t_1', 'to', 11)
Akron2a60c532020-02-13 15:52:18 +0100128
Akron044bd922020-07-30 09:12:28 +0200129 ->attr_is('spanList span#t_67', 'from', 427)
130 ->attr_is('spanList span#t_67', 'to', 430)
Akron2a60c532020-02-13 15:52:18 +0100131
Akron044bd922020-07-30 09:12:28 +0200132 ->attr_is('spanList span#t_214', 'from', 1209)
133 ->attr_is('spanList span#t_214', 'to', 1212)
Akron2a60c532020-02-13 15:52:18 +0100134
Akron044bd922020-07-30 09:12:28 +0200135 ->element_count_is('spanList span', 227);
Peter Harders57c884e2020-07-16 01:28:52 +0200136
Akron044bd922020-07-30 09:12:28 +0200137 # Uncompress GOE/AGA/00000/base/tokens_conservative.xml from zip file
138 $t->unzip_xml('GOE/AGA/00000/base/tokens_conservative.xml')
139 ->attr_is('spanList span:nth-child(1)', 'to', 8)
Akron2a60c532020-02-13 15:52:18 +0100140
Akron044bd922020-07-30 09:12:28 +0200141 ->attr_is('spanList span#t_1', 'from', 9)
142 ->attr_is('spanList span#t_1', 'to', 11)
Akron797e8072020-02-13 07:59:40 +0100143
Akron044bd922020-07-30 09:12:28 +0200144 ->attr_is('spanList span#t_67', 'from', 427)
145 ->attr_is('spanList span#t_67', 'to', 430)
Akroneac374d2020-07-07 09:00:44 +0200146
Akron044bd922020-07-30 09:12:28 +0200147 ->attr_is('spanList span#t_214', 'from', 1209)
148 ->attr_is('spanList span#t_214', 'to', 1212)
Akroneac374d2020-07-07 09:00:44 +0200149
Akron044bd922020-07-30 09:12:28 +0200150 ->element_count_is('spanList span', 227);
151};
Akroneac374d2020-07-07 09:00:44 +0200152
Akroneac374d2020-07-07 09:00:44 +0200153
Akron044bd922020-07-30 09:12:28 +0200154subtest 'Tokenize with external tokenizer' => sub {
Akroneac374d2020-07-07 09:00:44 +0200155
Akron044bd922020-07-30 09:12:28 +0200156 my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
Akroneac374d2020-07-07 09:00:44 +0200157
Akron044bd922020-07-30 09:12:28 +0200158 test_tei2korapxml(
159 file => $file,
160 param => "-tc='perl $cmd'",
161 tmp => 'script_out2'
162 )
Akron91577922021-02-19 10:32:54 +0100163 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akron044bd922020-07-30 09:12:28 +0200164 ->file_readable('GOE/AGA/00000/base/tokens.xml')
Akroneac374d2020-07-07 09:00:44 +0200165
Akron044bd922020-07-30 09:12:28 +0200166 # Uncompress GOE/AGA/00000/base/tokens.xml from zip file
167 ->unzip_xml('GOE/AGA/00000/base/tokens.xml')
168 ->attr_is('spanList span:nth-child(1)', 'to', 8)
169 ->attr_is('spanList span#t_1', 'from', 9)
170 ->attr_is('spanList span#t_1', 'to', 11)
171 ->attr_is('spanList span#t_67', 'from', 427)
172 ->attr_is('spanList span#t_67', 'to', 430)
173 ->attr_is('spanList span#t_214', 'from', 1209)
174 ->attr_is('spanList span#t_214', 'to', 1212)
175 ->element_count_is('spanList span', 227);
176};
Akroneac374d2020-07-07 09:00:44 +0200177
Akron91705d72021-02-19 10:59:45 +0100178subtest 'Tokenize with external tokenizer and defined folder' => sub {
179
180 my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
181
182 test_tei2korapxml(
183 file => $file,
184 param => "-tc='perl $cmd' --tokens-file=yadda",
185 tmp => 'script_out2'
186 )
187 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
188 ->file_exists_not('GOE/AGA/00000/base/tokens.xml')
189 ->file_readable('GOE/AGA/00000/base/yadda.xml')
190 ->unzip_xml('GOE/AGA/00000/base/yadda.xml')
191 ->attr_is('spanList span:nth-child(1)', 'to', 8)
192 ->attr_is('spanList span#t_1', 'from', 9)
193 ->attr_is('spanList span#t_1', 'to', 11)
194 ->attr_is('spanList span#t_67', 'from', 427)
195 ->attr_is('spanList span#t_67', 'to', 430)
196 ->attr_is('spanList span#t_214', 'from', 1209)
197 ->attr_is('spanList span#t_214', 'to', 1212)
198 ->element_count_is('spanList span', 227);
199};
200
Marc Kupietzeed4cb12021-02-17 19:39:32 +0100201subtest 'Check KorAP tokenizer for infinite loop bug' => sub {
202
203 my $file = catfile($f, 'data', 'korap_tokenizer_challenge.xml');
204
205 eval {
206 require KorAP::XML::TEI::Tokenizer::KorAP;
207 1;
208 } or do {
209 plan skip_all => "KorAP::XML::TEI::Tokenizer::KorAP cannot be used";
210 };
211
212 test_tei2korapxml(
213 file => $file,
214 param => "-tk -s",
215 tmp => 'script_bug_check'
216 )
Akron91577922021-02-19 10:32:54 +0100217 ->stderr_like(qr!tei2korapxml:.*? text_id=WDD19_H0039\.87242!)
Marc Kupietzeed4cb12021-02-17 19:39:32 +0100218 ->file_readable('WDD19/H0039/87242/struct/structure.xml');
219};
220
Marc Kupietz985da0c2021-02-15 19:29:50 +0100221subtest 'Sentence split with KorAP tokenizer' => sub {
222
223 eval {
224 require KorAP::XML::TEI::Tokenizer::KorAP;
225 1;
226 } or do {
227 plan skip_all => "KorAP::XML::TEI::Tokenizer::KorAP cannot be used";
228 };
229
230 test_tei2korapxml(
231 file => $file,
232 param => "-tk -s",
233 tmp => 'script_sentence_split'
234 )
Akron91577922021-02-19 10:32:54 +0100235 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Marc Kupietz985da0c2021-02-15 19:29:50 +0100236 ->file_readable('GOE/AGA/00000/struct/structure.xml')
237 ->unzip_xml('GOE/AGA/00000/struct/structure.xml')
238 ->text_is('span#s25 fs f', 's')
239 ->attr_is('span#s25', 'l', -1)
240 ->attr_is('span#s25', 'to', 54)
241 ->text_is('span#s30 fs f', 's')
242 ->attr_is('span#s30', 'l', -1)
243 ->attr_is('span#s30', 'from', 1099)
244 ->attr_is('span#s30', 'to', 1266);
245};
Akroneac374d2020-07-07 09:00:44 +0200246
Akron044bd922020-07-30 09:12:28 +0200247subtest 'Test Tokenizations' => sub {
Akroneac374d2020-07-07 09:00:44 +0200248
Akron044bd922020-07-30 09:12:28 +0200249 my $t = test_tei2korapxml(
250 file => catfile($f, 'data', 'text_with_blanks.i5.xml'),
251 tmp => 'script_out3',
252 param => '-ti'
Akron91577922021-02-19 10:32:54 +0100253 )->stderr_like(qr!tei2korapxml:.*? text_id=CORP_DOC.00001!);
Akron8b511f92020-07-09 17:28:08 +0200254
Akron044bd922020-07-30 09:12:28 +0200255 # ~ test conservative tokenization ~
256 $t->unzip_xml('CORP/DOC/00001/base/tokens_conservative.xml')
257 ->attr_is('spanList span:nth-child(1)', 'to', 6)
Akron8b511f92020-07-09 17:28:08 +0200258
Akron044bd922020-07-30 09:12:28 +0200259 ->attr_is('spanList span#t_1', 'from', 7)
260 ->attr_is('spanList span#t_1', 'to', 9)
Akron8b511f92020-07-09 17:28:08 +0200261
Akron044bd922020-07-30 09:12:28 +0200262 ->attr_is('spanList span#t_3', 'from', 12)
263 ->attr_is('spanList span#t_3', 'to', 16)
Akron8b511f92020-07-09 17:28:08 +0200264
Akron044bd922020-07-30 09:12:28 +0200265 ->attr_is('spanList span#t_9', 'from', 36)
266 ->attr_is('spanList span#t_9', 'to', 37)
Akron8b511f92020-07-09 17:28:08 +0200267
Akron044bd922020-07-30 09:12:28 +0200268 ->attr_is('spanList span#t_13', 'from', 44)
269 ->attr_is('spanList span#t_13', 'to', 45) # "
Akron8b511f92020-07-09 17:28:08 +0200270
Akron044bd922020-07-30 09:12:28 +0200271 ->attr_is('spanList span#t_14', 'from', 45) # twenty-two
272 ->attr_is('spanList span#t_14', 'to', 55)
Peter Harders42e18a62020-07-21 02:43:26 +0200273
Akron044bd922020-07-30 09:12:28 +0200274 ->attr_is('spanList span#t_15', 'from', 55) # "
275 ->attr_is('spanList span#t_15', 'to', 56)
Akron8b511f92020-07-09 17:28:08 +0200276
Akron044bd922020-07-30 09:12:28 +0200277 ->attr_is('spanList span#t_19', 'from', 66)
278 ->attr_is('spanList span#t_19', 'to', 67)
Peter Hardersb1227172020-07-21 02:12:10 +0200279
Akron044bd922020-07-30 09:12:28 +0200280 ->element_count_is('spanList span', 20);
Peter Harders71f072b2020-07-15 14:15:01 +0200281
Peter Harders71f072b2020-07-15 14:15:01 +0200282
Akron044bd922020-07-30 09:12:28 +0200283 # ~ test aggressive tokenization ~
284 $t->unzip_xml('CORP/DOC/00001/base/tokens_aggressive.xml')
285 ->attr_is('spanList span:nth-child(1)', 'to', 6)
Peter Harders71f072b2020-07-15 14:15:01 +0200286
Akron044bd922020-07-30 09:12:28 +0200287 ->attr_is('spanList span#t_1', 'from', 7)
288 ->attr_is('spanList span#t_1', 'to', 9)
Peter Harders71f072b2020-07-15 14:15:01 +0200289
Akron044bd922020-07-30 09:12:28 +0200290 ->attr_is('spanList span#t_3', 'from', 12)
291 ->attr_is('spanList span#t_3', 'to', 16)
Peter Harders71f072b2020-07-15 14:15:01 +0200292
Akron044bd922020-07-30 09:12:28 +0200293 ->attr_is('spanList span#t_9', 'from', 36)
294 ->attr_is('spanList span#t_9', 'to', 37)
Peter Harders71f072b2020-07-15 14:15:01 +0200295
Akron044bd922020-07-30 09:12:28 +0200296 ->attr_is('spanList span#t_13', 'from', 44)
297 ->attr_is('spanList span#t_13', 'to', 45) # "
Peter Harders71f072b2020-07-15 14:15:01 +0200298
Akron044bd922020-07-30 09:12:28 +0200299 ->attr_is('spanList span#t_14', 'from', 45) # twenty
300 ->attr_is('spanList span#t_14', 'to', 51)
Peter Harders71f072b2020-07-15 14:15:01 +0200301
Akron044bd922020-07-30 09:12:28 +0200302 ->attr_is('spanList span#t_15', 'from', 51) # -
303 ->attr_is('spanList span#t_15', 'to', 52)
Peter Harders71f072b2020-07-15 14:15:01 +0200304
Akron044bd922020-07-30 09:12:28 +0200305 ->attr_is('spanList span#t_16', 'from', 52) # two
306 ->attr_is('spanList span#t_16', 'to', 55)
Peter Harders71f072b2020-07-15 14:15:01 +0200307
Akron044bd922020-07-30 09:12:28 +0200308 ->attr_is('spanList span#t_17', 'from', 55) # "
309 ->attr_is('spanList span#t_17', 'to', 56)
Peter Harders71f072b2020-07-15 14:15:01 +0200310
Akron044bd922020-07-30 09:12:28 +0200311 ->attr_is('spanList span#t_21', 'from', 66)
312 ->attr_is('spanList span#t_21', 'to', 67)
Peter Harders71f072b2020-07-15 14:15:01 +0200313
Akron044bd922020-07-30 09:12:28 +0200314 ->element_count_is('spanList span', 22);
315};
Peter Harders71f072b2020-07-15 14:15:01 +0200316
317
Peter Harders42e18a62020-07-21 02:43:26 +0200318subtest 'Check Tokenization Flags' => sub {
319
320 # Get external tokenizer
321 my $f = dirname(__FILE__);
322 my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
323
324 # Load example file
Akron044bd922020-07-30 09:12:28 +0200325 test_tei2korapxml(
326 file => catfile($f, 'data', 'goe_sample.i5.xml'),
327 param => "-ti -tc 'perl $cmd'",
328 tmp => 'script_tokflags'
329 )
Akron91577922021-02-19 10:32:54 +0100330 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akron044bd922020-07-30 09:12:28 +0200331 ->file_exists('GOE/AGA/00000/base/tokens_aggressive.xml')
332 ->file_exists('GOE/AGA/00000/base/tokens_conservative.xml')
333 ->file_exists('GOE/AGA/00000/base/tokens.xml')
334 ;
Peter Harders42e18a62020-07-21 02:43:26 +0200335};
336
Peter Harders1c5ce152020-07-22 18:02:50 +0200337
338subtest 'Test utf-8 handling' => sub {
Peter Harders1c5ce152020-07-22 18:02:50 +0200339 # Introduce invalid utf-8 characters
340 my $text_sigle;
Akron54e363c2020-07-29 11:01:03 +0200341 {
342 no warnings;
343 # $text_sigle printed to file, without encoding: Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C
344 # the utf8-sequence 'þƒ¿¿¿¿¿' encodes 32 bit of data (see 0x7FFF_FFFF in perlunicode)
345 $text_sigle = "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B.C\x{FFFF_FFFF}C"
346 }
347 # If CHECK is 0, encoding and decoding replace any malformed character
348 # with a substitution character.
Peter Harders1c5ce152020-07-22 18:02:50 +0200349 # � = substitution character
350 my $text_sigle_lax = encode_utf8($text_sigle);
351 my $text_sigle_esc = encode('UTF-8', $text_sigle);
352
353 is(length($text_sigle), 11); # A�A_B�B.C�C (char string => length(�) = 1)
354 is(length($text_sigle_lax), 29); # Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C (byte string)
355 is(length($text_sigle_esc), 17); # A�A_B�B.C�C (byte string => length(�) = 3)
356
Akron54e363c2020-07-29 11:01:03 +0200357
358 my $tpl;
359 {
360 no warnings;
361 $tpl = i5_template(
362 korpusSigle => "A\x{FFFF_FFFF}A",
363 dokumentSigle => "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B",
364 text => "<p>d\x{FFFF_FFFF}d e\x{FFFF_FFFF}e f\x{FFFF_FFFF}f</p>",
365 textSigle => $text_sigle
366 );
367 };
Peter Harders1c5ce152020-07-22 18:02:50 +0200368
369 my ($fh, $tplfile) = korap_tempfile('script_out4');
370 binmode($fh);
371 print $fh encode_utf8($tpl); # => text_id=Aþƒ¿¿¿¿¿A_Bþƒ¿¿¿¿¿B.Cþƒ¿¿¿¿¿C
372 close($fh);
373
374 my (undef, $outzip) = korap_tempfile('script_out5');
375
Akron54e363c2020-07-29 11:01:03 +0200376 # because output 'textid=...' goes to STDERR (see script/tei2korapxml)
Akroneaa96232020-10-15 17:06:15 +0200377 binmode STDERR;
Peter Harders1c5ce152020-07-22 18:02:50 +0200378
379 stderr_like(
380 sub { `cat '$tplfile' | perl '$script' -ti > '$outzip'` },
Akron91577922021-02-19 10:32:54 +0100381 qr!tei2korapxml:.*? text_id=$text_sigle_esc!, # see above: print $fh encode_utf8($tpl);
Peter Harders1c5ce152020-07-22 18:02:50 +0200382 );
383};
384
Akrone68ec0c2020-07-28 18:06:19 +0200385
Akrondd0be8f2021-02-18 19:29:41 +0100386subtest 'Check structure parsing with defined foundry and folder' => sub {
387 # Load example file
388 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
389
390 my $t = test_tei2korapxml(
391 tmp => 'script_out',
392 file => $file,
393 param => '-ti --inline-structures=myfoundry#mystr'
Akron91705d72021-02-19 10:59:45 +0100394 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akrondd0be8f2021-02-18 19:29:41 +0100395 ->file_exists_not('GOE/AGA/00000/struct/structure.xml', 'Structure not generated')
396 ->unzip_xml('GOE/AGA/00000/myfoundry/mystr.xml')
397 ->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content')
398 ->text_is('#s3 *[name=type]', 'Autobiographie', 'text content')
399 ->attr_is('#s0','to','1266')
400 ->attr_is('#s0','l','1')
401 ->attr_is('#s18','from','925')
402 ->attr_is('#s18','to','1266')
403 ->attr_is('#s18','l','5')
404 ;
405
406 $t = test_tei2korapxml(
407 tmp => 'script_out',
408 file => $file,
409 param => '-ti --inline-structures=myfoundry'
Akron91705d72021-02-19 10:59:45 +0100410 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akrondd0be8f2021-02-18 19:29:41 +0100411 ->file_exists_not('GOE/AGA/00000/struct/structure.xml', 'Structure not generated')
412 ->unzip_xml('GOE/AGA/00000/myfoundry/structure.xml')
413 ->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content')
414 ->text_is('#s3 *[name=type]', 'Autobiographie', 'text content')
415 ->attr_is('#s0','to','1266')
416 ->attr_is('#s0','l','1')
417 ->attr_is('#s18','from','925')
418 ->attr_is('#s18','to','1266')
419 ->attr_is('#s18','l','5')
420 ;
421};
422
Akron54c3ff12021-02-25 11:33:37 +0100423subtest 'Check structure parsing with skipped tags' => sub {
424 # Load example file
425 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
426
427 my $t = test_tei2korapxml(
428 tmp => 'script_out',
429 file => $file,
430 param => '-ti'
431 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
432 ->file_exists('GOE/AGA/00000/struct/structure.xml', 'Structure generated')
433 ->unzip_xml('GOE/AGA/00000/struct/structure.xml')
434 ->text_is('layer spanList span fs f', 'text')
435 ->text_is('#s5 fs f[name=name]','head')
436 ->text_is('#s6 fs f[name=name]','s')
437 ->text_is('#s7 fs f[name=name]','head')
438 ->text_is('#s8 fs f[name=name]','s')
439 ->text_is('#s9 fs f[name=name]','quote')
440 ->text_is('#s10 fs f[name=name]','s')
441 ;
442
443 $t = test_tei2korapxml(
444 tmp => 'script_out',
445 file => $file,
446 param => '-ti --skip-inline-tags=head'
447 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
448 ->file_exists('GOE/AGA/00000/struct/structure.xml', 'Structure generated')
449 ->unzip_xml('GOE/AGA/00000/struct/structure.xml')
450 ->text_is('layer spanList span fs f', 'text')
451 ->text_is('#s5 fs f[name=name]','s')
452 ->text_is('#s6 fs f[name=name]','s')
453 ->text_is('#s7 fs f[name=name]','quote')
454 ->text_is('#s8 fs f[name=name]','s')
455 ;
456
457 $t = test_tei2korapxml(
458 tmp => 'script_out',
459 file => $file,
460 param => '-ti --skip-inline-tags=head,quote'
461 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
462 ->file_exists('GOE/AGA/00000/struct/structure.xml', 'Structure generated')
463 ->unzip_xml('GOE/AGA/00000/struct/structure.xml')
464 ->text_is('layer spanList span fs f', 'text')
465 ->text_is('#s5 fs f[name=name]','s')
466 ->text_is('#s6 fs f[name=name]','s')
467 ->text_is('#s7 fs f[name=name]','s')
468 ;
469};
470
Akrondd0be8f2021-02-18 19:29:41 +0100471
Akron75d63142021-02-23 18:40:56 +0100472subtest 'Check parsing but skip inline tokens' => sub {
473 # Load example file
474 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
475
476 my $t = test_tei2korapxml(
477 tmp => 'script_skip_inline_tokens_1',
478 file => $file,
479 param => '-ti --skip-inline-tokens'
480 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
481 ->file_exists('GOE/AGA/00000/data.xml', 'Data exists')
482 ->file_exists('GOE/AGA/00000/struct/structure.xml', 'Structure generated')
483 ->file_exists_not('GOE/AGA/00000/tokens/morpho.xml', 'Morpho not generated')
484 ;
485
486 $t = test_tei2korapxml(
487 tmp => 'script_skip_inline_tokens_2',
488 file => $file,
489 param => '-ti --skip-inline-tokens --inline-tokens=myfoundry#myfile'
490 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
491 ->file_exists('GOE/AGA/00000/struct/structure.xml', 'Structure generated')
492 ->file_exists_not('GOE/AGA/00000/tokens/morpho.xml', 'Morpho not generated')
493 ->file_exists_not('GOE/AGA/00000/myfoundry/myfile.xml', 'Morpho not generated')
494 ;
495};
496
497
Akrone68ec0c2020-07-28 18:06:19 +0200498subtest 'Check Inline annotations' => sub {
499
500 # Load example file
501 my $file = catfile($f, 'data', 'goe_sample_tagged.i5.xml');
502
Akron044bd922020-07-30 09:12:28 +0200503 my $t = test_tei2korapxml(
504 file => $file,
505 env => 'KORAPXMLTEI_INLINE=1',
506 tmp => 'script_tagged'
507 )
Akron91577922021-02-19 10:32:54 +0100508 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akrone68ec0c2020-07-28 18:06:19 +0200509
Akron044bd922020-07-30 09:12:28 +0200510 # Check zip using xml loy
511 ->unzip_xml('GOE/AGA/00000/tokens/morpho.xml')
Akrone68ec0c2020-07-28 18:06:19 +0200512
Akron044bd922020-07-30 09:12:28 +0200513 ->attr_is('layer', 'docid', 'GOE_AGA.00000')
Akrone68ec0c2020-07-28 18:06:19 +0200514 ->attr_is('spanList span:nth-child(1)', 'id', 's0')
515 ->attr_is('spanList span:nth-child(1)', 'from', '75')
516 ->attr_is('spanList span:nth-child(1)', 'to', '81')
517 ->attr_is('spanList span:nth-child(1)', 'l', '7')
518
519 ->attr_is('span#s0 > fs', 'type', 'lex')
520 ->attr_is('span#s0 > fs', 'xmlns', 'http://www.tei-c.org/ns/1.0')
521 ->attr_is('span#s0 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
522 ->text_is('span#s0 > fs > f > fs > f:nth-child(1)', 'A')
523 ->attr_is('span#s0 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
524 ->text_is('span#s0 > fs > f > fs > f:nth-child(2)', '@NH')
525
526 ->attr_is('span#s25', 'from', '259')
527 ->attr_is('span#s25', 'to', '263')
528 ->attr_is('span#s25', 'l', '7')
529 ->attr_is('span#s25 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
530 ->text_is('span#s25 > fs > f > fs > f:nth-child(1)', 'PRON')
531 ->attr_is('span#s25 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
532 ->text_is('span#s25 > fs > f > fs > f:nth-child(2)', '@NH')
533
534 ->attr_is('span#s58', 'from', '495')
535 ->attr_is('span#s58', 'to', '500')
536 ->attr_is('span#s58', 'l', '7')
537 ->attr_is('span#s58 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
538 ->text_is('span#s58 > fs > f > fs > f:nth-child(1)', 'N')
539 ->attr_is('span#s58 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
540 ->text_is('span#s58 > fs > f > fs > f:nth-child(2)', '@NH')
541
542 ->attr_is('span#s119', 'from', '914')
543 ->attr_is('span#s119', 'to', '925')
544 ->attr_is('span#s119', 'l', '7')
545 ->attr_is('span#s119 > fs > f > fs > f:nth-child(1)', 'name', 'pos')
546 ->text_is('span#s119 > fs > f > fs > f:nth-child(1)', 'A')
547 ->attr_is('span#s119 > fs > f > fs > f:nth-child(2)', 'name', 'msd')
548 ->text_is('span#s119 > fs > f > fs > f:nth-child(2)', '@NH')
549 ->element_exists_not('span#s120')
550 ;
551};
552
Akron26a71522021-02-19 10:27:37 +0100553
554subtest 'Check file structure with defined folder and filenames' => sub {
555 # Load example file
556 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
557 my $t = test_tei2korapxml(
558 tmp => 'script_out',
559 file => $file,
560 param => '-ti --base-foundry=root --data-file=primary --header-file=meta'
Akron91705d72021-02-19 10:59:45 +0100561 )->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akron26a71522021-02-19 10:27:37 +0100562 ->file_exists_not('GOE/AGA/00000/header.xml', 'Header not there')
563 ->file_exists_not('GOE/AGA/header.xml', 'Header not there')
564 ->file_exists_not('GOE/header.xml', 'Header not there')
565 ->file_exists_not('GOE/AGA/00000/data.xml', 'Data not there')
566 ->file_exists_not('GOE/AGA/00000/base/tokens_conservative.xml', 'Tokens not there')
567 ->file_exists_not('GOE/AGA/00000/base/tokens_aggressive.xml', 'Tokens not there')
568 ->file_exists('GOE/AGA/00000/meta.xml', 'Header there')
569 ->file_exists('GOE/AGA/meta.xml', 'Header there')
570 ->file_exists('GOE/meta.xml', 'Header there')
571 ->file_exists('GOE/AGA/00000/primary.xml', 'Data there')
572 ->file_exists('GOE/AGA/00000/root/tokens_conservative.xml', 'Tokens there')
573 ->file_exists('GOE/AGA/00000/root/tokens_aggressive.xml', 'Tokens there')
574 ;
575
576 $t->unzip_xml('GOE/AGA/00000/primary.xml')
577 ->content_like(qr/\Q&quot;Kriegstheater&quot;\E/)
578 ;
579};
580
Akron1a5271a2021-02-18 13:18:15 +0100581subtest 'Check Inline annotations with defined foundry and folder' => sub {
582 # Load example file
583 my $file = catfile($f, 'data', 'goe_sample_tagged.i5.xml');
584
585 my $t = test_tei2korapxml(
586 file => $file,
587 env => 'KORAPXMLTEI_INLINE=1',
588 tmp => 'script_tagged',
589 param => '--inline-tokens=myfoundry#myfile'
590 )
Akron91577922021-02-19 10:32:54 +0100591 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akron1a5271a2021-02-18 13:18:15 +0100592
593 ->file_exists_not('GOE/AGA/00000/tokens/morpho.xml', 'Morpho not generated')
594
595 # Check zip using xml loy
596 ->unzip_xml('GOE/AGA/00000/myfoundry/myfile.xml')
597
598 ->attr_is('layer', 'docid', 'GOE_AGA.00000')
599 ->attr_is('spanList span:nth-child(1)', 'id', 's0')
600 ->attr_is('spanList span:nth-child(1)', 'from', '75')
601 ->attr_is('spanList span:nth-child(1)', 'to', '81')
602 ->attr_is('spanList span:nth-child(1)', 'l', '7')
603 ;
604
605 $t = test_tei2korapxml(
606 file => $file,
607 env => 'KORAPXMLTEI_INLINE=1',
608 tmp => 'script_tagged',
609 param => '--inline-tokens=myfoundry'
610 )
Akron91577922021-02-19 10:32:54 +0100611 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akron1a5271a2021-02-18 13:18:15 +0100612
613 ->file_exists_not('GOE/AGA/00000/tokens/morpho.xml', 'Morpho not generated')
614
615 # Check zip using xml loy
616 ->unzip_xml('GOE/AGA/00000/myfoundry/morpho.xml')
617
618 ->attr_is('layer', 'docid', 'GOE_AGA.00000')
619 ->attr_is('spanList span:nth-child(1)', 'id', 's0')
620 ->attr_is('spanList span:nth-child(1)', 'from', '75')
621 ->attr_is('spanList span:nth-child(1)', 'to', '81')
622 ->attr_is('spanList span:nth-child(1)', 'l', '7')
623 ;
624};
625
Akron09e0b2c2020-07-28 15:57:01 +0200626subtest 'Check Inline annotations with untagged file' => sub {
627
628 # Load example file
629 my $file = catfile($f, 'data', 'goe_sample.i5.xml');
630
631 my ($fh, $outzip) = korap_tempfile('script_untagged');
632
633 # Generate zip file (unportable!)
634 stderr_like(
635 sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` },
Akron91577922021-02-19 10:32:54 +0100636 qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!,
Akron09e0b2c2020-07-28 15:57:01 +0200637 'Processing 1'
638 );
639
640 # TODO: there should be a better way to test this
641 stderr_unlike(
642 sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` },
643 qr!.*undefined value.*!,
644 'Processing 2'
645 );
646 #
647
648 ok(-e $outzip, "File $outzip exists");
649
650 my $zip = IO::Uncompress::Unzip->new(
651 $outzip,
652 Name => 'GOE/AGA/00000/tokens/morpho.xml'
653 );
654 ok((not $zip), 'missing morpho.xml');
655
656 $zip = IO::Uncompress::Unzip->new(
657 $outzip,
658 Name => 'GOE/AGA/00000/struct/structure.xml'
659 );
660 ok($zip, 'found structure.xml');
661};
Akrone68ec0c2020-07-28 18:06:19 +0200662
Akron3378dfd2020-08-01 15:01:36 +0200663
Akroneaa96232020-10-15 17:06:15 +0200664subtest 'Check input encoding' => sub {
665
666 # Load example file
667 test_tei2korapxml(
668 file => catfile($f, 'data', 'goe_sample.i5.xml'),
669 env => 'KORAPXMLTEI_INLINE=1',
670 tmp => 'script_utf8_enc'
671 )
Akron91577922021-02-19 10:32:54 +0100672 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akroneaa96232020-10-15 17:06:15 +0200673 ->unzip_xml('GOE/AGA/00000/data.xml')
674 ->content_like(qr/\Q&quot;Kriegstheater&quot;\E/)
675 ->content_like(qr/\QTür&#39;\E/)
676 ;
677
678 test_tei2korapxml(
679 file => catfile($f, 'data', 'goe_sample.i5.iso.xml'),
680 env => 'KORAPXMLTEI_INLINE=1',
681 tmp => 'script_iso_enc'
682 )
Akron91577922021-02-19 10:32:54 +0100683 ->stderr_like(qr!tei2korapxml:.*? text_id=GOE_AGA\.00000!)
Akroneaa96232020-10-15 17:06:15 +0200684 ->unzip_xml('GOE/AGA/00000/data.xml')
685 ->content_like(qr/\Q&quot;Kriegstheater&quot;\E/)
686 ->content_like(qr/\QTür&#39;\E/)
687 ;
688};
689
Marc Kupietz44b1f252020-11-26 16:31:40 +0100690subtest 'Check encoding with utf-8 sigle' => sub {
691
692 # Load example file
693 my $file = catfile($f, 'data', 'wdd_sample.i5.xml');
694
695 my $t = test_tei2korapxml(
696 tmp => 'script_sigle',
697 file => $file,
698 param => "-ti"
Akron91577922021-02-19 10:32:54 +0100699 )->stderr_like(qr!tei2korapxml:.*? text_id=WDD19_ß0000\.10317!)
Marc Kupietz44b1f252020-11-26 16:31:40 +0100700 ->stderr_unlike(qr!Debugging is activated!);
701
702 $t->unzip_xml('WDD19/ß0000/10317/header.xml')
703 ->text_is('idsHeader fileDesc titleStmt textSigle', 'WDD19/ß0000.10317');
704
705 $t->unzip_xml('WDD19/ß0000/10317/data.xml')
706 ->attr_is('raw_text', 'docid', 'WDD19_ß0000.10317');
707
708 $t->unzip_xml('WDD19/ß0000/10317/struct/structure.xml')
709 ->attr_is('layer', 'docid', 'WDD19_ß0000.10317');
710
711 $t->unzip_xml('WDD19/ß0000/10317/base/tokens_conservative.xml')
712 ->attr_is('layer', 'docid', 'WDD19_ß0000.10317');
713};
714
Marc Kupietz8a954e52021-02-16 22:03:07 +0100715subtest 'Check entity replacement' => sub {
716 my $t = test_tei2korapxml(
717 file => catfile($f, 'data', 'text_with_entities.i5.xml'),
718 tmp => 'script_entity_replacement',
719 param => '-ti'
Akron91577922021-02-19 10:32:54 +0100720 )->stderr_like(qr!tei2korapxml:.*? text_id=CORP_DOC.00003!);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100721
722 $t->unzip_xml('CORP/DOC/00003/data.xml')
723 ->content_like(qr!üüü Aα≈„▒░▓█╗┐┌╔═─┬╦┴╩╝┘└╚│║┼╬┤╣╠├•ˇčˆ†‡ě€ƒ…‗ıι“„▄‹‘‚—–νœŒωΩ‰φπϖř”ρ›’‘šŠσ□■▪⊂˜™▀ŸžŽ!);
724
725 $t->unzip_xml('CORP/DOC/00003/header.xml')
726 ->content_like(qr!üüü x α•α y!);
727};
728
Akron3378dfd2020-08-01 15:01:36 +0200729subtest 'Test Log' => sub {
730 test_tei2korapxml(
731 tmp => 'script_out',
732 file => $file,
733 param => '-l=warn'
734 )->stderr_is('');
735};
736
737
Akron797e8072020-02-13 07:59:40 +0100738done_testing;