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