| 1 | package Algorithm::AhoCorasick::SearchMachine; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 129 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 7 | 129 | my $class = shift; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 129 | ▶ | if (!@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 10 | 4 | die "no keywords"; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 125 | my %keywords; | |||||||||||||||||||||||||||
| 14 | 125 | foreach (@_) { | |||||||||||||||||||||||||||
| 15 | 396 | ▶ | if (!defined($_) || ($_ eq '')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_ or $_ eq ""
MC/DC50%1/2
not defined $_ or $_ eq ""
not defined $_
$_ eq ""
Truth table66%2/3
not(A) or B
A= not defined $_B= $_ eq ""
| |||||||||||||||||||||||||||||
| 16 | 2 | die "empty keyword"; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 394 | $keywords{$_} = 1; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 123 | my $self = { keywords => [ keys %keywords ] }; | |||||||||||||||||||||||||||
| 23 | 123 | bless $self, $class; | |||||||||||||||||||||||||||
| 24 | 123 | $self->_build_tree(); | |||||||||||||||||||||||||||
| 25 | 123 | return $self; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 123 | ▶ | sub _build_tree { | ||||||||||||||||||||||||||
|
Subroutine
_build_tree: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 29 | 123 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 123 | $self->{root} = Algorithm::AhoCorasick::Node->new(); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # build transition links | ||||||||||||||||||||||||||||
| 34 | 123 | foreach my $p (@{$self->{keywords}}) { | |||||||||||||||||||||||||||
| 35 | 338 | my $nd = $self->{root}; | |||||||||||||||||||||||||||
| 36 | 338 | foreach my $c (split //, $p) { | |||||||||||||||||||||||||||
| 37 | 819 | my $ndNew = $nd->get_transition($c); | |||||||||||||||||||||||||||
| 38 | 819 | ▶ | if (!$ndNew) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 806 | $ndNew = Algorithm::AhoCorasick::Node->new(parent => $nd, char => $c); | |||||||||||||||||||||||||||
| 40 | 806 | $nd->add_transition($ndNew); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 819 | $nd = $ndNew; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 338 | $nd->add_result($p); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # build failure links | ||||||||||||||||||||||||||||
| 50 | 123 | my @nodes; | |||||||||||||||||||||||||||
| 51 | 123 | foreach my $nd ($self->{root}->transitions) { | |||||||||||||||||||||||||||
| 52 | 327 | $nd->failure($self->{root}); | |||||||||||||||||||||||||||
| 53 | 327 | push @nodes, $nd->transitions; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 123 | ▶ | while (@nodes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 369 | my @newNodes; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 369 | foreach my $nd (@nodes) { | |||||||||||||||||||||||||||
| 60 | 479 | my $r = $nd->parent->failure; | |||||||||||||||||||||||||||
| 61 | 479 | my $c = $nd->char; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 479 | ▶ | while ($r && !($r->get_transition($c))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r and not $r->get_transition($c)
MC/DC100%2/2
$r and not $r->get_transition($c)
$r
not $r->get_transition($c)
Truth table100%3/3
A and not(B)
A= $rB= not $r->get_transition($c)
| |||||||||||||||||||||||||||||
| 64 | 173 | $r = $r->failure; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 479 | ▶ | if (!$r) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 171 | $nd->failure($self->{root}); | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 308 | my $tc = $r->get_transition($c); | |||||||||||||||||||||||||||
| 71 | 308 | $nd->failure($tc); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 308 | foreach my $result ($tc->results) { | |||||||||||||||||||||||||||
| 74 | 306 | $nd->add_result($result); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 479 | push @newNodes, $nd->transitions; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 369 | @nodes = @newNodes; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 123 | $self->{root}->failure($self->{root}); | |||||||||||||||||||||||||||
| 85 | 123 | $self->{state} = $self->{root}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 124 | ▶ | sub feed { | ||||||||||||||||||||||||||
|
Subroutine
feed: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 124 | my ($self, $text, $callback) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 124 | my $index = 0; | |||||||||||||||||||||||||||
| 92 | 124 | my $l = length($text); | |||||||||||||||||||||||||||
| 93 | 124 | ▶ | while ($index < $l) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 868 | my $trans = undef; | |||||||||||||||||||||||||||
| 95 | 868 | while (1) { | |||||||||||||||||||||||||||
| 96 | 916 | $trans = $self->{state}->get_transition(substr($text, $index, 1)); | |||||||||||||||||||||||||||
| 97 | 916 | ▶ | last if ($self->{state} == $self->{root}) || $trans; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"state"} == $self->{"root"} or $trans
MC/DC100%2/2
$self->{"state"} == $self->{"root"} or $trans
$self->{"state"} == $self->{"root"}
$trans
Truth table100%3/3
A or B
A= $self->{"state"} == $self->{"root"}B= $trans
| |||||||||||||||||||||||||||||
| 98 | 48 | $self->{state} = $self->{state}->failure; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 868 | ▶ | if ($trans) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 510 | $self->{state} = $trans; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 868 | foreach my $found ($self->{state}->results) { | |||||||||||||||||||||||||||
| 106 | 248 | my $rv = &$callback($index - length($found) + 1, $found); | |||||||||||||||||||||||||||
| 107 | 248 | ▶ | if ($rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 57 | return $rv; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 811 | ++$index; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 67 | return undef; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | package Algorithm::AhoCorasick::Node; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 5 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 929 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 125 | 929 | my $class = shift; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 929 | my $self = { @_ }; | |||||||||||||||||||||||||||
| 128 | 929 | $self->{results} = { }; | |||||||||||||||||||||||||||
| 129 | 929 | $self->{transitions} = { }; | |||||||||||||||||||||||||||
| 130 | 929 | ▶ | weaken $self->{parent} if $self->{parent}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 929 | return bless $self, $class; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1285 | ▶ | sub char { | ||||||||||||||||||||||||||
|
Subroutine
char: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 135 | 1285 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1285 | ▶ | if (!exists($self->{char})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | die "root node has no character"; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1285 | return $self->{char}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 479 | ▶ | sub parent { | ||||||||||||||||||||||||||
|
Subroutine
parent: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 145 | 479 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 479 | ▶ | if (!exists($self->{parent})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | die "root node has no parent"; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 479 | return $self->{parent}; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1629 | ▶ | sub failure { | ||||||||||||||||||||||||||
|
Subroutine
failure: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 1629 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1629 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 929 | $self->{failure} = $_[0]; | |||||||||||||||||||||||||||
| 159 | 929 | weaken $self->{failure}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1629 | return $self->{failure}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Returns transition to the specified character, or undef. | ||||||||||||||||||||||||||||
| 166 | 2524 | ▶ | sub get_transition { | ||||||||||||||||||||||||||
|
Subroutine
get_transition: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 2524 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2524 | return $self->{transitions}->{$c}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Returns a list of descendant nodes. | ||||||||||||||||||||||||||||
| 173 | 929 | ▶ | sub transitions { | ||||||||||||||||||||||||||
|
Subroutine
transitions: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 929 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 929 | return values %{$self->{transitions}}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Returns a list of patterns ending in this node, longest first. | ||||||||||||||||||||||||||||
| 180 | 1176 | ▶ | sub results { | ||||||||||||||||||||||||||
|
Subroutine
results: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 181 | 1176 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1176 | ▶ | return sort { length($b) <=> length($a) || $a cmp $b } keys %{$self->{results}}; | ||||||||||||||||||||||||||
|
Condition33%1/3
length $b <=> length $a || $a cmp $b
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a
$a cmp $b
Truth table33%1/3
A || B
A= length $b <=> length $aB= $a cmp $b
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Adds pattern ending in this node. | ||||||||||||||||||||||||||||
| 187 | 644 | ▶ | sub add_result { | ||||||||||||||||||||||||||
|
Subroutine
add_result: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | 644 | my ($self, $res) = @_; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 644 | $self->{results}->{$res} = 1; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # Adds transition node. | ||||||||||||||||||||||||||||
| 194 | 806 | ▶ | sub add_transition { | ||||||||||||||||||||||||||
|
Subroutine
add_transition: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | 806 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 806 | $self->{transitions}->{$node->char} = $node; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 5 | 1; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||||||||