2012-10-29 12:32:04 -07:00
|
|
|
package DDG::Goodie::FindAnagrams;
|
|
|
|
|
|
|
|
use DDG::Goodie;
|
|
|
|
use JSON;
|
2012-10-29 13:39:00 -07:00
|
|
|
#use Data::Dumper;
|
2012-10-29 12:32:04 -07:00
|
|
|
|
|
|
|
zci is_cached => 1;
|
|
|
|
|
2012-10-29 13:12:45 -07:00
|
|
|
triggers start => "find anagrams";
|
2012-10-29 12:32:04 -07:00
|
|
|
|
2012-10-29 13:52:20 -07:00
|
|
|
|
|
|
|
my $json = scalar share('words.json')->slurp;
|
|
|
|
|
|
|
|
my %wordHash = %{decode_json($json)};
|
|
|
|
|
|
|
|
#print Dumper(\%wordHash);
|
|
|
|
|
|
|
|
|
2012-10-29 12:32:04 -07:00
|
|
|
handle remainder => sub {
|
|
|
|
|
|
|
|
if ($_ eq ""){
|
|
|
|
return "No Anagrams Found."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Format string to look like hash key by making it lowercase then splitting the string into chars, sort them and finally join back into sorted string
|
|
|
|
my $sorted_string = join("",sort(split(//,lc($_))));
|
|
|
|
|
|
|
|
my @resultArray = ();
|
|
|
|
|
|
|
|
if (exists $wordHash{$sorted_string}) {
|
|
|
|
push(@resultArray, @{$wordHash{$sorted_string}});
|
|
|
|
} else {
|
|
|
|
return "No Anagrams Found.";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $index = 0;
|
|
|
|
|
|
|
|
$index++ until $resultArray[$index] eq $_;
|
|
|
|
|
|
|
|
splice(@resultArray, $index, 1);
|
|
|
|
|
|
|
|
my $result_string = join(",",@resultArray);
|
|
|
|
|
|
|
|
return (($result_string eq "") ? "No Anagrams Found!" : $result_string);
|
|
|
|
};
|
|
|
|
|
|
|
|
1;
|