2012-04-15 22:58:41 -07:00
|
|
|
package DDG::Goodie::NLetterWords;
|
|
|
|
|
|
|
|
use DDG::Goodie;
|
2012-04-23 11:15:39 -07:00
|
|
|
use Lingua::EN::Numericalize;
|
|
|
|
|
2012-04-15 22:58:41 -07:00
|
|
|
triggers end => "words", "word";
|
2012-05-23 19:05:08 -07:00
|
|
|
|
2012-04-15 23:29:28 -07:00
|
|
|
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 {
|
2012-04-23 11:15:39 -07:00
|
|
|
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, $_); }
|
|
|
|
}
|
2012-04-15 23:29:28 -07:00
|
|
|
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;
|
2012-04-15 23:29:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@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;
|