Skip to content
Newer
Older
100644 77 lines (60 sloc) 2.15 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
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
13 handle remainder => sub {
1e2740a @moollaza Add srand() to other goodies using rand
moollaza authored
14
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
15 my $query = $_;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
16
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
17 # split the query on whitespace and rm whitespace
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
18 my @words = grep { length } split /\s+/, $query;
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
19
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
20 return if query_is_malformed(@words);
21
22 # rm every 'or' from the list
23 my @choices = grep { lc $_ ne 'or' } @words;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
24
ad58556 @MrChrisW Update Case for ABC, Anagram, Ascii, AspectRatio, Base, Base64
MrChrisW authored
25 my $selection_type = 'Random';
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
26 my $selection;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
27 # Easter egg. For queries like:
28 # 'choose duckduckgo or google or bing or something'
60a51e5 Make "DuckDuckGo" in "DuckDuckGo or something" query case insensitive.
GlitchMr authored
29 if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @choices) {
ad58556 @MrChrisW Update Case for ABC, Anagram, Ascii, AspectRatio, Base, Base64
MrChrisW authored
30 $selection_type = 'Non-random';
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
31 $selection = $duck[0];
32 } else {
33 # Ensure rand is seeded for each process
34 srand();
35 # Choose randomly
36 $selection = $choices[int rand scalar @choices];
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
37 }
38
c6afdf5 @mwmiller ABC: updated with structured answer.
mwmiller authored
39 # Multi-inputs to single input.
40 my $last = pop @choices;
41 my $choice_string = join(', ', @choices) . ' or ' . $last;
42 my $operation = $selection_type . ' selection from';
43
44 return $selection . " (" . $selection_type . ")",
45 structured_answer => {
46 input => [html_enc($choice_string)],
47 operation => $operation,
48 result => html_enc($selection),
49 };
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
50 };
51
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
52 # The query must look like
29a78b7 @kablamo Improved comments in DDG::Goodie::ABC. Added a complimentary test.
kablamo authored
53 # '<choice> or <choice> or <choice>'
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
54 #
55 # 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
56 # generates false positives for queries such as
57 # '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
58 #
29a78b7 @kablamo Improved comments in DDG::Goodie::ABC. Added a complimentary test.
kablamo authored
59 # Returns 0 if the query looks good
60 # Returns 1 if the query looks malformed
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
61 sub query_is_malformed {
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
62 my @words = @_;
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
63
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
64 return 1 if none { lc $_ eq 'or' } @words; # ignore queries like 'i choose'
65 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
66
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
67 # Ensure every other element of @$words is 'or'
68 foreach my $i (1..$#words) {
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
69 next if $i % 2 == 0; # skip even indices
2511c9b @kablamo Handle queries like 'choose pick or axe'
kablamo authored
70 return 1 if lc $words[$i] ne 'or';
1fb4f83 @kablamo Improved readability a bit for DDG::Goodie::ABC. Added more tests.
kablamo authored
71 }
72
73 return 0;
74 }
75
871e270 @crazedpsyc start converting goodies for new API
crazedpsyc authored
76 1;
Something went wrong with that request. Please try again.