File Coverage

blib/lib/Algorithm/AhoCorasick.pm
Criterion Covered Total %
statement 28 28 100.0
branch 10 10 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Algorithm::AhoCorasick;
2              
3 3     3   62929 use warnings;
  3         8  
  3         84  
4 3     3   15 use strict;
  3         7  
  3         91  
5              
6 3     3   1490 use Algorithm::AhoCorasick::SearchMachine;
  3         6  
  3         1357  
7              
8             require Exporter;
9              
10             our @ISA = qw(Exporter);
11              
12             our %EXPORT_TAGS = ( 'all' => [ qw(
13             find_first
14             find_all
15             ) ] );
16              
17             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18              
19             our $VERSION = '0.03';
20              
21             sub find_first {
22 10     10 1 8689 my $text = shift;
23              
24 10         48 my $m = Algorithm::AhoCorasick::SearchMachine->new(@_);
25 7     4   51 my $rv = $m->feed($text, sub { [ @_ ]; });
  4         14  
26 7 100       34 if (wantarray) {
27 4 100       54 return $rv ? @$rv : ();
28             } else {
29 3 100       34 return $rv ? $rv : undef;
30             }
31             }
32              
33             sub find_all {
34 9     9 1 6906 my $text = shift;
35              
36 9         42 my $m = Algorithm::AhoCorasick::SearchMachine->new(@_);
37              
38 6         9 my %total;
39             my $handle_all = sub {
40 14     14   22 my ($pos, $keyword) = @_;
41              
42 14 100       38 if (!exists($total{$pos})) {
43 13         35 $total{$pos} = [ ];
44             }
45              
46 14         16 push @{$total{$pos}}, $keyword;
  14         29  
47              
48 14         40 undef;
49 6         27 };
50              
51 6         19 $m->feed($text, $handle_all);
52              
53 6 100       99 return keys(%total) ? \%total : undef;
54             }
55              
56             1;
57              
58             __END__