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 | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 5 | use File::Temp qw/tempfile/; |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 6 | use IO::Uncompress::Unzip qw(unzip $UnzipError); |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 7 | |
| 8 | use Test::More; |
| 9 | use Test::Output; |
| 10 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 11 | use Test::XML::Loy; |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 12 | |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 13 | our %ENV; |
| 14 | # default: remove temp. file created by func. tempfile |
| 15 | # to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t' |
| 16 | my $_UNLINK = $ENV{KORAPXMLTEI_DONTUNLINK}?0:1; |
| 17 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 18 | my $f = dirname(__FILE__); |
| 19 | my $script = catfile($f, '..', 'script', 'tei2korapxml'); |
| 20 | ok(-f $script, 'Script found'); |
| 21 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 22 | stdout_like( |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 23 | sub { system('perl', $script, '--help') }, |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 24 | 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] | 25 | 'Help' |
| 26 | ); |
| 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 | |
| 34 | |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 35 | # Load example file |
| 36 | my $file = catfile($f, 'data', 'goe_sample.i5.xml'); |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 37 | |
| 38 | my ($fh, $outzip) = tempfile("KorAP-XML-TEI_script_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 39 | |
| 40 | # Generate zip file (unportable!) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 41 | # TODO: |
| 42 | # Call with aggressive and conservative tokenizations! |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 43 | stderr_like( |
| 44 | sub { `cat '$file' | perl '$script' > '$outzip'` }, |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 45 | # approaches for working with $fh (also better use OO interface then) |
| 46 | # sub { open STDOUT, '>&', $fh; system("cat '$file' | perl '$script'") }, |
| 47 | # sub { open(my $pipe, "cat '$file' | perl '$script'|"); while(<$pipe>){$fh->print($_)}; $fh->close }, |
| 48 | # sub { |
| 49 | # defined(my $pid = fork) or die "fork: $!"; |
| 50 | # if (!$pid) { |
| 51 | # open STDOUT, '>&', $fh; |
| 52 | # exec "cat '$file' | perl '$script'" |
| 53 | # } |
| 54 | # waitpid $pid, 0; |
| 55 | # $fh->close; |
| 56 | # }, |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 57 | qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!, |
| 58 | 'Processing' |
| 59 | ); |
| 60 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 61 | ok(-e $outzip, "File $outzip exists"); |
| 62 | |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 63 | # Uncompress GOE/header.xml from zip file |
| 64 | my $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/header.xml'); |
| 65 | |
| 66 | ok($zip, 'Zip-File is created'); |
| 67 | |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 68 | # TODO: check wrong encoding in header-files (compare with input document)! |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 69 | # Read GOE/header.xml |
| 70 | my $header_xml = ''; |
| 71 | $header_xml .= $zip->getline while !$zip->eof; |
| 72 | ok($zip->close, 'Closed'); |
| 73 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 74 | my $t = Test::XML::Loy->new($header_xml); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 75 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 76 | $t->text_is('korpusSigle', 'GOE', 'korpusSigle') |
| 77 | ->text_is('h\.title[type=main]', 'Goethes Werke', 'h.title') |
| 78 | ->text_is('h\.author', 'Goethe, Johann Wolfgang von', 'h.author') |
| 79 | ->text_is('pubDate[type=year]', '1982', 'pubDate'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 80 | |
Akron | 6896608 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 81 | |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 82 | # Uncompress GOE/AGA/header.xml from zip file |
| 83 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/header.xml'); |
| 84 | |
| 85 | ok($zip, 'Zip-File is found'); |
| 86 | |
| 87 | # Read GOE/AGA/header.xml |
| 88 | $header_xml = ''; |
| 89 | $header_xml .= $zip->getline while !$zip->eof; |
| 90 | ok($zip->close, 'Closed'); |
| 91 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 92 | $t = Test::XML::Loy->new($header_xml); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 93 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 94 | $t->text_is('dokumentSigle', 'GOE/AGA', 'dokumentSigle') |
| 95 | ->text_is('d\.title', 'Goethe: Autobiographische Schriften II, (1817-1825, 1832)', 'd.title') |
| 96 | ->text_is('creatDate', '1820-1822', 'creatDate'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 97 | |
| 98 | # Uncompress GOE/AGA/00000/header.xml from zip file |
| 99 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/header.xml'); |
| 100 | |
| 101 | ok($zip, 'Zip-File is found'); |
| 102 | |
| 103 | # Read GOE/AGA/00000/header.xml |
| 104 | $header_xml = ''; |
| 105 | $header_xml .= $zip->getline while !$zip->eof; |
| 106 | ok($zip->close, 'Closed'); |
| 107 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 108 | $t = Test::XML::Loy->new($header_xml); |
| 109 | $t->text_is('textSigle', 'GOE/AGA.00000', 'textSigle') |
| 110 | ->text_is('analytic > h\.title[type=main]', 'Campagne in Frankreich', 'h.title'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 111 | |
| 112 | # Uncompress GOE/AGA/00000/data.xml from zip file |
| 113 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/data.xml'); |
| 114 | |
| 115 | ok($zip, 'Zip-File is found'); |
| 116 | |
| 117 | # Read GOE/AGA/00000/data.xml |
| 118 | my $data_xml = ''; |
| 119 | $data_xml .= $zip->getline while !$zip->eof; |
| 120 | ok($zip->close, 'Closed'); |
| 121 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 122 | $t = Test::XML::Loy->new($data_xml); |
| 123 | $t->attr_is('raw_text', 'docid', 'GOE_AGA.00000', 'text id') |
| 124 | ->text_like('raw_text > text', qr!^Campagne in Frankreich 1792.*?uns allein begl.*cke\.$!, 'text content'); |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 125 | |
| 126 | # Uncompress GOE/AGA/00000/struct/structure.xml from zip file |
| 127 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/struct/structure.xml'); |
| 128 | |
| 129 | ok($zip, 'Zip-File is found'); |
| 130 | |
| 131 | # Read GOE/AGA/00000/struct/structure.xml |
| 132 | my $struct_xml = ''; |
| 133 | $struct_xml .= $zip->getline while !$zip->eof; |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 134 | |
Akron | 2a60c53 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 135 | ok($zip->close, 'Closed'); |
| 136 | |
Akron | d89ef82 | 2020-02-17 12:42:09 +0100 | [diff] [blame] | 137 | $t = Test::XML::Loy->new($struct_xml); |
| 138 | $t->text_is('span[id=s3] *[name=type]', 'Autobiographie', 'text content'); |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 139 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 140 | |
| 141 | # Uncompress GOE/AGA/00000/base/tokens_aggressive.xml from zip file |
| 142 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/base/tokens_aggressive.xml'); |
| 143 | |
| 144 | # Read GOE/AGA/00000/base/tok.xml |
| 145 | my $tokens_xml = ''; |
| 146 | $tokens_xml .= $zip->getline while !$zip->eof; |
| 147 | ok($zip->close, 'Closed'); |
| 148 | |
| 149 | $t = Test::XML::Loy->new($tokens_xml); |
| 150 | $t->attr_is('spanList span:nth-child(1)', 'to', 8); |
| 151 | |
| 152 | $t->attr_is('spanList span#t_1', 'from', 9); |
| 153 | $t->attr_is('spanList span#t_1', 'to', 11); |
| 154 | |
| 155 | $t->attr_is('spanList span#t_67', 'from', 427); |
| 156 | $t->attr_is('spanList span#t_67', 'to', 430); |
| 157 | |
| 158 | $t->attr_is('spanList span#t_214', 'from', 1209); |
| 159 | $t->attr_is('spanList span#t_214', 'to', 1212); |
| 160 | |
| 161 | $t->element_count_is('spanList span', 227); |
| 162 | |
| 163 | |
| 164 | # Uncompress GOE/AGA/00000/base/tokens_conservative.xml from zip file |
| 165 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/base/tokens_conservative.xml'); |
| 166 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 167 | $tokens_xml = ''; |
| 168 | $tokens_xml .= $zip->getline while !$zip->eof; |
| 169 | ok($zip->close, 'Closed'); |
| 170 | |
| 171 | $t = Test::XML::Loy->new($tokens_xml); |
| 172 | $t->attr_is('spanList span:nth-child(1)', 'to', 8); |
| 173 | |
| 174 | $t->attr_is('spanList span#t_1', 'from', 9); |
| 175 | $t->attr_is('spanList span#t_1', 'to', 11); |
| 176 | |
| 177 | $t->attr_is('spanList span#t_67', 'from', 427); |
| 178 | $t->attr_is('spanList span#t_67', 'to', 430); |
| 179 | |
| 180 | $t->attr_is('spanList span#t_214', 'from', 1209); |
| 181 | $t->attr_is('spanList span#t_214', 'to', 1212); |
| 182 | |
| 183 | $t->element_count_is('spanList span', 227); |
| 184 | |
| 185 | # Tokenize with external tokenizer |
| 186 | my $cmd = catfile($f, 'cmd', 'tokenizer.pl'); |
| 187 | |
| 188 | stderr_like( |
| 189 | sub { `cat '$file' | perl '$script' --tc='perl $cmd' > '$outzip'` }, |
| 190 | qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!, |
| 191 | 'Processing' |
| 192 | ); |
| 193 | |
| 194 | # Uncompress GOE/AGA/00000/base/tokens_conservative.xml from zip file |
| 195 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/base/tokens.xml'); |
| 196 | |
| 197 | # Read GOE/AGA/00000/base/tokens.xml |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 198 | $tokens_xml = ''; |
| 199 | $tokens_xml .= $zip->getline while !$zip->eof; |
| 200 | ok($zip->close, 'Closed'); |
| 201 | |
| 202 | $t = Test::XML::Loy->new($tokens_xml); |
| 203 | $t->attr_is('spanList span:nth-child(1)', 'to', 8); |
| 204 | |
| 205 | $t->attr_is('spanList span#t_1', 'from', 9); |
| 206 | $t->attr_is('spanList span#t_1', 'to', 11); |
| 207 | |
| 208 | $t->attr_is('spanList span#t_67', 'from', 427); |
| 209 | $t->attr_is('spanList span#t_67', 'to', 430); |
| 210 | |
| 211 | $t->attr_is('spanList span#t_214', 'from', 1209); |
| 212 | $t->attr_is('spanList span#t_214', 'to', 1212); |
| 213 | |
| 214 | $t->element_count_is('spanList span', 227); |
| 215 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 216 | done_testing; |