Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 3 | #use open qw(:std :utf8); # see perlunifaq: What is the difference between ":encoding" and ":utf8"? |
| 4 | use open qw(:std :encoding(UTF-8)); # assume utf-8 encoding (see utf8 in Test::More) |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 5 | use Test::More; |
| 6 | use File::Basename 'dirname'; |
| 7 | use File::Spec::Functions qw/catfile/; |
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 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 14 | require_ok('KorAP::XML::TEI::Tokenizer::Aggressive'); |
| 15 | require_ok('KorAP::XML::TEI::Tokenizer::Conservative'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 16 | |
| 17 | # Test aggressive |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 18 | my $aggr = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 19 | $aggr->tokenize("Der alte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 20 | is_deeply($aggr, [0,3,4,8,9,13]); |
| 21 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 22 | $aggr->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 23 | 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] | 24 | |
| 25 | # Test conservative |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 26 | my $cons = KorAP::XML::TEI::Tokenizer::Conservative->new; |
| 27 | $cons->tokenize("Der alte Mann"); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 28 | is_deeply($cons, [0,3,4,8,9,13]); |
| 29 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 30 | $cons->reset->tokenize("Der alte bzw. der grau-melierte Mann"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 31 | is_deeply($cons, [0,3,4,8,9,12,12,13,14,17,18,31,32,36]); |
| 32 | |
Peter Harders | 71f072b | 2020-07-15 14:15:01 +0200 | [diff] [blame] | 33 | $cons->reset->tokenize(" Der alte bzw. der grau-melierte Mann"); |
| 34 | is_deeply($cons, [2,5,6,10,11,14,14,15,16,19,20,33,34,38]); |
| 35 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 36 | $cons->reset->tokenize(". Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 37 | is_deeply($cons, [0,1,2,5]); |
| 38 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 39 | $cons->reset->tokenize(" . Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 40 | is_deeply($cons, [1,2,3,6]); |
| 41 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 42 | $cons->reset->tokenize(" . Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 43 | is_deeply($cons, [3,4,5,8]); |
| 44 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 45 | $cons->reset->tokenize("... Der"); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 46 | is_deeply($cons, [0,1,1,2,2,3,4,7]); |
| 47 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 48 | # done: '.' is now tokenized |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 49 | $cons->reset->tokenize(".Der"); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 50 | is_deeply($cons, [0,1,1,4]); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 51 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 52 | $cons->reset->tokenize(".Der.... "); |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 53 | 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] | 54 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 55 | $cons->reset->tokenize("..Der.... "); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 56 | is_deeply($cons, [0,1,1,2,2,5,5,6,6,7,7,8,8,9]); |
| 57 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 58 | $cons->reset->tokenize(". Der.... "); |
| 59 | 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] | 60 | |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 61 | $cons->reset->tokenize(". .Der.... "); |
| 62 | is_deeply($cons, [0,1,2,3,3,6,6,7,7,8,8,9,9,10]); |
| 63 | |
| 64 | $cons->reset->tokenize("Der\talte\nMann"); |
| 65 | is_deeply($cons, [0,3,4,8,9,13]); |
| 66 | |
| 67 | |
| 68 | ##### TODO: big wikipedia.txt leads to very slow processing => use smaller test file as temporary solution (see below) |
| 69 | ## Test data |
| 70 | #my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia.txt'); |
| 71 | #my $data = ''; |
| 72 | # |
| 73 | #ok(open(my $fh, '<' . $dataf), 'Open file wikipedia.txt'); |
| 74 | #while (!eof($fh)) { |
| 75 | # $data .= <$fh> |
| 76 | #}; |
| 77 | # |
| 78 | ### DEBUG |
| 79 | ##my @layers = PerlIO::get_layers($fh); # see 'man PerlIO': Querying the layers of filehandles |
| 80 | ##foreach my $l(@layers){print STDERR "DEBUG (filehandle layer): $l\n"}; |
| 81 | # |
| 82 | #ok(close($fh), 'Close file wikipedia.txt'); |
| 83 | # |
| 84 | #is(134996, length($data)); # mind that each UTF-8 character counts only once |
| 85 | # |
| 86 | ## TODO: With then necessary open-pragma (see above), this is extremely slow ... Where's the bottleneck? |
| 87 | ## No performance-issue, when piping 'wikipedia.txt' into a perl one-liner (also not, when using while-loop from Aggressive.pm): |
| 88 | ## cat t/data/wikipedia.txt | perl -ne 'use open qw(:std :utf8); chomp; for($i=0;$i<length;$i++){$c=substr $_,$i,1; print ">$c<\n" if $c=~/\p{Punct}/}' >/dev/null |
| 89 | ## cat t/data/wikipedia.txt | perl -ne 'use open qw(:std :utf8); chomp; while($_=~/([^\p{Punct} \x{9}\n]+)(?:(\p{Punct})|(?:[ \x{9}\n])?)|(\p{Punct})/gx){ print "$1\n" if $1}' >/dev/null |
| 90 | ## note |
| 91 | ## check different output with/without additional UTF-8 layer |
| 92 | ## echo "„Wikipedia-Artikel brauchen Fotos“" | perl -ne 'chomp; for($i=0;$i<length;$i++){$c=substr $_,$i,1; print ">$c<\n" if $c=~/\p{Punct}/}' |
| 93 | ## echo "„Wikipedia-Artikel brauchen Fotos“" | perl -ne 'use open qw(:std :utf8); chomp; for($i=0;$i<length;$i++){$c=substr $_,$i,1; print ">$c<\n" if $c=~/\p{Punct}/}' |
| 94 | # |
| 95 | #diag("DEBUG (aggr): Tokenizing Wikipedia Text (134K). Because of an additional PerlIO layer (utf8) on the filehandle, this takes significant more time. Please wait ...\n"); |
| 96 | #$aggr->reset->tokenize($data); |
| 97 | #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]); |
| 98 | #is(47112, scalar(@$aggr)); |
| 99 | # |
| 100 | #diag("DEBUG (cons): Tokenizing Wikipedia Text (134K). Because of an additional PerlIO layer (utf8) on the filehandle, this takes significant more time. Please wait ...\n"); |
| 101 | #$cons->reset->tokenize($data); |
| 102 | #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]); |
| 103 | #is(42412, scalar(@$cons)); |
| 104 | # |
| 105 | ## check tokenization of 'Community-Ämter aufgestiegen' |
| 106 | ## from @{cons}[19518] (=66070) to @{cons}[19519] (=66085) => 'Community-Ämter' |
| 107 | ## from @{cons}[19520] (=66086) to @{cons}[19521] (=66098) => 'aufgestiegen' |
| 108 | #my @vals_got=(66070,66085,66086,66098); |
| 109 | #my @vals_exp; push @vals_exp, @{$cons}[$_] for(19518,19519,19520,19521); |
| 110 | #is_deeply([@vals_exp], [@vals_got]); |
| 111 | ## |
| 112 | ##### TODO: use smaller test file as temporary workaround (until problem solved) |
| 113 | $cons->reset->tokenize("Community-\xc4mter aufgestiegen"); |
| 114 | is_deeply($cons, [0,15,16,28]); |
| 115 | |
| 116 | my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia_small.txt'); |
| 117 | my $data = ''; |
| 118 | ok(open(my $fh, '<' . $dataf), 'Open file wikipedia_small.txt'); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 119 | while (!eof($fh)) { |
| 120 | $data .= <$fh> |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 121 | }; |
Peter Harders | 854a115 | 2020-07-22 22:48:02 +0200 | [diff] [blame^] | 122 | ok(close($fh), 'Close file wikipedia_small.txt'); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 123 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 124 | $aggr->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 125 | 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^] | 126 | is(366, scalar(@$aggr)); |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 127 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 128 | $cons->reset->tokenize($data); |
Peter Harders | 1d65f94 | 2020-07-22 23:31:00 +0200 | [diff] [blame] | 129 | 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^] | 130 | is(302, scalar(@$cons)); |
| 131 | ##### |
| 132 | |
Akron | eac374d | 2020-07-07 09:00:44 +0200 | [diff] [blame] | 133 | |
| 134 | done_testing; |