blob: 503eeee0216e6213804c542247856a0fb7f653c5 [file] [log] [blame]
Akroneac374d2020-07-07 09:00:44 +02001use strict;
2use warnings;
Peter Harders1d65f942020-07-22 23:31:00 +02003#use open qw(:std :utf8); # see perlunifaq: What is the difference between ":encoding" and ":utf8"?
4use open qw(:std :encoding(UTF-8)); # assume utf-8 encoding (see utf8 in Test::More)
Akroneac374d2020-07-07 09:00:44 +02005use Test::More;
6use File::Basename 'dirname';
7use File::Spec::Functions qw/catfile/;
Akroneac374d2020-07-07 09:00:44 +02008
9use FindBin;
10BEGIN {
11 unshift @INC, "$FindBin::Bin/../lib";
12};
13
Akrond9627472020-07-09 16:53:09 +020014require_ok('KorAP::XML::TEI::Tokenizer::Aggressive');
15require_ok('KorAP::XML::TEI::Tokenizer::Conservative');
Akroneac374d2020-07-07 09:00:44 +020016
17# Test aggressive
Akrond9627472020-07-09 16:53:09 +020018my $aggr = KorAP::XML::TEI::Tokenizer::Aggressive->new;
19$aggr->tokenize("Der alte Mann");
Akron510a88c2020-07-07 10:16:50 +020020is_deeply($aggr, [0,3,4,8,9,13]);
21
Akrond9627472020-07-09 16:53:09 +020022$aggr->reset->tokenize("Der alte bzw. der grau-melierte Mann");
Akron510a88c2020-07-07 10:16:50 +020023is_deeply($aggr, [0,3,4,8,9,12,12,13,14,17,18,22,22,23,23,31,32,36]);
Akroneac374d2020-07-07 09:00:44 +020024
25# Test conservative
Akrond9627472020-07-09 16:53:09 +020026my $cons = KorAP::XML::TEI::Tokenizer::Conservative->new;
27$cons->tokenize("Der alte Mann");
Akroneac374d2020-07-07 09:00:44 +020028is_deeply($cons, [0,3,4,8,9,13]);
29
Akrond9627472020-07-09 16:53:09 +020030$cons->reset->tokenize("Der alte bzw. der grau-melierte Mann");
Akron510a88c2020-07-07 10:16:50 +020031is_deeply($cons, [0,3,4,8,9,12,12,13,14,17,18,31,32,36]);
32
Peter Harders71f072b2020-07-15 14:15:01 +020033$cons->reset->tokenize(" Der alte bzw. der grau-melierte Mann");
34is_deeply($cons, [2,5,6,10,11,14,14,15,16,19,20,33,34,38]);
35
Akrond9627472020-07-09 16:53:09 +020036$cons->reset->tokenize(". Der");
Akron510a88c2020-07-07 10:16:50 +020037is_deeply($cons, [0,1,2,5]);
38
Akrond9627472020-07-09 16:53:09 +020039$cons->reset->tokenize(" . Der");
Akron510a88c2020-07-07 10:16:50 +020040is_deeply($cons, [1,2,3,6]);
41
Akrond9627472020-07-09 16:53:09 +020042$cons->reset->tokenize(" . Der");
Akron510a88c2020-07-07 10:16:50 +020043is_deeply($cons, [3,4,5,8]);
44
Akrond9627472020-07-09 16:53:09 +020045$cons->reset->tokenize("... Der");
Akron510a88c2020-07-07 10:16:50 +020046is_deeply($cons, [0,1,1,2,2,3,4,7]);
47
Peter Harders41c35622020-07-12 01:16:22 +020048# TODO:
49# bug: '.' is not tokenized
Akrond9627472020-07-09 16:53:09 +020050$cons->reset->tokenize(".Der");
Akron510a88c2020-07-07 10:16:50 +020051is_deeply($cons, [1,4]);
52
Akrond9627472020-07-09 16:53:09 +020053$cons->reset->tokenize(".Der.... ");
Akron510a88c2020-07-07 10:16:50 +020054is_deeply($cons, [1,4,4,5,5,6,6,7,7,8]);
55
Akrond9627472020-07-09 16:53:09 +020056$cons->reset->tokenize("..Der.... ");
Akron510a88c2020-07-07 10:16:50 +020057is_deeply($cons, [0,1,1,2,2,5,5,6,6,7,7,8,8,9]);
58
Akroneac374d2020-07-07 09:00:44 +020059# Test data
60my $dataf = catfile(dirname(__FILE__), 'data', 'wikipedia.txt');
61my $data = '';
62
Peter Harders1d65f942020-07-22 23:31:00 +020063ok(open(my $fh, '<' . $dataf), 'Open file');
64while (!eof($fh)) {
65 $data .= <$fh>
Akroneac374d2020-07-07 09:00:44 +020066};
Akroneac374d2020-07-07 09:00:44 +020067
Peter Harders1d65f942020-07-22 23:31:00 +020068## DEBUG
69#my @layers = PerlIO::get_layers($fh); # see 'man PerlIO': Querying the layers of filehandles
70#foreach my $l(@layers){print STDERR "DEBUG (filehandle layer): $l\n"};
Akroneac374d2020-07-07 09:00:44 +020071
Peter Harders1d65f942020-07-22 23:31:00 +020072ok(close($fh), 'Close file');
73
74is(134996, length($data)); # mind that each UTF-8 character counts only once
75
76## note
77# check different output with/without additional UTF-8 layer
78# 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}/}'
79# 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}/}'
80
81# TODO: With then necessary open-pragma (see above), this is extremely slow ... Where's the bottleneck?
82# No performance-issue, when piping 'wikipedia.txt' into a perl one-liner (also not, when using while-loop from Aggressive.pm):
83# 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
84# 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
85diag("DEBUG: Tokenizing Wikipedia Text (134K). Because of an additional PerlIO layer (utf8) on the filehandle, this takes significant more time. Please wait ...\n");
Akrond9627472020-07-09 16:53:09 +020086$aggr->reset->tokenize($data);
Peter Harders1d65f942020-07-22 23:31:00 +020087is_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]);
88is(47112, scalar(@$aggr));
Akroneac374d2020-07-07 09:00:44 +020089
Peter Harders1d65f942020-07-22 23:31:00 +020090diag("DEBUG: Tokenizing Wikipedia Text (134K). Because of an additional PerlIO layer (utf8) on the filehandle, this takes significant more time. Please wait ...\n");
Akrond9627472020-07-09 16:53:09 +020091$cons->reset->tokenize($data);
Peter Harders1d65f942020-07-22 23:31:00 +020092is_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]);
93is(43218, scalar(@$cons));
Akroneac374d2020-07-07 09:00:44 +020094
95done_testing;