blib/lib/KinoSearch1/Analysis/Tokenizer.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 8 / 8 Conditioncond 55.5% 5 / 9 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 90.0% 63 / 70 CC 8 SCAR 12.0
CC
8
Cov
93%
analyze
6.1
init_instance
3.0
BEGIN
1.0
CRAP 3.3
« buildlib/KinoSearch1/Test/TestUtils.pm ↑ Summary blib/lib/KinoSearch1/Search/Hits.pm »
1 package KinoSearch1::Analysis::Tokenizer;
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use KinoSearch1::Util::ToolSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use base qw( KinoSearch1::Analysis::Analyzer );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 34 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 __PACKAGE__->init_instance_vars(
10  
11 # constructor params / members
12 token_re => undef, # regex for a single token
13  
14 # members
15 separator_re => undef, # regex for separations between tokens
16 );
17 }
18  
19 34 ▶use KinoSearch1::Analysis::TokenBatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 43 ▶sub init_instance {
Subroutine init_instance: called CC=3 SCAR=11.0
Pod documented
22 43 my $self = shift;
23  
24 # supply defaults if token_re wasn't specified
25 43 ▶ if ( !defined $self->{token_re} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"token_re"}) 39 4 43
26 39 $self->{token_re} = qr/\b\w+(?:'\w+)?\b/;
27 39 $self->{separator_re} = qr/\W*/;
28 }
29  
30 # if user-defined token_re...
31 43 ▶ if ( !defined $self->{separator_re} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"separator_re"}) 4 39 43
32  
33 # define separator using lookahead
34 4 $self->{separator_re} = qr/
35 .*? # match up to...
36 (?= # but not including...
37 $self->{token_re} # a token,
38 |\z # or the end of the string
39 )/xsm;
40 }
41 }
42  
43 15850 ▶sub analyze {
Subroutine analyze: called CC=6 SCAR=18.2
Pod documented
44 15850 my ( $self, $batch ) = @_;
45  
46 15850 my $new_batch = KinoSearch1::Analysis::TokenBatch->new;
47 15850 my $token_re = $self->{token_re};
48 15850 my $separator_re = $self->{separator_re};
49  
50 # alias input to $_
51 15850 ▶ while ( $batch->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch->next) 15778 15850 31628
52 15778 local $_ = $batch->get_text;
53  
54 # ensure that pos is set to 0 for this scalar
55 15778 pos = 0;
56  
57 # accumulate token start_offsets and end_offsets
58 15778 my ( @starts, @ends );
59 15778 ▶ 1 while ( m/$separator_re/g and push @starts,
Branch: 1 branch
BranchTrueFalseTotal
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl and push @ends, pos $_ and do { '???' } 41637 15778 57415
Condition55%5/9
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl and push @ends, pos $_
!ll&&!rl&&r
15778041637
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl
!ll&&!rl&&r
01577841637
/$separator_re/gl and push @starts, pos $_
!ll&&!rl&&r
0057415
MC/DC25%1/4
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl and push @ends, pos $_
/$separator_re/gl push @starts, pos $_ /$token_re/gl push @ends, pos $_
Truth table40%2/5
A and B and C and D
A= /$separator_re/glB= push @starts, pos $_C= /$token_re/glD= push @ends, pos $_
ABCDresult
0XXX0
10XX0
110X0
11100
11111
60 pos and m/$token_re/g and push @ends, pos );
61  
62 # correct for overshoot
63 15778 $#starts = $#ends;
64  
65 # add the new tokens to the batch
66 15778 $new_batch->add_many_tokens( $_, \@starts, \@ends );
67 }
68  
69 15850 return $new_batch;
70 }
71  
72 34 1;
73  
74 __END__
« buildlib/KinoSearch1/Test/TestUtils.pm ↑ Summary blib/lib/KinoSearch1/Search/Hits.pm »