| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition55%5/9
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl and push @ends, pos $_
/$separator_re/gl and push @starts, pos $_ and /$token_re/gl
/$separator_re/gl and push @starts, pos $_
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 $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||