/usr/lib/perl5/KinoSearch1/Analysis/Tokenizer.pm is in libkinosearch1-perl 1.00-1build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | package KinoSearch1::Analysis::Tokenizer;
use strict;
use warnings;
use KinoSearch1::Util::ToolSet;
use base qw( KinoSearch1::Analysis::Analyzer );
use locale;
BEGIN {
__PACKAGE__->init_instance_vars(
# constructor params / members
token_re => undef, # regex for a single token
# members
separator_re => undef, # regex for separations between tokens
);
}
use KinoSearch1::Analysis::TokenBatch;
sub init_instance {
my $self = shift;
# supply defaults if token_re wasn't specified
if ( !defined $self->{token_re} ) {
$self->{token_re} = qr/\b\w+(?:'\w+)?\b/;
$self->{separator_re} = qr/\W*/;
}
# if user-defined token_re...
if ( !defined $self->{separator_re} ) {
# define separator using lookahead
$self->{separator_re} = qr/
.*? # match up to...
(?= # but not including...
$self->{token_re} # a token,
|\z # or the end of the string
)/xsm;
}
}
sub analyze {
my ( $self, $batch ) = @_;
my $new_batch = KinoSearch1::Analysis::TokenBatch->new;
my $token_re = $self->{token_re};
my $separator_re = $self->{separator_re};
# alias input to $_
while ( $batch->next ) {
local $_ = $batch->get_text;
# ensure that pos is set to 0 for this scalar
pos = 0;
# accumulate token start_offsets and end_offsets
my ( @starts, @ends );
1 while ( m/$separator_re/g and push @starts,
pos and m/$token_re/g and push @ends, pos );
# correct for overshoot
$#starts = $#ends;
# add the new tokens to the batch
$new_batch->add_many_tokens( $_, \@starts, \@ends );
}
return $new_batch;
}
1;
__END__
=head1 NAME
KinoSearch1::Analysis::Tokenizer - customizable tokenizing
=head1 SYNOPSIS
my $whitespace_tokenizer
= KinoSearch1::Analysis::Tokenizer->new( token_re => qr/\S+/, );
# or...
my $word_char_tokenizer
= KinoSearch1::Analysis::Tokenizer->new( token_re => qr/\w+/, );
# or...
my $apostrophising_tokenizer = KinoSearch1::Analysis::Tokenizer->new;
# then... once you have a tokenizer, put it into a PolyAnalyzer
my $polyanalyzer = KinoSearch1::Analysis::PolyAnalyzer->new(
analyzers => [ $lc_normalizer, $word_char_tokenizer, $stemmer ], );
=head1 DESCRIPTION
Generically, "tokenizing" is a process of breaking up a string into an array
of "tokens".
# before:
my $string = "three blind mice";
# after:
@tokens = qw( three blind mice );
KinoSearch1::Analysis::Tokenizer decides where it should break up the text
based on the value of C<token_re>.
# before:
my $string = "Eats, Shoots and Leaves.";
# tokenized by $whitespace_tokenizer
@tokens = qw( Eats, Shoots and Leaves. );
# tokenized by $word_char_tokenizer
@tokens = qw( Eats Shoots and Leaves );
=head1 METHODS
=head2 new
# match "O'Henry" as well as "Henry" and "it's" as well as "it"
my $token_re = qr/
\b # start with a word boundary
\w+ # Match word chars.
(?: # Group, but don't capture...
'\w+ # ... an apostrophe plus word chars.
)? # Matching the apostrophe group is optional.
\b # end with a word boundary
/xsm;
my $tokenizer = KinoSearch1::Analysis::Tokenizer->new(
token_re => $token_re, # default: what you see above
);
Constructor. Takes one hash style parameter.
=over
=item *
B<token_re> - must be a pre-compiled regular expression matching one token.
=back
=head1 COPYRIGHT
Copyright 2005-2010 Marvin Humphrey
=head1 LICENSE, DISCLAIMER, BUGS, etc.
See L<KinoSearch1> version 1.00.
=cut
|