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

47 lines
1.3 KiB
Perl
Raw Normal View History

2012-02-29 12:05:49 -08:00
package DDG::Goodie::ABC;
# ABSTRACT: Randomly pick one of different choices splitted by "or"
2012-02-29 12:05:49 -08:00
use DDG::Goodie;
triggers any => "or";
zci answer_type => "rand";
2012-11-06 12:19:24 -08:00
primary_example_queries 'yes or no';
secondary_example_queries 'this or that or none';
description 'make a random choice';
name 'ABC';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ABC.pm';
category 'random';
topics 'trivia';
attribution twitter => 'crazedpsyc',
cpan => 'CRZEDPSYC' ;
2012-02-29 12:05:49 -08:00
handle query_parts => sub {
my @choices;
my @collected_parts;
while (my $part = shift) {
if ( lc($part) eq 'or' ) {
return unless @collected_parts;
push @choices, join(' ', @collected_parts);
my $length = @collected_parts;
return if $length > 1;
@collected_parts = ();
} elsif ( $part ) {
push @collected_parts, $part;
}
}
push @choices, join(' ', @collected_parts) if @choices && @collected_parts;
2012-04-15 14:01:03 -07:00
return if scalar(@choices) <= 1;
2012-02-29 12:05:49 -08:00
my $choice = int(rand(@choices));
if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @choices) {
2012-02-29 12:05:49 -08:00
return $duck[0]." (not random)", answer_type => 'egg';
}
return $choices[$choice]." (random)";
return;
};
1;