NAME
Lingua::Anagrams - pure Perl anagram finder
VERSION
version 0.002
SYNOPSIS
use Lingua::Anagrams;
open my $fh, '<', 'wordsEn.txt' or die "Aargh! $!";
my @words = <$fh>;
close $fh;
my $anagramizer = Lingua::Anagrams->new( \@words ); # NOT a good word list for this purpose
my $t1 = time;
my @anagrams = $anagramizer->anagrams('Find anagrams!');
my $t2 = time;
print join ' ', @$_ for @anagrams;
print "\n\n";
print scalar(@anagrams) , " anagrams\n"";
print 'it took ' , ( $t2 - $t1 ) , " seconds\n"";
Giving you
...
naif nm rag sad
naif nm raga sd
naif nm rd saga
naif ragman sd
20906 anagrams
it took 3 seconds
DESCRIPTION
Lingua::Anagrams constructs a trie out of a list of words you give it. It then uses this trie to find all the anagrams of a phrase you give to its anagrams
method. A dynamic programming algorithm is used to accelerate at the cost of memory. See new
for how one may modify this algorithm.
Be aware that the anagram algorithm has been golfed down pretty far to squeeze more speed out of it. It isn't the prettiest.
METHODS
CLASS->new( $word_list, %params )
Construct a new anagram engine from a word list. The parameters understood by the constructor are
- limit
-
The character count limit used by the dynamic programming algorithm to throttle memory consumption somewhat. If you wish to find the anagrams of a very long phrase you may find the caching in the dynamic programming algorithm consumes too much memory. Set this limit lower to protect yourself from memory exhaustion (and slow things down).
The default limit is set by the global
$LIMIT
variable. It will be 20 unless you tinker with it. - clean
-
A code reference specifying how text is to be cleaned of extraneous characters and normalized. The default cleaning function is
sub _clean { $_[0] =~ s/\W+//g; $_[0] = lc $_[0]; }
Note that this function, like
_clean
, must modify its argument directly.
$self->anagrams( $phrase )
Returns a list of array references, each reference containing a list of words which together constitute an anagram of the phrase.
AUTHOR
David F. Houghton <dfhoughton@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by David F. Houghton.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.