Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
| 4 | use File::Basename 'dirname'; |
| 5 | use File::Spec::Functions qw/catfile/; |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 6 | use IO::Uncompress::Unzip; |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 7 | use open qw(:std :utf8); # assume utf-8 encoding |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 8 | |
| 9 | use FindBin; |
| 10 | BEGIN { |
| 11 | unshift @INC, "$FindBin::Bin/../lib"; |
| 12 | }; |
| 13 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 14 | use_ok('Test::KorAP::XML::TEI','korap_tempfile'); |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 15 | require_ok('KorAP::XML::TEI::Tokenizer::Aggressive'); |
| 16 | require_ok('KorAP::XML::TEI::Tokenizer::Conservative'); |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 17 | require_ok('KorAP::XML::TEI::Zipper'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 18 | |
| 19 | # Test aggressive |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 20 | my $aggr = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 21 | $aggr->tokenize("Der alte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 22 | is_deeply($aggr, [0,3,4,8,9,13]); |
| 23 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 24 | $aggr->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 25 | is_deeply($aggr, [0,3,4,8,9,12,12,13,14,17,18,22,22,23,23,31,32,36]); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 26 | |
Akron | edee6e5 | 2020-07-27 14:15:11 +0200 | [diff] [blame^] | 27 | like( |
| 28 | $aggr->reset->tokenize("Der")->to_string('a'), |
| 29 | qr!id="t_0"!, |
| 30 | 'Chainable' |
| 31 | ); |
| 32 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 33 | # Test conservative |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 34 | my $cons = KorAP::XML::TEI::Tokenizer::Conservative->new; |
| 35 | $cons->tokenize("Der alte Mann"); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 36 | is_deeply($cons, [0,3,4,8,9,13]); |
| 37 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 38 | $cons->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 39 | is_deeply($cons, [0,3,4,8,9,12,12,13,14,17,18,31,32,36]); |
| 40 | |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 41 | $cons->reset->tokenize(" Der alte bzw. der grau-melierte Mann"); |
| 42 | is_deeply($cons, [2,5,6,10,11,14,14,15,16,19,20,33,34,38]); |
| 43 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 44 | $cons->reset->tokenize(". Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 45 | is_deeply($cons, [0,1,2,5]); |
| 46 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 47 | $cons->reset->tokenize(" . Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 48 | is_deeply($cons, [1,2,3,6]); |
| 49 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 50 | $cons->reset->tokenize(" . Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 51 | is_deeply($cons, [3,4,5,8]); |
| 52 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 53 | $cons->reset->tokenize("... Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 54 | is_deeply($cons, [0,1,1,2,2,3,4,7]); |
| 55 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 56 | $cons->reset->tokenize(".Der"); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 57 | is_deeply($cons, [0,1,1,4]); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 58 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 59 | $cons->reset->tokenize(".Der.... "); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 60 | is_deeply($cons, [0,1,1,4,4,5,5,6,6,7,7,8]); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 61 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 62 | $cons->reset->tokenize("..Der.... "); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 63 | is_deeply($cons, [0,1,1,2,2,5,5,6,6,7,7,8,8,9]); |
| 64 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 65 | $cons->reset->tokenize(". Der.... "); |
| 66 | is_deeply($cons, [0,1,2,5,5,6,6,7,7,8,8,9]); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 67 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 68 | $cons->reset->tokenize(". .Der.... "); |
| 69 | is_deeply($cons, [0,1,2,3,3,6,6,7,7,8,8,9,9,10]); |
| 70 | |
| 71 | $cons->reset->tokenize("Der\talte\nMann"); |
| 72 | is_deeply($cons, [0,3,4,8,9,13]); |
| 73 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 74 | ## Test data |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 75 | my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia.txt'); |
| 76 | my $data = ''; |
| 77 | |
| 78 | ok(open(my $fh, '<' . $dataf), 'Open file wikipedia.txt'); |
| 79 | |
| 80 | while (!eof($fh)) { |
| 81 | $data .= <$fh> |
| 82 | }; |
| 83 | |
| 84 | ok(close($fh), 'Close file wikipedia.txt'); |
| 85 | |
| 86 | is(134996, length($data)); |
| 87 | |
| 88 | $aggr->reset->tokenize($data); |
| 89 | is_deeply([@{$aggr}[0..25]], [1,7,8,12,14,18,19,22,23,27,28,38,39,40,40,49,49,50,50,57,58,66,67,72,72,73]); |
| 90 | is(47112, scalar(@$aggr)); |
| 91 | |
| 92 | $cons->reset->tokenize($data); |
| 93 | is_deeply([@{$cons}[0..21]], [1,7,8,12,14,18,19,22,23,27,28,38,39,40,40,57,58,66,67,72,72,73]); |
| 94 | is(42412, scalar(@$cons)); |
| 95 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 96 | ## check tokenization of 'Community-Ämter aufgestiegen' |
| 97 | ## from @{cons}[19518] (=66070) to @{cons}[19519] (=66085) => 'Community-Ämter' |
| 98 | ## from @{cons}[19520] (=66086) to @{cons}[19521] (=66098) => 'aufgestiegen' |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 99 | my @vals_got=(66070,66085,66086,66098); |
| 100 | my @vals_exp; push @vals_exp, @{$cons}[$_] for(19518,19519,19520,19521); |
| 101 | is_deeply([@vals_exp], [@vals_got]); |
| 102 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 103 | $cons->reset->tokenize("Community-\xc4mter aufgestiegen"); |
| 104 | is_deeply($cons, [0,15,16,28]); |
| 105 | |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 106 | $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia_small.txt'); |
| 107 | $data = ''; |
| 108 | ok(open($fh, '<' . $dataf), 'Open file wikipedia_small.txt'); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 109 | while (!eof($fh)) { |
| 110 | $data .= <$fh> |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 111 | }; |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 112 | ok(close($fh), 'Close file wikipedia_small.txt'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 113 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 114 | $aggr->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 115 | is_deeply([@{$aggr}[0..25]], [1,7,8,12,14,18,19,22,23,27,28,38,39,40,40,49,49,50,50,57,58,66,67,72,72,73]); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 116 | is(366, scalar(@$aggr)); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 117 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 118 | $cons->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 119 | is_deeply([@{$cons}[0..21]], [1,7,8,12,14,18,19,22,23,27,28,38,39,40,40,57,58,66,67,72,72,73]); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 120 | is(302, scalar(@$cons)); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 121 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 122 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 123 | subtest 'Test Zipper' => sub { |
| 124 | # Test Zipper |
| 125 | my ($fh, $outzip) = korap_tempfile('tokenize_zipper'); |
| 126 | my $zip = KorAP::XML::TEI::Zipper->new($outzip); |
| 127 | $fh->close; |
| 128 | |
| 129 | my $aggr = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 130 | $aggr->tokenize("Der alte Mann"); |
| 131 | ok($aggr->to_zip( |
| 132 | $zip->new_stream('tokens.xml'), |
| 133 | 'fun' |
| 134 | ), 'Written successfully'); |
| 135 | |
| 136 | $zip->close; |
| 137 | |
| 138 | ok(-e $outzip, 'Zip exists'); |
| 139 | my $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'tokens.xml'); |
| 140 | ok(!$unzip->eof, 'Unzip successful'); |
| 141 | }; |
| 142 | |
| 143 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 144 | done_testing; |