| 1 | package Algorithm::AhoCorasick; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use Algorithm::AhoCorasick::SearchMachine; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | require Exporter; | |||||||||
| 9 | |||||||||||
| 10 | 4 | our @ISA = qw(Exporter); | |||||||||
| 11 | |||||||||||
| 12 | 4 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 13 | find_first | ||||||||||
| 14 | find_all | ||||||||||
| 15 | ) ] ); | ||||||||||
| 16 | |||||||||||
| 17 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 18 | |||||||||||
| 19 | 4 | our $VERSION = '0.04'; | |||||||||
| 20 | |||||||||||
| 21 | 61 | ▶ | sub find_first { | ||||||||
|
Subroutine
find_first: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 22 | 61 | my $text = shift; | |||||||||
| 23 | |||||||||||
| 24 | 61 | my $m = Algorithm::AhoCorasick::SearchMachine->new(@_); | |||||||||
| 25 | 58 | ▶ | my $rv = $m->feed($text, sub { [ @_ ]; }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 58 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 55 | ▶ | return $rv ? @$rv : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | } else { | ||||||||||
| 29 | 3 | ▶ | return $rv ? $rv : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | } | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 61 | ▶ | sub find_all { | ||||||||
|
Subroutine
find_all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 61 | my $text = shift; | |||||||||
| 35 | |||||||||||
| 36 | 61 | my $m = Algorithm::AhoCorasick::SearchMachine->new(@_); | |||||||||
| 37 | |||||||||||
| 38 | 58 | my %total; | |||||||||
| 39 | 181 | ▶ | my $handle_all = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 40 | 181 | my ($pos, $keyword) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 181 | ▶ | if (!exists($total{$pos})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 176 | $total{$pos} = [ ]; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 181 | push @{$total{$pos}}, $keyword; | |||||||||
| 47 | |||||||||||
| 48 | 181 | undef; | |||||||||
| 49 | 58 | }; | |||||||||
| 50 | |||||||||||
| 51 | 58 | $m->feed($text, $handle_all); | |||||||||
| 52 | |||||||||||
| 53 | 58 | ▶ | return keys(%total) ? \%total : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 4 | 1; | |||||||||
| 57 | |||||||||||
| 58 | __END__ | ||||||||||