Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 87 lines (69 sloc) 2.613 kB
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
1 package DDG::Goodie::ABC;
d0a2817 @kablamo Comments and minor cleanup for DDG::Goodie::ABC.
kablamo authored
2 # ABSTRACT: Randomly pick one of several different choices delimited by "or"
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
3
56c5ea4 @mintsoft Goodies: Enabling `use strict` on all goodies
mintsoft authored
4 use strict;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
5 use DDG::Goodie;
f3fb4fc @kablamo Added 'select' as a trigger word. +refactor, +tests
kablamo authored
6 use List::AllUtils qw/none/;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
7
3c60ff5 @kablamo DDG::Goodie::ABC - Removed @TRIGGERS var and added an attribution.
kablamo authored
8 triggers startend => qw/choose pick select/;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
9
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
10 zci answer_type => "choice";
11 zci is_cached => 0;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
12
cea9e54 @nospampleasemam updated ABC example queries
nospampleasemam authored
13 primary_example_queries 'choose yes or no';
30c4222 @kablamo Add pick as a trigger word for DDG::Goodie::ABC. +tests.
kablamo authored
14 secondary_example_queries 'choose heads or tails', 'pick this or that or none';
da04b53 @nospampleasemam added attribution to ABC goodie
nospampleasemam authored
15 description 'make a random choice';
16 name 'ABC';
17 code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ABC.pm';
18 category 'random';
19 topics 'trivia';
fa5a969 @russellholt attribution cleanup
russellholt authored
20 attribution cpan => ['CRZEDPSYC','crazedpsyc'],
21 github => ['kablamo', 'Eric Johnson'];
da04b53 @nospampleasemam added attribution to ABC goodie
nospampleasemam authored
22
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
23 handle remainder => sub {
1e2740a @moollaza Add srand() to other goodies using rand
moollaza authored
24
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
25 my $query = $_;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
26
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
27 # split the query on whitespace and rm whitespace
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
28 my @words = grep { length } split /\s+/, $query;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
29
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
30 return if query_is_malformed(@words);
31
32 # rm every 'or' from the list
33 my @choices = grep { lc $_ ne 'or' } @words;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
34
ad58556 @MrChrisW Update Case for ABC, Anagram, Ascii, AspectRatio, Base, Base64
MrChrisW authored
35 my $selection_type = 'Random';
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
36 my $selection;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
37 # Easter egg. For queries like:
38 # 'choose duckduckgo or google or bing or something'
60a51e5 Make "DuckDuckGo" in "DuckDuckGo or something" query case insensitive.
GlitchMr authored
39 if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @choices) {
ad58556 @MrChrisW Update Case for ABC, Anagram, Ascii, AspectRatio, Base, Base64
MrChrisW authored
40 $selection_type = 'Non-random';
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
41 $selection = $duck[0];
42 } else {
43 # Ensure rand is seeded for each process
44 srand();
45 # Choose randomly
46 $selection = $choices[int rand scalar @choices];
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
47 }
48
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
49 # Multi-inputs to single input.
50 my $last = pop @choices;
51 my $choice_string = join(', ', @choices) . ' or ' . $last;
52 my $operation = $selection_type . ' selection from';
53
54 return $selection . " (" . $selection_type . ")",
55 structured_answer => {
56 input => [html_enc($choice_string)],
57 operation => $operation,
58 result => html_enc($selection),
59 };
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
60 };
61
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
62 # The query must look like
29a78b7 @kablamo Improved comments in DDG::Goodie::ABC. Added a complimentary test.
kablamo authored
63 # '<choice> or <choice> or <choice>'
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
64 #
65 # Note this method also prevents choices from being > 1 word long as this
29a78b7 @kablamo Improved comments in DDG::Goodie::ABC. Added a complimentary test.
kablamo authored
66 # generates false positives for queries such as
67 # 'choose from a selection of products like venison, turkey, quail, or fish'
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
68 #
29a78b7 @kablamo Improved comments in DDG::Goodie::ABC. Added a complimentary test.
kablamo authored
69 # Returns 0 if the query looks good
70 # Returns 1 if the query looks malformed
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
71 sub query_is_malformed {
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
72 my @words = @_;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
73
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
74 return 1 if none { lc $_ eq 'or' } @words; # ignore queries like 'i choose'
75 return 1 if @words <= 1; # ignore queries like 'choose or'
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
76
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
77 # Ensure every other element of @$words is 'or'
78 foreach my $i (1..$#words) {
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
79 next if $i % 2 == 0; # skip even indices
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
80 return 1 if lc $words[$i] ne 'or';
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
81 }
82
83 return 0;
84 }
85
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
86 1;
Something went wrong with that request. Please try again.