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/; |
| 6 | use File::Temp 'tempfile'; |
| 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 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 32 | $cons->reset->tokenize(". Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 33 | is_deeply($cons, [0,1,2,5]); |
| 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, [1,2,3,6]); |
| 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, [3,4,5,8]); |
| 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, [0,1,1,2,2,3,4,7]); |
| 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, [1,4]); |
| 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,4,4,5,5,6,6,7,7,8]); |
| 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, [0,1,1,2,2,5,5,6,6,7,7,8,8,9]); |
| 52 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 53 | # Test data |
| 54 | my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia.txt'); |
| 55 | my $data = ''; |
| 56 | |
| 57 | ok(open(FH, '<' . $dataf), 'Open file'); |
| 58 | while (!eof(FH)) { |
| 59 | $data .= <FH> |
| 60 | }; |
| 61 | close(FH); |
| 62 | |
| 63 | is(137166, length($data)); |
| 64 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 65 | $aggr->reset->tokenize($data); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 66 | is_deeply([@{$aggr}[0..7]], [1,7,8,12,14,18,19,22]); |
| 67 | is(47242, scalar(@$aggr)); |
| 68 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 69 | $cons->reset->tokenize($data); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 70 | is_deeply([@{$cons}[0..7]], [1,7,8,12,14,18,19,22]); |
| 71 | is(43068, scalar(@$cons)); |
| 72 | |
| 73 | done_testing; |