Algorithm-UrataniTakeda
view release on metacpan or search on metacpan
lib/Algorithm/UrataniTakeda.pm view on Meta::CPAN
$q += $f;
last if $q > $n;
}
}
method matches( $text ) {
my @matches;
$self->search( $text, sub( $, $phrase ) { push @matches, $phrase; return 1; } );
return @matches;
}
method first( $text ) {
my $match;
$self->search( $text, sub( $, $phrase ) { $match = $phrase; return 0; } );
return $match;
}
method has_match( $text ) {
my $match = "";
$self->search( $text, sub( $, $ ) { $match = 1; return 0; } );
return $match;
}
ADJUST :params ( :$patterns ) {
if ( my @patterns = $patterns->@* ) {
Carp::croak sprintf( "Parameter 'patterns' cannot contain empty strings for \%s constructor", ref($self) )
if List::Util::any { !defined($_) || $_ eq "" } @patterns;
$self->$enter($_) for @patterns;
}
else {
Carp::croak sprintf("Parameter 'patterns' cannot be empty for \%s constructor", ref($self) );
}
$self->$build_phi;
$self->$build_shift1;
$self->$build_shift2;
}
sub BUILDARGS( $class, @args ) {
if ( @args == 1 && ref( $args[0] ) eq "ARRAY" ) {
return $class->SUPER::BUILDARGS( patterns => $args[0] );
}
return $class->SUPER::BUILDARGS(@args);
}
}
1;
__END__
=pod
=encoding UTF-8
=for Pod::Coverage DOES META new
=for Pod::Coverage BUILDARGS
=for stopwords Aho Commentz Corasick Takeda Uratani
=head1 NAME
Algorithm::UrataniTakeda - an implementation of the Uratani-Takeda string searching algorithm
=head1 VERSION
version v0.1.6
=head1 SYNOPSIS
use Algorithm::UrataniTakeda;
use experimental qw( signatures ); # for Perl versions before v5.36
my $m = Algorithm::UrataniTakeda->new( \@patterns );
my $match = $m->first($text);
my @all = $m->matches($text);
if ( $m->has_match($text) ) {
...
}
sub callback( $pos, $phrase ) {
...
return 1;
}
while (<STDIN>) {
$m->search( $_, \&callback );
}
=head1 STATUS
This is an experimental implementation.
It may not be correct.
=head1 DESCRIPTION
This is an implementation of the Uratani-Takeda algorithm for searching for multiple strings.
( run in 3.639 seconds using v1.01-cache-2.11-cpan-54e63673c56 )