Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use File::Basename 'dirname'; |
| 4 | use File::Spec::Functions qw/catfile/; |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 5 | use Encode qw!encode_utf8 decode_utf8 encode!; |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 6 | |
| 7 | use Test::More; |
| 8 | use Test::Output; |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 9 | |
| 10 | use FindBin; |
| 11 | BEGIN { |
| 12 | unshift @INC, "$FindBin::Bin/../lib"; |
| 13 | }; |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 14 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 15 | use Test::KorAP::XML::TEI qw!korap_tempfile i5_template test_tei2korapxml!; |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame] | 16 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 17 | my $f = dirname(__FILE__); |
| 18 | my $script = catfile($f, '..', 'script', 'tei2korapxml'); |
| 19 | ok(-f $script, 'Script found'); |
| 20 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 21 | stdout_like( |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 22 | sub { system('perl', $script, '--help') }, |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 23 | qr!This\s*program\s*is\s*usually\s*called\s*from\s*inside\s*another\s*script\.!, |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 24 | 'Help' |
| 25 | ); |
| 26 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 27 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 28 | stdout_like( |
| 29 | sub { system('perl', $script, '--version') }, |
| 30 | qr!tei2korapxml - v\d+?\.\d+?!, |
| 31 | 'Version' |
| 32 | ); |
| 33 | |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 34 | # Load example file |
| 35 | my $file = catfile($f, 'data', 'goe_sample.i5.xml'); |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame] | 36 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 37 | subtest 'Basic processing' => sub { |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 38 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 39 | my $t = test_tei2korapxml( |
| 40 | tmp => 'script_out', |
| 41 | file => $file, |
| 42 | param => '-ti' |
| 43 | )->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 44 | |
Akron | 6896608 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 45 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 46 | # Uncompress GOE/header.xml from zip file |
| 47 | $t->unzip_xml('GOE/header.xml') |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 48 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 49 | # 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'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 54 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 55 | # 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'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 60 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 61 | # 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'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 65 | |
| 66 | # Uncompress GOE/AGA/00000/data.xml from zip file |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 67 | $t->unzip_xml('GOE/AGA/00000/data.xml') |
| 68 | ->attr_is('raw_text', 'docid', 'GOE_AGA.00000', 'text id') |
Marc Kupietz | fd0e6a9 | 2020-09-09 18:07:29 +0200 | [diff] [blame] | 69 | ->text_like('raw_text > text', qr!^Campagne in Frankreich 1792.*?uns allein begl.*cke\.$!, 'text content') |
| 70 | ->text_like('raw_text > text', qr!unter dem Titel "Kriegstheater"!, 'text content'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 71 | |
Akron | 417ed2f | 2020-09-28 12:43:17 +0200 | [diff] [blame^] | 72 | my $content = $t->get_content_of('GOE/AGA/00000/data.xml'); |
| 73 | like($content, qr!unter dem Titel "Kriegstheater"!, 'raw text content'); |
| 74 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 75 | $t->unzip_xml('GOE/AGA/00000/struct/structure.xml') |
Akron | c57625b | 2020-07-30 11:46:06 +0200 | [diff] [blame] | 76 | ->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content') |
| 77 | ->text_is('#s3 *[name=type]', 'Autobiographie', 'text content') |
| 78 | ->attr_is('#s0','to','1266') |
| 79 | ->attr_is('#s0','l','1') |
| 80 | |
| 81 | ->attr_is('#s18','from','925') |
| 82 | ->attr_is('#s18','to','1266') |
| 83 | ->attr_is('#s18','l','5') |
| 84 | ->attr_is('#s18 > fs','type', 'struct') |
| 85 | ->attr_is('#s18 > fs > f','name','name') |
| 86 | ->text_is('#s18 > fs > f','poem') |
| 87 | |
| 88 | ->attr_is('#s19','from','925') |
| 89 | ->attr_is('#s19','to','1098') |
| 90 | ->attr_is('#s19','l','6') |
| 91 | ->attr_is('#s19 > fs','type','struct') |
| 92 | ->text_is('#s19 > fs > f[name=name]','lg') |
| 93 | ->text_is('#s19 > fs > f[name=attr] > fs[type=attr] >f[name=part]','u') |
| 94 | |
| 95 | ->attr_is('#s37','from','1229') |
| 96 | ->attr_is('#s37','to','1266') |
| 97 | ->attr_is('#s37','l','8') |
| 98 | ->attr_is('#s37 > fs','type','struct') |
| 99 | ->text_is('#s37 > fs > f[name=name]','s') |
| 100 | ->text_is('#s37 > fs > f[name=attr] > fs[type=attr] > f[name=type]','manual') |
| 101 | |
| 102 | ->attr_is('#s38','from','1266') |
| 103 | ->attr_is('#s38','to','1266') |
| 104 | ->attr_is('#s38','l','2') |
| 105 | ->attr_is('#s38 > fs','type','struct') |
| 106 | ->text_is('#s38 > fs > f[name=name]','back') |
| 107 | ->element_count_is('', 196); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 108 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 109 | $t->file_exists_not('GOE/AGA/00000/base/tokens.xml', 'External not generated'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 110 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 111 | # Uncompress GOE/AGA/00000/base/tokens_aggressive.xml from zip file |
| 112 | $t->unzip_xml('GOE/AGA/00000/base/tokens_aggressive.xml') |
| 113 | ->attr_is('spanList span:nth-child(1)', 'to', 8) |
| 114 | ->attr_is('spanList span#t_1', 'from', 9) |
| 115 | ->attr_is('spanList span#t_1', 'to', 11) |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 116 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 117 | ->attr_is('spanList span#t_67', 'from', 427) |
| 118 | ->attr_is('spanList span#t_67', 'to', 430) |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 119 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 120 | ->attr_is('spanList span#t_214', 'from', 1209) |
| 121 | ->attr_is('spanList span#t_214', 'to', 1212) |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 122 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 123 | ->element_count_is('spanList span', 227); |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame] | 124 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 125 | # Uncompress GOE/AGA/00000/base/tokens_conservative.xml from zip file |
| 126 | $t->unzip_xml('GOE/AGA/00000/base/tokens_conservative.xml') |
| 127 | ->attr_is('spanList span:nth-child(1)', 'to', 8) |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 128 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 129 | ->attr_is('spanList span#t_1', 'from', 9) |
| 130 | ->attr_is('spanList span#t_1', 'to', 11) |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 131 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 132 | ->attr_is('spanList span#t_67', 'from', 427) |
| 133 | ->attr_is('spanList span#t_67', 'to', 430) |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 134 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 135 | ->attr_is('spanList span#t_214', 'from', 1209) |
| 136 | ->attr_is('spanList span#t_214', 'to', 1212) |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 137 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 138 | ->element_count_is('spanList span', 227); |
| 139 | }; |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 140 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 141 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 142 | subtest 'Tokenize with external tokenizer' => sub { |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 143 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 144 | my $cmd = catfile($f, 'cmd', 'tokenizer.pl'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 145 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 146 | test_tei2korapxml( |
| 147 | file => $file, |
| 148 | param => "-tc='perl $cmd'", |
| 149 | tmp => 'script_out2' |
| 150 | ) |
| 151 | ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!) |
| 152 | ->file_readable('GOE/AGA/00000/base/tokens.xml') |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 153 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 154 | # Uncompress GOE/AGA/00000/base/tokens.xml from zip file |
| 155 | ->unzip_xml('GOE/AGA/00000/base/tokens.xml') |
| 156 | ->attr_is('spanList span:nth-child(1)', 'to', 8) |
| 157 | ->attr_is('spanList span#t_1', 'from', 9) |
| 158 | ->attr_is('spanList span#t_1', 'to', 11) |
| 159 | ->attr_is('spanList span#t_67', 'from', 427) |
| 160 | ->attr_is('spanList span#t_67', 'to', 430) |
| 161 | ->attr_is('spanList span#t_214', 'from', 1209) |
| 162 | ->attr_is('spanList span#t_214', 'to', 1212) |
| 163 | ->element_count_is('spanList span', 227); |
| 164 | }; |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 165 | |
| 166 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 167 | subtest 'Test Tokenizations' => sub { |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 168 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 169 | my $t = test_tei2korapxml( |
| 170 | file => catfile($f, 'data', 'text_with_blanks.i5.xml'), |
| 171 | tmp => 'script_out3', |
| 172 | param => '-ti' |
| 173 | )->stderr_like(qr!tei2korapxml: .*? text_id=CORP_DOC.00001!); |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 174 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 175 | # ~ test conservative tokenization ~ |
| 176 | $t->unzip_xml('CORP/DOC/00001/base/tokens_conservative.xml') |
| 177 | ->attr_is('spanList span:nth-child(1)', 'to', 6) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 178 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 179 | ->attr_is('spanList span#t_1', 'from', 7) |
| 180 | ->attr_is('spanList span#t_1', 'to', 9) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 181 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 182 | ->attr_is('spanList span#t_3', 'from', 12) |
| 183 | ->attr_is('spanList span#t_3', 'to', 16) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 184 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 185 | ->attr_is('spanList span#t_9', 'from', 36) |
| 186 | ->attr_is('spanList span#t_9', 'to', 37) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 187 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 188 | ->attr_is('spanList span#t_13', 'from', 44) |
| 189 | ->attr_is('spanList span#t_13', 'to', 45) # " |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 190 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 191 | ->attr_is('spanList span#t_14', 'from', 45) # twenty-two |
| 192 | ->attr_is('spanList span#t_14', 'to', 55) |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 193 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 194 | ->attr_is('spanList span#t_15', 'from', 55) # " |
| 195 | ->attr_is('spanList span#t_15', 'to', 56) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 196 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 197 | ->attr_is('spanList span#t_19', 'from', 66) |
| 198 | ->attr_is('spanList span#t_19', 'to', 67) |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 199 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 200 | ->element_count_is('spanList span', 20); |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 201 | |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 202 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 203 | # ~ test aggressive tokenization ~ |
| 204 | $t->unzip_xml('CORP/DOC/00001/base/tokens_aggressive.xml') |
| 205 | ->attr_is('spanList span:nth-child(1)', 'to', 6) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 206 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 207 | ->attr_is('spanList span#t_1', 'from', 7) |
| 208 | ->attr_is('spanList span#t_1', 'to', 9) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 209 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 210 | ->attr_is('spanList span#t_3', 'from', 12) |
| 211 | ->attr_is('spanList span#t_3', 'to', 16) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 212 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 213 | ->attr_is('spanList span#t_9', 'from', 36) |
| 214 | ->attr_is('spanList span#t_9', 'to', 37) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 215 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 216 | ->attr_is('spanList span#t_13', 'from', 44) |
| 217 | ->attr_is('spanList span#t_13', 'to', 45) # " |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 218 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 219 | ->attr_is('spanList span#t_14', 'from', 45) # twenty |
| 220 | ->attr_is('spanList span#t_14', 'to', 51) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 221 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 222 | ->attr_is('spanList span#t_15', 'from', 51) # - |
| 223 | ->attr_is('spanList span#t_15', 'to', 52) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 224 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 225 | ->attr_is('spanList span#t_16', 'from', 52) # two |
| 226 | ->attr_is('spanList span#t_16', 'to', 55) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 227 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 228 | ->attr_is('spanList span#t_17', 'from', 55) # " |
| 229 | ->attr_is('spanList span#t_17', 'to', 56) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 230 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 231 | ->attr_is('spanList span#t_21', 'from', 66) |
| 232 | ->attr_is('spanList span#t_21', 'to', 67) |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 233 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 234 | ->element_count_is('spanList span', 22); |
| 235 | }; |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 236 | |
| 237 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 238 | subtest 'Check Tokenization Flags' => sub { |
| 239 | |
| 240 | # Get external tokenizer |
| 241 | my $f = dirname(__FILE__); |
| 242 | my $cmd = catfile($f, 'cmd', 'tokenizer.pl'); |
| 243 | |
| 244 | # Load example file |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 245 | test_tei2korapxml( |
| 246 | file => catfile($f, 'data', 'goe_sample.i5.xml'), |
| 247 | param => "-ti -tc 'perl $cmd'", |
| 248 | tmp => 'script_tokflags' |
| 249 | ) |
| 250 | ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!) |
| 251 | ->file_exists('GOE/AGA/00000/base/tokens_aggressive.xml') |
| 252 | ->file_exists('GOE/AGA/00000/base/tokens_conservative.xml') |
| 253 | ->file_exists('GOE/AGA/00000/base/tokens.xml') |
| 254 | ; |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 255 | }; |
| 256 | |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 257 | |
| 258 | subtest 'Test utf-8 handling' => sub { |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 259 | # Introduce invalid utf-8 characters |
| 260 | my $text_sigle; |
Akron | 54e363c | 2020-07-29 11:01:03 +0200 | [diff] [blame] | 261 | { |
| 262 | no warnings; |
| 263 | # $text_sigle printed to file, without encoding: Aþ¿¿¿¿¿A_Bþ¿¿¿¿¿B.Cþ¿¿¿¿¿C |
| 264 | # the utf8-sequence 'þ¿¿¿¿¿' encodes 32 bit of data (see 0x7FFF_FFFF in perlunicode) |
| 265 | $text_sigle = "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B.C\x{FFFF_FFFF}C" |
| 266 | } |
| 267 | # If CHECK is 0, encoding and decoding replace any malformed character |
| 268 | # with a substitution character. |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 269 | # � = substitution character |
| 270 | my $text_sigle_lax = encode_utf8($text_sigle); |
| 271 | my $text_sigle_esc = encode('UTF-8', $text_sigle); |
| 272 | |
| 273 | is(length($text_sigle), 11); # A�A_B�B.C�C (char string => length(�) = 1) |
| 274 | is(length($text_sigle_lax), 29); # Aþ¿¿¿¿¿A_Bþ¿¿¿¿¿B.Cþ¿¿¿¿¿C (byte string) |
| 275 | is(length($text_sigle_esc), 17); # A�A_B�B.C�C (byte string => length(�) = 3) |
| 276 | |
Akron | 54e363c | 2020-07-29 11:01:03 +0200 | [diff] [blame] | 277 | |
| 278 | my $tpl; |
| 279 | { |
| 280 | no warnings; |
| 281 | $tpl = i5_template( |
| 282 | korpusSigle => "A\x{FFFF_FFFF}A", |
| 283 | dokumentSigle => "A\x{FFFF_FFFF}A_B\x{FFFF_FFFF}B", |
| 284 | text => "<p>d\x{FFFF_FFFF}d e\x{FFFF_FFFF}e f\x{FFFF_FFFF}f</p>", |
| 285 | textSigle => $text_sigle |
| 286 | ); |
| 287 | }; |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 288 | |
| 289 | my ($fh, $tplfile) = korap_tempfile('script_out4'); |
| 290 | binmode($fh); |
| 291 | print $fh encode_utf8($tpl); # => text_id=Aþ¿¿¿¿¿A_Bþ¿¿¿¿¿B.Cþ¿¿¿¿¿C |
| 292 | close($fh); |
| 293 | |
| 294 | my (undef, $outzip) = korap_tempfile('script_out5'); |
| 295 | |
Akron | 54e363c | 2020-07-29 11:01:03 +0200 | [diff] [blame] | 296 | # because output 'textid=...' goes to STDERR (see script/tei2korapxml) |
| 297 | binmode STDERR, qw{ :encoding(UTF-8) }; |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 298 | |
| 299 | stderr_like( |
| 300 | sub { `cat '$tplfile' | perl '$script' -ti > '$outzip'` }, |
| 301 | qr!tei2korapxml: .*? text_id=$text_sigle_lax!, # see above: print $fh encode_utf8($tpl); |
| 302 | ); |
| 303 | }; |
| 304 | |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 305 | |
| 306 | subtest 'Check Inline annotations' => sub { |
| 307 | |
| 308 | # Load example file |
| 309 | my $file = catfile($f, 'data', 'goe_sample_tagged.i5.xml'); |
| 310 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 311 | my $t = test_tei2korapxml( |
| 312 | file => $file, |
| 313 | env => 'KORAPXMLTEI_INLINE=1', |
| 314 | tmp => 'script_tagged' |
| 315 | ) |
| 316 | ->stderr_like(qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!) |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 317 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 318 | # Check zip using xml loy |
| 319 | ->unzip_xml('GOE/AGA/00000/tokens/morpho.xml') |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 320 | |
Akron | 044bd92 | 2020-07-30 09:12:28 +0200 | [diff] [blame] | 321 | ->attr_is('layer', 'docid', 'GOE_AGA.00000') |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 322 | ->attr_is('spanList span:nth-child(1)', 'id', 's0') |
| 323 | ->attr_is('spanList span:nth-child(1)', 'from', '75') |
| 324 | ->attr_is('spanList span:nth-child(1)', 'to', '81') |
| 325 | ->attr_is('spanList span:nth-child(1)', 'l', '7') |
| 326 | |
| 327 | ->attr_is('span#s0 > fs', 'type', 'lex') |
| 328 | ->attr_is('span#s0 > fs', 'xmlns', 'http://www.tei-c.org/ns/1.0') |
| 329 | ->attr_is('span#s0 > fs > f > fs > f:nth-child(1)', 'name', 'pos') |
| 330 | ->text_is('span#s0 > fs > f > fs > f:nth-child(1)', 'A') |
| 331 | ->attr_is('span#s0 > fs > f > fs > f:nth-child(2)', 'name', 'msd') |
| 332 | ->text_is('span#s0 > fs > f > fs > f:nth-child(2)', '@NH') |
| 333 | |
| 334 | ->attr_is('span#s25', 'from', '259') |
| 335 | ->attr_is('span#s25', 'to', '263') |
| 336 | ->attr_is('span#s25', 'l', '7') |
| 337 | ->attr_is('span#s25 > fs > f > fs > f:nth-child(1)', 'name', 'pos') |
| 338 | ->text_is('span#s25 > fs > f > fs > f:nth-child(1)', 'PRON') |
| 339 | ->attr_is('span#s25 > fs > f > fs > f:nth-child(2)', 'name', 'msd') |
| 340 | ->text_is('span#s25 > fs > f > fs > f:nth-child(2)', '@NH') |
| 341 | |
| 342 | ->attr_is('span#s58', 'from', '495') |
| 343 | ->attr_is('span#s58', 'to', '500') |
| 344 | ->attr_is('span#s58', 'l', '7') |
| 345 | ->attr_is('span#s58 > fs > f > fs > f:nth-child(1)', 'name', 'pos') |
| 346 | ->text_is('span#s58 > fs > f > fs > f:nth-child(1)', 'N') |
| 347 | ->attr_is('span#s58 > fs > f > fs > f:nth-child(2)', 'name', 'msd') |
| 348 | ->text_is('span#s58 > fs > f > fs > f:nth-child(2)', '@NH') |
| 349 | |
| 350 | ->attr_is('span#s119', 'from', '914') |
| 351 | ->attr_is('span#s119', 'to', '925') |
| 352 | ->attr_is('span#s119', 'l', '7') |
| 353 | ->attr_is('span#s119 > fs > f > fs > f:nth-child(1)', 'name', 'pos') |
| 354 | ->text_is('span#s119 > fs > f > fs > f:nth-child(1)', 'A') |
| 355 | ->attr_is('span#s119 > fs > f > fs > f:nth-child(2)', 'name', 'msd') |
| 356 | ->text_is('span#s119 > fs > f > fs > f:nth-child(2)', '@NH') |
| 357 | ->element_exists_not('span#s120') |
| 358 | ; |
| 359 | }; |
| 360 | |
Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 361 | subtest 'Check Inline annotations with untagged file' => sub { |
| 362 | |
| 363 | # Load example file |
| 364 | my $file = catfile($f, 'data', 'goe_sample.i5.xml'); |
| 365 | |
| 366 | my ($fh, $outzip) = korap_tempfile('script_untagged'); |
| 367 | |
| 368 | # Generate zip file (unportable!) |
| 369 | stderr_like( |
| 370 | sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` }, |
| 371 | qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!, |
| 372 | 'Processing 1' |
| 373 | ); |
| 374 | |
| 375 | # TODO: there should be a better way to test this |
| 376 | stderr_unlike( |
| 377 | sub { `cat '$file' | KORAPXMLTEI_INLINE=1 perl '$script' > '$outzip'` }, |
| 378 | qr!.*undefined value.*!, |
| 379 | 'Processing 2' |
| 380 | ); |
| 381 | # |
| 382 | |
| 383 | ok(-e $outzip, "File $outzip exists"); |
| 384 | |
| 385 | my $zip = IO::Uncompress::Unzip->new( |
| 386 | $outzip, |
| 387 | Name => 'GOE/AGA/00000/tokens/morpho.xml' |
| 388 | ); |
| 389 | ok((not $zip), 'missing morpho.xml'); |
| 390 | |
| 391 | $zip = IO::Uncompress::Unzip->new( |
| 392 | $outzip, |
| 393 | Name => 'GOE/AGA/00000/struct/structure.xml' |
| 394 | ); |
| 395 | ok($zip, 'found structure.xml'); |
| 396 | }; |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 397 | |
Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 398 | |
| 399 | subtest 'Test Log' => sub { |
| 400 | test_tei2korapxml( |
| 401 | tmp => 'script_out', |
| 402 | file => $file, |
| 403 | param => '-l=warn' |
| 404 | )->stderr_is(''); |
| 405 | }; |
| 406 | |
| 407 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 408 | done_testing; |