zeroclickinfo-goodies/lib/DDG/Goodie/NLetterWords.pm

52 lines
1.4 KiB
Perl
Raw Normal View History

2012-04-15 22:58:41 -07:00
package DDG::Goodie::NLetterWords;
use DDG::Goodie;
use Lingua::EN::Numericalize;
2012-04-15 22:58:41 -07:00
triggers end => "words", "word";
zci is_cached => 0;
2012-04-15 22:58:41 -07:00
2012-11-06 12:03:19 -08:00
primary_example_queries '5 letter words';
secondary_example_queries '12 character word';
2012-11-06 09:58:47 -08:00
description 'find words of a certain length';
name 'NLetterWords';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/NLetterWords.pm';
2012-11-06 12:03:19 -08:00
topics 'words_and_games';
category 'language';
attribution github => ['http://github.com/nospampleasemam', 'nospampleasemam'],
2012-11-06 09:58:47 -08:00
web => ['http://dylansserver.com/', 'Dylan Lloyd'];
2012-04-15 22:58:41 -07:00
handle query_parts => sub {
my $numericalized = str2nbr($_);
return unless $numericalized =~ /^(\d{1,50}) (letter|char|character) words?$/;
2012-04-15 22:58:41 -07:00
my $length = $1;
my @allwords = share('words.txt')->slurp;
my @words;
2012-05-01 20:26:43 -07:00
for (@allwords) {
2012-04-15 22:58:41 -07:00
chomp($_);
if (length($_) == $length) { push(@words, $_); }
}
return unless @words;
my @randomwords;
if (scalar(@words) > 30) {
while (scalar(@randomwords) < 30) {
2012-05-01 20:26:43 -07:00
my $rand = int(rand(scalar(@words)));
2012-04-16 19:42:21 -07:00
if ($words[$rand]) {
push(@randomwords, $words[$rand]);
$words[$rand] = 0;
}
}
@words = @randomwords;
2012-04-15 22:58:41 -07:00
}
2012-04-23 10:21:11 -07:00
my $output = "Random $length letter words: " . join ', ', @words;
$output .= ".";
2012-04-15 22:58:41 -07:00
return $output;
};
1;