| 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
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | while (my ($arg, $val) = splice(@_, 0, 2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 7 | ▶ | $arg .= "=>" . (ref($val) || "SCALAR"); | ||||||||||||||||||||||||||
|
Condition100%2/2
ref $val || 'SCALAR'
MC/DC100%1/1
ref $val || 'SCALAR'
ref $val
Truth table100%2/2
A
A= ref $val
| |||||||||||||||||||||||||||||
| 27 | CHECK: | ||||||||||||||||||||||||||||
| 28 | 7 | for ($arg) { | |||||||||||||||||||||||||||
| 29 | 7 | ▶ | /^regex=>Regexp$/ and do { $regex = $val; last CHECK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | /^lower=>SCALAR$/ and do { $lower = !!$val; last CHECK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | /^filter=>CODE$/ and do { $filter = $val; last CHECK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | /^filter_in_place=>CODE$/ and do { $filter_in_place = $val; last CHECK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | /^stopwords=>HASH$/ and do { $stopwords = $val; last CHECK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | $term = $filter->($term) if $filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 12 | ▶ | $filter_in_place->($term) if $filter_in_place; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 12 | ▶ | undef $term if $stopwords and $stopwords->{$term}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stopwords and $stopwords->{$term}
MC/DC50%1/2
$stopwords and $stopwords->{$term}
$stopwords
$stopwords->{$term}
Truth table66%2/3
A and B
A= $stopwordsB= $stopwords->{$term}
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # if $term was not cancelled by filters above, return it | ||||||||||||||||||||||||||||
| 77 | 12 | ▶ | if ($term) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 7 | ▶ | return wantarray ? ($term, length($term), $start, $end, $term_index) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | my %unaccenter_args = $want_lower ? () : (upper => 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 129 | 7 | ▶ | push @results, $no_details ? $r[0] : \@r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 3 | return @results; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | 1; # End of Search::Tokenizer | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||