blib/lib/Search/Tokenizer.pm

filter: Statementstmt 92.1% 70 / 76 Branchbran 76.3% 29 / 38 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 76.9% 10 / 13 Podpod 83.3% 5 / 6 totaltotal 82.8% 121 / 146 CC 24 SCAR 19.0
CC
24
Cov
85%
new
11.4
__ANON__
9.2
unaccent
3.2
CRAP 6.7
↑ Summary
1 package Search::Tokenizer;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Unicode::CaseFold qw(fc); # because CORE::fc only came with Perl 5.16
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.03';
8  
9 #======================================================================
10 # MAIN FUNCTIONALITY
11 #======================================================================
12  
13 2 ▶sub new {
Subroutine new: called CC=11 SCAR=24.3
Pod undocumented
14 2 my $class = shift;
15  
16 # defaults
17 2 my $regex = qr/\p{Word}+/;
18 2 my $lower = 1;
19 2 my $filter = undef;
20 2 my $filter_in_place = undef;
21 2 my $stopwords = undef;
22  
23 # parse arguments
24 2 ▶ unshift @_, "regex" if @_ == 1; # positional API
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 2 2
25 2 ▶ while (my ($arg, $val) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 7 2 9
26 7 ▶ $arg .= "=>" . (ref($val) || "SCALAR");
Condition100%2/2
ref $val || 'SCALAR'
l!l
61
MC/DC100%1/1
ref $val || 'SCALAR'
ref $val
Truth table100%2/2
A
A= ref $val
Aresult
11
00
27 CHECK:
28 7 for ($arg) {
29 7 ▶ /^regex=>Regexp$/ and do { $regex = $val; last CHECK};
Branch: 1 branch
BranchTrueFalseTotal
/^regex=>Regexp$/ and do { $regex = $val; last CHECK } 2 5 7
30 5 ▶ /^lower=>SCALAR$/ and do { $lower = !!$val; last CHECK};
Branch: 1 branch
BranchTrueFalseTotal
/^lower=>SCALAR$/ and do { $lower = !!$val; last CHECK } 1 4 5
31 0 ▶ /^filter=>CODE$/ and do { $filter = $val; last CHECK};
Branch: 1 branch
BranchTrueFalseTotal
/^filter=>CODE$/ and do { $filter = $val; last CHECK } 0 4 4
32 4 ▶ /^filter_in_place=>CODE$/ and do { $filter_in_place = $val; last CHECK};
Branch: 1 branch
BranchTrueFalseTotal
/^filter_in_place=>CODE$/ and do { $filter_in_place = $val; last CHECK } 2 2 4
33 2 ▶ /^stopwords=>HASH$/ and do { $stopwords = $val; last CHECK};
Branch: 1 branch
BranchTrueFalseTotal
/^stopwords=>HASH$/ and do { $stopwords = $val; last CHECK } 2 0 2
34 0 croak "Invalid option or invalid operand: $arg";
35 }
36 }
37  
38 # check that regex doest not match the empty string
39 2 ▶ not "" =~ $regex
Branch: 1 branch
BranchTrueFalseTotal
"" =~ /$regex/ and croak("regex $regex matches the empty string: cannot tokenize") 0 2 2
40 or croak "regex $regex matches the empty string: cannot tokenize";
41  
42 # return tokenizer factory: closure
43 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
44 3 my ($string, @other_args) = @_;
45 not @other_args
46 3 ▶ or croak "too many args -- just a single string is expected";
Branch: 1 branch
BranchTrueFalseTotal
@other_args and croak("too many args -- just a single string is expected") 0 3 3
47  
48 3 my $term_index = -1;
49  
50 # return tokenizer : additional closure on $string and $term_index
51 10 ▶ return sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
52  
53 # get next occurrence of $regex in $string (thanks to the /g flag)
54 10 ▶ while ($string =~ /$regex/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regex/g) 12 3 15
55  
56 # index of this term within the input string
57 12 $term_index += 1;
58  
59 # boundaries for the match
60 12 my $end = pos($string);
61 12 my $term = $&; # used to be slow in older perls, but now OK
62 12 my $start = $end - length($term);
63  
64 # the old way used to be as follows, but it is ridiculously slow on utf8 strings
65 # .. see https://github.com/Perl/perl5/issues/18786
66 #
67 # my ($start, $end) = ($-[0], $+[0]);
68 # my $term = substr($string, $start, $end-$start);
69  
70 # apply filtering and stopwords, if any
71 12 ▶ $term = Unicode::CaseFold::fc($term) if $lower;
Branch: 1 branch
BranchTrueFalseTotal
if $lower 8 4 12
72 12 ▶ $term = $filter->($term) if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 0 12 12
73 12 ▶ $filter_in_place->($term) if $filter_in_place;
Branch: 1 branch
BranchTrueFalseTotal
if $filter_in_place 12 0 12
74 12 ▶ undef $term if $stopwords and $stopwords->{$term};
Branch: 1 branch
BranchTrueFalseTotal
if $stopwords and $stopwords->{$term} 5 7 12
Condition66%2/3
$stopwords and $stopwords->{$term}
!ll&&!rl&&r
075
MC/DC50%1/2
$stopwords and $stopwords->{$term}
$stopwords $stopwords->{$term}
Truth table66%2/3
A and B
A= $stopwordsB= $stopwords->{$term}
ABresult
0X0
100
111
75  
76 # if $term was not cancelled by filters above, return it
77 12 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) 7 5 12
78 7 ▶ return wantarray ? ($term, length($term), $start, $end, $term_index)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
79 : $term;
80 }
81 } # otherwise, loop again to extract next term
82  
83 # otherwise, that's the end of the input string, return undef or empty list
84 3 return;
85 3 };
86 2 };
87 }
88  
89 #======================================================================
90 # BUILTIN TOKENIZERS
91 #======================================================================
92  
93  
94 0 ▶sub word {
Subroutine word: not called CC=1 SCAR=6.9
Pod documented
95 0 __PACKAGE__->new(regex => qr/\w+/, @_);
96 }
97  
98 0 ▶sub word_locale {
Subroutine word_locale: not called CC=1 SCAR=0.2
Pod documented
99 1 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 0 __PACKAGE__->new(regex => qr/\w+/, @_);
101 }
102  
103 0 ▶sub word_unicode {
Subroutine word_unicode: not called CC=1 SCAR=6.9
Pod documented
104 0 __PACKAGE__->new(regex => qr/\p{Word}+/, @_);
105 }
106  
107 1 ▶sub unaccent {
Subroutine unaccent: called CC=3 SCAR=11.6
Pod documented
108 1 require Text::Transliterator::Unaccent;
109 1 my %args = @_;
110 1 ▶ my $want_lower = !exists $args{lower} || $args{lower};
Condition33%1/3
!exists $args{'lower'} || $args{'lower'}
l!l&&r!l&&!r
100
MC/DC0%0/2
!exists $args{'lower'} || $args{'lower'}
!exists $args{'lower'} $args{'lower'}
Truth table33%1/3
not(A) || B
A= !exists $args{'lower'}B= $args{'lower'}
ABresult
1X1
011
000
111 1 ▶ my %unaccenter_args = $want_lower ? () : (upper => 0);
Branch: 1 branch
BranchTrueFalseTotal
$want_lower ? : 1 0 1
112 1 my $unaccenter = Text::Transliterator::Unaccent->new(%unaccenter_args);
113 1 __PACKAGE__->new(regex => qr/\p{Word}+/,
114 filter_in_place => $unaccenter,
115 %args);
116 }
117  
118  
119 #======================================================================
120 # UTILITY FUNCTION
121 #======================================================================
122  
123 3 ▶sub unroll {
Subroutine unroll: called CC=3 SCAR=11.0
Pod documented
124 3 my $iterator = shift;
125 3 my $no_details = shift;
126 3 my @results;
127  
128 3 ▶ while (my @r = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@r) = &$iterator()) 7 3 10
129 7 ▶ push @results, $no_details ? $r[0] : \@r;
Branch: 1 branch
BranchTrueFalseTotal
$no_details ? : 4 3 7
130 }
131 3 return @results;
132 }
133  
134  
135 1 1; # End of Search::Tokenizer
136  
137  
138 __END__
↑ Summary