| 1 | package Plucene::Analysis::CharTokenizer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Plucene::Analysis::CharTokenizer - base class for character tokenisers | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # isa Plucene::Analysis::Tokenizer | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $next = $chartokenizer->next; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | This is an abstract base class for simple, character-oriented tokenizers. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 METHODS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 19 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | ▶ | use Plucene::Analysis::Token; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 19 | ▶ | use base 'Plucene::Analysis::Tokenizer'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head2 token_re | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This should be defined in subclasses. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # And here we deviate from the script | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub token_re { die "You should define this" } | ||||||||||||||||||||||||||
|
Subroutine
token_re: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Class::Virtually::Abstract doesn't like being called twice. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 normalize | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This will normalise the character before it is added to the token. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 402 | ▶ | sub normalize { return $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
normalize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 next | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $next = $chartokenizer->next; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | This will return the next token in the string, or undef at the end | ||||||||||||||||||||||||||||
| 54 | of the string. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 144605 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 144605 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 144605 | my $re = $self->token_re(); | |||||||||||||||||||||||||||
| 61 | 144605 | my $fh = $self->{reader}; | |||||||||||||||||||||||||||
| 62 | retry: | ||||||||||||||||||||||||||||
| 63 | 159746 | ▶ | if (!defined $self->{buffer} or !length $self->{buffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"buffer"} and length $self->{"buffer"}
MC/DC100%2/2
defined $self->{"buffer"} and length $self->{"buffer"}
defined $self->{"buffer"}
length $self->{"buffer"}
Truth table100%3/3
A and B
A= defined $self->{"buffer"}B= length $self->{"buffer"}
| |||||||||||||||||||||||||||||
| 64 | 16278 | ▶ | return if eof($fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 15683 | $self->{start} = tell($fh); | |||||||||||||||||||||||||||
| 66 | 15683 | $self->{buffer} .= <$fh>; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 159151 | ▶ | return unless length $self->{buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 159151 | ▶ | if ($self->{buffer} =~ s/(.*?)($re)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 144010 | $self->{start} += length $1; | |||||||||||||||||||||||||||
| 72 | 144010 | my $word = $self->normalize($2); | |||||||||||||||||||||||||||
| 73 | my $rv = Plucene::Analysis::Token->new( | ||||||||||||||||||||||||||||
| 74 | text => $word, | ||||||||||||||||||||||||||||
| 75 | start => $self->{start}, | ||||||||||||||||||||||||||||
| 76 | 144010 | end => ($self->{start} + length($word))); | |||||||||||||||||||||||||||
| 77 | 144010 | $self->{start} += length($word); | |||||||||||||||||||||||||||
| 78 | 144010 | return $rv; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # No match, rest of buffer is useless. | ||||||||||||||||||||||||||||
| 82 | 15141 | $self->{buffer} = ""; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # But we should try for some more text | ||||||||||||||||||||||||||||
| 85 | 15141 | goto retry; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 19 | 1; | |||||||||||||||||||||||||||