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