blib/lib/Hailo/Tokenizer/Words.pm

filter: Statementstmt 100% 121 / 121 Branchbran 92.8% 26 / 28 Conditioncond 92.3% 36 / 39 MC/DCmcdc 85.0% 17 / 20 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 95.4% 208 / 218 CC 29 SCAR 27.4
CC
29
Cov
97%
make_tokens
22.1
make_output
8.0
BEGIN
1.0
CRAP 15.4
« blib/lib/Hailo/Storage/MySQL.pm ↑ Summary blib/lib/Hailo/Command.pm »
1 package Hailo::Tokenizer::Words;
2 29 our $AUTHORITY = 'cpan:AVAR';
3 29 $Hailo::Tokenizer::Words::VERSION = '0.73';
4 29 ▶use v5.28.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Regexp::Common qw/ URI /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 29 with qw(Hailo::Role::Arguments
12 Hailo::Role::Tokenizer);
13  
14 # [[:alpha:]] doesn't match combining characters on Perl >=5.12
15 29 my $ALPHABET = qr/(?![_\d])\w/;
16  
17 # tokenization
18 29 my $SPACE = qr/\s/;
19 29 my $NONSPACE = qr/\S/;
20 29 my $DASH = qr/[–-]/;
21 29 my $POINT = qr/[.,]/;
22 29 my $APOSTROPHE = qr/['’´]/;
23 29 my $ELLIPSIS = qr/\.{2,}|…/;
24 29 my $NON_WORD = qr/[^\w\s]+/;
25 29 my $BARE_WORD = qr/\w+/;
26 29 my $CURRENCY = qr/[¤¥¢£\$]/;
27 29 my $NUMBER = qr/$CURRENCY?$POINT\d+(?:$POINT\d+)*(?:$CURRENCY|$ALPHABET+)?|$CURRENCY?\d+(?:$POINT\d+)*(?:$CURRENCY|$ALPHABET+)?(?!\d|$ALPHABET)/;
28 29 my $APOST_WORD = qr/$ALPHABET+(?:$APOSTROPHE$ALPHABET+)+/;
29 29 my $ABBREV = qr/$ALPHABET(?:\.$ALPHABET)+\./;
30 29 my $DOTTED = qr/$BARE_WORD?\.$BARE_WORD(?:\.$BARE_WORD)*/;
31 29 my $WORD_TYPES = qr/$NUMBER|$ABBREV|$DOTTED|$APOST_WORD|$BARE_WORD/;
32 29 my $WORD_APOST = qr/$WORD_TYPES(?:$DASH$WORD_TYPES)*$APOSTROPHE(?!$ALPHABET|$NUMBER)/;
33 29 my $WORD = qr/$WORD_TYPES(?:(?:$DASH$WORD_TYPES)+|$DASH(?!$DASH))?/;
34 29 my $MIXED_CASE = qr/ \p{Lower}+ \p{Upper} | \p{Upper}{2,} \p{Lower} /x;
35 29 my $UPPER_NONW = qr/^ (?:\p{Upper}+ \W+)(?<!I') (?: \p{Upper}* \p{Lower} ) /x;
36  
37 # special tokens
38 29 my $TWAT_NAME = qr/ \@ [A-Za-z0-9_]+ /x;
39 29 my $EMAIL = qr/ [A-Z0-9._%+-]+ @ [A-Z0-9.-]+ (?: \. [A-Z]{2,4} )* /xi;
40 29 my $PERL_CLASS = qr/ (?: :: \w+ (?: :: \w+ )* | \w+ (?: :: \w+ )+ ) (?: :: )? | \w+ :: /x;
41 29 my $EXTRA_URI = qr{ (?: \w+ \+ ) ssh:// $NONSPACE+ }x;
42 29 my $ESC_SPACE = qr/(?:\\ )+/;
43 29 my $NAME = qr/(?:$BARE_WORD|$ESC_SPACE)+/;
44 29 my $FILENAME = qr/ $NAME? \. $NAME (?: \. $NAME )* | $NAME/x;
45 29 my $UNIX_PATH = qr{ / $FILENAME (?: / $FILENAME )* /? }x;
46 29 my $WIN_PATH = qr{ $ALPHABET : \\ $FILENAME (?: \\ $FILENAME )* \\?}x;
47 29 my $PATH = qr/$UNIX_PATH|$WIN_PATH/;
48 29 my $DATE = qr/[0-9]{4}-W?[0-9]{1,2}-[0-9]{1,2}/i;
49 29 my $TIME = qr/[0-9]{1,2}:[0-9]{2}(?::[0-9]{2})?(?:Z| ?[AP]M|[-+±][0-9]{2}(?::?[0-9]{2})?)?/i;
50 29 my $DATETIME = qr/${DATE}T$TIME/;
51 29 my $IRC_NICK = qr/<(?: |[&~]?[@%+~&])?[A-Za-z_`\-^\|\\\{}\[\]][A-Za-z_0-9`\-^\|\\\{}\[\]]+>/;
52 29 my $IRC_CHAN = qr/[#&+][^ \a\0\012\015,:]{1,199}/;
53 29 my $NUMERO = qr/#[0-9]+/;
54 29 my $CLOSE_TAG = qr{</[-\w]+>};
55  
56 29 my $CASED_WORD = qr/$CLOSE_TAG|$IRC_NICK|$IRC_CHAN|$DATETIME|$DATE|$TIME|$PERL_CLASS|$EXTRA_URI|$EMAIL|$TWAT_NAME|$PATH|$NUMERO/;
57  
58 # capitalization
59 # The rest of the regexes are pretty hairy. The goal here is to catch the
60 # most common cases where a word should be capitalized. We try hard to
61 # guard against capitalizing things which don't look like proper words.
62 # Examples include URLs and code snippets.
63 29 my $OPEN_QUOTE = qr/['"‘“„«»「『‹‚]/;
64 29 my $CLOSE_QUOTE = qr/['"’“”«»」』›‘]/;
65 29 my $TERMINATOR = qr/(?:[?!‽]+|(?<!\.)\.)/;
66 29 my $ADDRESS = qr/:/;
67 29 my $PUNCTUATION = qr/[?!‽,;.:]/;
68 29 my $BOUNDARY = qr/$CLOSE_QUOTE?(?:\s*$TERMINATOR|$ADDRESS)\s+$OPEN_QUOTE?\s*/;
69 29 my $LOOSE_WORD = qr/$IRC_CHAN|$DATETIME|$DATE|$TIME|$PATH|$NUMBER|$ABBREV|$APOST_WORD|$NUMERO|$BARE_WORD(?:$DASH(?:$WORD_TYPES|$BARE_WORD)|$APOSTROPHE(?!$ALPHABET|$NUMBER|$APOSTROPHE)|$DASH(?!$DASH{2}))*/;
70 29 my $SPLIT_WORD = qr{$LOOSE_WORD(?:/$LOOSE_WORD)?(?=$PUNCTUATION(?:\s+|$)|$CLOSE_QUOTE|$TERMINATOR|\s+|$)};
71  
72 # we want to capitalize words that come after "On example.com?"
73 # or "You mean 3.2?", but not "Yes, e.g."
74 29 my $DOTTED_STRICT = qr/$LOOSE_WORD(?:$POINT(?:\d+|\w{2,}))?/;
75 29 my $WORD_STRICT = qr/$DOTTED_STRICT(?:$APOSTROPHE$DOTTED_STRICT)*/;
76  
77 # This string is added to (and later removed from) the output string when
78 # capitalizing it in multiple passes. We use backspace, because that is
79 # unlikely to be in the input. This dirty approach can probably be replaced
80 # with regex grammars, but I haven't bothered to learn to use those.
81 29 my $SEPARATOR = "\x08";
82  
83 # input -> tokens
84 sub make_tokens {
85 5177 ▶ my ($self, $input) = @_;
Subroutine make_tokens: called CC=22 SCAR=30.9
Pod undocumented
86  
87 5177 my @tokens;
88 5177 $input =~ s/$DASH\K\s*\n+\s*//;
89 5177 $input =~ s/\s*\n+\s*/ /gm;
90  
91 5177 ▶ while (length $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input) 28417 5177 33594
92 # remove the next chunk of whitespace
93 28417 $input =~ s/^$SPACE+//;
94 28417 my $got_word;
95  
96 28417 ▶ while (length $input && $input =~ /^$NONSPACE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input and $input =~ /^$NONSPACE/u) 35034 28417 63451
Condition100%3/3
length $input and $input =~ /^$NONSPACE/u
!ll&&!rl&&r
50502336735034
MC/DC100%2/2
length $input and $input =~ /^$NONSPACE/u
length $input $input =~ /^$NONSPACE/u
Truth table100%3/3
A and B
A= length $inputB= $input =~ /^$NONSPACE/u
ABresult
0X0
100
111
97 # We convert it to ASCII and then look for a URI because $RE{URI}
98 # from Regexp::Common doesn't support non-ASCII domain names
99 35034 my ($ascii) = $input =~ /^($NONSPACE+)/;
100 35034 $ascii =~ s/[^[:ascii:]]/a/g;
101  
102 # URIs
103 35034 ▶ if (!$got_word && $ascii =~ / ^ $RE{URI} /xo) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $got_word and $ascii =~ / ^ $Hailo::Tokenizer::Words::RE{'URI'} /oux) { } 8 35026 35034
elsif (not $got_word and $input =~ s/ ^ (?<word> $CASED_WORD )//oux) { } 321 34705 35026
elsif ($input =~ / ^ $WORD /oux) { } 26996 7709 34705
elsif ($input =~ s/ ^ (?<non_word> $NON_WORD ) //oux) { } 7709 0 7709
Condition100%3/3
not $got_word and $ascii =~ / ^ $Hailo::Tokenizer::Words::RE{"URI"} /oux
!ll&&!rl&&r
6226288008
MC/DC100%2/2
not $got_word and $ascii =~ / ^ $Hailo::Tokenizer::Words::RE{"URI"} /oux
not $got_word $ascii =~ / ^ $Hailo::Tokenizer::Words::RE{"URI"} /oux
Truth table100%3/3
not(A) and B
A= not $got_wordB= $ascii =~ / ^ $Hailo::Tokenizer::Words::RE{"URI"} /oux
ABresult
0X0
100
111
104 8 my $uri_end = $+[0];
105 8 my $uri = substr $input, 0, $uri_end;
106 8 $input =~ s/^\Q$uri//;
107  
108 8 push @tokens, [$self->{_spacing_normal}, $uri];
109 8 $got_word = 1;
110 }
111 # special words for which we preserve case
112 ▶ elsif (!$got_word && $input =~ s/ ^ (?<word> $CASED_WORD )//xo) {
Condition100%3/3
not $got_word and $input =~ s/ ^ (?<word> $CASED_WORD )//oux
!ll&&!rl&&r
622628479321
MC/DC100%2/2
not $got_word and $input =~ s/ ^ (?<word> $CASED_WORD )//oux
not $got_word $input =~ s/ ^ (?<word> $CASED_WORD )//oux
Truth table100%3/3
not(A) and B
A= not $got_wordB= $input =~ s/ ^ (?<word> $CASED_WORD )//oux
ABresult
0X0
100
111
113 321 push @tokens, [$self->{_spacing_normal}, $+{word}];
114 321 $got_word = 1;
115 }
116 # normal words
117 elsif ($input =~ / ^ $WORD /xo) {
118 26996 my $word;
119  
120 # special case to allow matching q{ridin'} as one word, even when
121 # it appears as q{"ridin'"}, but not as q{'ridin'}
122 26996 ▶ my $last_char = @tokens ? substr $tokens[-1][1], -1, 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
@tokens ? : 22746 4250 26996
123 26996 ▶ if (!@tokens && $input =~ s/ ^ (?<word>$WORD_APOST) //xo
Branch: 1 branch
BranchTrueFalseTotal
if (not @tokens and $input =~ s/ ^ (?<word>$WORD_APOST) //oux or $last_char =~ / ^ $APOSTROPHE $ /oux and $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux) { } 21 26975 26996
Condition77%7/9
not @tokens and $input =~ s/ ^ (?<word>$WORD_APOST) //oux or $last_char =~ / ^ $APOSTROPHE $ /oux and $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux
l!l&&r!l&&!r
21026975
not @tokens and $input =~ s/ ^ (?<word>$WORD_APOST) //oux
!ll&&!rl&&r
22746422921
$last_char =~ / ^ $APOSTROPHE $ /oux and $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux
!ll&&!rl&&r
268341410
MC/DC50%2/4
not @tokens and $input =~ s/ ^ (?<word>$WORD_APOST) //oux or $last_char =~ / ^ $APOSTROPHE $ /oux and $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux
not @tokens $input =~ s/ ^ (?<word>$WORD_APOST) //oux $last_char =~ / ^ $APOSTROPHE $ /oux $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux
Truth table57%4/7
not(A) and B or C and D
A= not @tokensB= $input =~ s/ ^ (?<word>$WORD_APOST) //ouxC= $last_char =~ / ^ $APOSTROPHE $ /ouxD= $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //oux
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
124 || $last_char =~ / ^ $APOSTROPHE $ /xo
125 && $input =~ s/ ^ (?<word>$WORD_APOST) (?<! $last_char ) //xo) {
126 21 $word = $+{word};
127 }
128 else {
129 26975 ▶ $input =~ s/^($WORD)//o and $word = $1;
Branch: 1 branch
BranchTrueFalseTotal
$input =~ s/^($WORD)//ou and $word = $1 26975 0 26975
130 }
131  
132 # Maybe preserve the casing of this word
133 26996 ▶ $word = lc $word
Branch: 1 branch
BranchTrueFalseTotal
if $word ne uc $word and not $word =~ /$MIXED_CASE/u and not $word =~ /$UPPER_NONW/u 25095 1901 26996
Condition100%6/6
$word ne uc $word and not $word =~ /$MIXED_CASE/u and not $word =~ /$UPPER_NONW/u
!ll&&!rl&&r
18792225095
$word ne uc $word and not $word =~ /$MIXED_CASE/u
!ll&&!rl&&r
18285125117
MC/DC100%3/3
$word ne uc $word and not $word =~ /$MIXED_CASE/u and not $word =~ /$UPPER_NONW/u
$word ne uc $word not $word =~ /$MIXED_CASE/u not $word =~ /$UPPER_NONW/u
Truth table100%4/4
A and not(B) and not(C)
A= $word ne uc $wordB= not $word =~ /$MIXED_CASE/uC= not $word =~ /$UPPER_NONW/u
ABCresult
0XX0
10X0
1100
1111
134 if $word ne uc $word
135 # Mixed-case words like "WoW" or "ATMs"
136 and $word !~ $MIXED_CASE
137 # Words that are upper case followed by a non-word character.
138 and $word !~ $UPPER_NONW;
139  
140 26996 push @tokens, [$self->{_spacing_normal}, $word];
141 26996 $got_word = 1;
142 }
143 # everything else
144 elsif ($input =~ s/ ^ (?<non_word> $NON_WORD ) //xo) {
145 7709 my $non_word = $+{non_word};
146 7709 my $spacing = $self->{_spacing_normal};
147  
148 # was the previous token a word?
149 7709 ▶ if ($got_word) {
Branch: 2 branches
BranchTrueFalseTotal
if ($got_word) { } 5697 2012 7709
elsif ($input =~ /^$NONSPACE/u) { } 691 1321 2012
150 $spacing = $input =~ /^$NONSPACE/
151 ? $self->{_spacing_infix}
152 5697 ▶ : $self->{_spacing_postfix};
Branch: 1 branch
BranchTrueFalseTotal
$input =~ /^$NONSPACE/u ? : 527 5170 5697
153 }
154 # do we still have more tokens?
155 elsif ($input =~ /^$NONSPACE/) {
156 691 $spacing = $self->{_spacing_prefix};
157 }
158  
159 7709 push @tokens, [$spacing, $non_word];
160 }
161 }
162 }
163  
164 5177 return \@tokens;
165 }
166  
167 # tokens -> output
168 sub make_output {
169 1300 ▶ my ($self, $tokens) = @_;
Subroutine make_output: called CC=8 SCAR=20.8
Pod undocumented
170 1300 my $reply = '';
171  
172 1300 for my $pos (0 .. $#{ $tokens }) {
173 13813 my ($spacing, $text) = @{ $tokens->[$pos] };
174 13813 $reply .= $text;
175  
176 # append whitespace if this is not a prefix token or infix token,
177 # and this is not the last token, and the next token is not
178 # a postfix/infix token
179 13813 ▶ if ($pos != $#{ $tokens }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != $#{$tokens;} and $spacing != $self->{"_spacing_prefix"} and $spacing != $self->{"_spacing_infix"} and not $pos < $#{$tokens;} && ($$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} || $$tokens[$pos + 1][0] == $self->{"_spacing_infix"})) 9793 4020 13813
Condition91%11/12
$pos != $#{$tokens;} and $spacing != $self->{"_spacing_prefix"} and $spacing != $self->{"_spacing_infix"} and not $pos < $#{$tokens;} && ($$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} || $$tokens[$pos + 1][0] == $self->{"_spacing_infix"})
!ll&&!rl&&r
170223189793
$pos != $#{$tokens;} and $spacing != $self->{"_spacing_prefix"} and $spacing != $self->{"_spacing_infix"}
!ll&&!rl&&r
152917312111
$pos != $#{$tokens;} and $spacing != $self->{"_spacing_prefix"}
!ll&&!rl&&r
130022912284
$pos < $#{$tokens;} && ($$tokens[$pos + 1][0] == $self->{'_spacing_postfix'} || $$tokens[$pos + 1][0] == $self->{'_spacing_infix'})
!ll&&!rl&&r
097932318
MC/DC80%4/5
$pos != $#{$tokens;} and $spacing != $self->{"_spacing_prefix"} and $spacing != $self->{"_spacing_infix"} and not $pos < $#{$tokens;} && ($$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} || $$tokens[$pos + 1][0] == $self->{"_spacing_infix"})
$pos != $#{$tokens;} $spacing != $self->{"_spacing_prefix"} $spacing != $self->{"_spacing_infix"} $pos < $#{$tokens;} ($$tokens[$pos + 1][0] == $self->{'_spacing_postfix'} || $$tokens[$pos + 1][0] == $self->{'_spacing_infix'})
Truth table83%5/6
A and B and C and not(D && E)
A= $pos != $#{$tokens;}B= $spacing != $self->{"_spacing_prefix"}C= $spacing != $self->{"_spacing_infix"}D= $pos < $#{$tokens;}E= ($$tokens[$pos + 1][0] == $self->{'_spacing_postfix'} || $$tokens[$pos + 1][0] == $self->{'_spacing_infix'})
ABCDEresult
0XXXX0
10XXX0
110XX0
111110
1110X1
111101
180 && $spacing != $self->{_spacing_prefix}
181 && $spacing != $self->{_spacing_infix}
182 12111 ▶ && !($pos < $#{ $tokens }
Condition100%3/3
$$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} or $$tokens[$pos + 1][0] == $self->{"_spacing_infix"}
l!l&&r!l&&!r
21451739793
MC/DC100%2/2
$$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} or $$tokens[$pos + 1][0] == $self->{"_spacing_infix"}
$$tokens[$pos + 1][0] == $self->{"_spacing_postfix"} $$tokens[$pos + 1][0] == $self->{"_spacing_infix"}
Truth table100%3/3
A or B
A= $$tokens[$pos + 1][0] == $self->{"_spacing_postfix"}B= $$tokens[$pos + 1][0] == $self->{"_spacing_infix"}
ABresult
1X1
011
000
183 && ($tokens->[$pos+1][0] == $self->{_spacing_postfix}
184 || $tokens->[$pos+1][0] == $self->{_spacing_infix})
185 )
186 ) {
187 9793 $reply .= ' ';
188 }
189 }
190  
191 # capitalize the first word
192 1300 $reply =~ s/^\s*$OPEN_QUOTE?\s*\K($SPLIT_WORD)(?=$ELLIPSIS|(?:(?:$CLOSE_QUOTE|$TERMINATOR|$ADDRESS|$PUNCTUATION+)?(?:\s|$)))/\u$1/o;
193  
194 # capitalize the second word
195 1300 $reply =~ s/^\s*$OPEN_QUOTE?\s*$SPLIT_WORD(?:(?:\s*$TERMINATOR|$ADDRESS)\s+)\K($SPLIT_WORD)/\u$1/o;
196  
197 # capitalize all other words after word boundaries
198 # we do it in two passes because we need to match two words at a time
199 1300 $reply =~ s/(?:$ELLIPSIS|\s+)$OPEN_QUOTE?\s*$WORD_STRICT$BOUNDARY\K($SPLIT_WORD)/$SEPARATOR\u$1$SEPARATOR/go;
200 1300 $reply =~ s/$SEPARATOR$WORD_STRICT$SEPARATOR$BOUNDARY\K($SPLIT_WORD)/\u$1/go;
201 1300 $reply =~ s/$SEPARATOR//go;
202  
203 # end paragraphs with a period when it makes sense
204 1300 $reply =~ s/(?:$ELLIPSIS|\s+|^)$OPEN_QUOTE?(?:$SPLIT_WORD(?:\.$SPLIT_WORD)*)\K($CLOSE_QUOTE?)$/.$1/o;
205  
206 # capitalize I'm, I've...
207 1300 $reply =~ s{(?:(?:$ELLIPSIS|\s+)|$OPEN_QUOTE)\Ki(?=$APOSTROPHE$ALPHABET)}{I}go;
208  
209 1300 return $reply;
210 }
211  
212 29 __PACKAGE__->meta->make_immutable;
213  
214 =encoding utf8
215  
216 =head1 NAME
217  
218 Hailo::Tokenizer::Words - A tokenizer for L<Hailo|Hailo> which splits
219 on whitespace and word boundaries, mostly.
220  
221 =head1 DESCRIPTION
222  
223 This tokenizer does its best to handle various languages. It knows about most
224 apostrophes, quotes, and sentence terminators.
225  
226 =head1 AUTHOR
227  
228 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
229  
230 =head1 LICENSE AND COPYRIGHT
231  
232 Copyright 2010 Hinrik E<Ouml>rn SigurE<eth>sson
233  
234 This program is free software, you can redistribute it and/or modify
235 it under the same terms as Perl itself.
236  
237 =cut
« blib/lib/Hailo/Storage/MySQL.pm ↑ Summary blib/lib/Hailo/Command.pm »