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 | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame^] | 6 | use open qw(:std :utf8); # assume utf-8 encoding |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 7 | |
| 8 | use FindBin; |
| 9 | BEGIN { |
| 10 | unshift @INC, "$FindBin::Bin/../lib"; |
| 11 | }; |
| 12 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 13 | require_ok('KorAP::XML::TEI::Tokenizer::Aggressive'); |
| 14 | require_ok('KorAP::XML::TEI::Tokenizer::Conservative'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 15 | |
| 16 | # Test aggressive |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 17 | my $aggr = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 18 | $aggr->tokenize("Der alte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 19 | is_deeply($aggr, [0,3,4,8,9,13]); |
| 20 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 21 | $aggr->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 22 | 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] | 23 | |
| 24 | # Test conservative |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 25 | my $cons = KorAP::XML::TEI::Tokenizer::Conservative->new; |
| 26 | $cons->tokenize("Der alte Mann"); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 27 | is_deeply($cons, [0,3,4,8,9,13]); |
| 28 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 29 | $cons->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 30 | is_deeply($cons, [0,3,4,8,9,12,12,13,14,17,18,31,32,36]); |
| 31 | |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 32 | $cons->reset->tokenize(" Der alte bzw. der grau-melierte Mann"); |
| 33 | is_deeply($cons, [2,5,6,10,11,14,14,15,16,19,20,33,34,38]); |
| 34 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 35 | $cons->reset->tokenize(". Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 36 | is_deeply($cons, [0,1,2,5]); |
| 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, [1,2,3,6]); |
| 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, [3,4,5,8]); |
| 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,1,2,2,3,4,7]); |
| 46 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 47 | $cons->reset->tokenize(".Der"); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 48 | is_deeply($cons, [0,1,1,4]); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 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,4,5,5,6,6,7,7,8]); |
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.... "); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 54 | is_deeply($cons, [0,1,1,2,2,5,5,6,6,7,7,8,8,9]); |
| 55 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 56 | $cons->reset->tokenize(". Der.... "); |
| 57 | 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] | 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,3,3,6,6,7,7,8,8,9,9,10]); |
| 61 | |
| 62 | $cons->reset->tokenize("Der\talte\nMann"); |
| 63 | is_deeply($cons, [0,3,4,8,9,13]); |
| 64 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 65 | ## Test data |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame^] | 66 | my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia.txt'); |
| 67 | my $data = ''; |
| 68 | |
| 69 | ok(open(my $fh, '<' . $dataf), 'Open file wikipedia.txt'); |
| 70 | |
| 71 | while (!eof($fh)) { |
| 72 | $data .= <$fh> |
| 73 | }; |
| 74 | |
| 75 | ok(close($fh), 'Close file wikipedia.txt'); |
| 76 | |
| 77 | is(134996, length($data)); |
| 78 | |
| 79 | $aggr->reset->tokenize($data); |
| 80 | 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]); |
| 81 | is(47112, scalar(@$aggr)); |
| 82 | |
| 83 | $cons->reset->tokenize($data); |
| 84 | 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]); |
| 85 | is(42412, scalar(@$cons)); |
| 86 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 87 | ## check tokenization of 'Community-Ämter aufgestiegen' |
| 88 | ## from @{cons}[19518] (=66070) to @{cons}[19519] (=66085) => 'Community-Ämter' |
| 89 | ## from @{cons}[19520] (=66086) to @{cons}[19521] (=66098) => 'aufgestiegen' |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame^] | 90 | my @vals_got=(66070,66085,66086,66098); |
| 91 | my @vals_exp; push @vals_exp, @{$cons}[$_] for(19518,19519,19520,19521); |
| 92 | is_deeply([@vals_exp], [@vals_got]); |
| 93 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 94 | $cons->reset->tokenize("Community-\xc4mter aufgestiegen"); |
| 95 | is_deeply($cons, [0,15,16,28]); |
| 96 | |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame^] | 97 | $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia_small.txt'); |
| 98 | $data = ''; |
| 99 | ok(open($fh, '<' . $dataf), 'Open file wikipedia_small.txt'); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 100 | while (!eof($fh)) { |
| 101 | $data .= <$fh> |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 102 | }; |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 103 | ok(close($fh), 'Close file wikipedia_small.txt'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 104 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 105 | $aggr->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 106 | 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] | 107 | is(366, scalar(@$aggr)); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 108 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 109 | $cons->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 110 | 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] | 111 | is(302, scalar(@$cons)); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame] | 112 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 113 | |
| 114 | done_testing; |