Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Rewrote ABC.pm to solve the 'choose' problem #158

Closed
wants to merge 2 commits into from

4 participants

@mattlehning

No description provided.

@mattlehning

It is live on matt.duckduckgo.com if you want to test it out.

@hunterlang
Owner

What happens if the arguments are comma-delimited? E.g. choose a, b, or c

@jagtalon
Owner

@mattlehning were you able to implement @hunterlang's comment? :)

@djinn

@jagtalon Since original ABC module also followed the same convention AFAIK. Is "," use case a blocker to merging?

@jagtalon
Owner

Closing for now due to inactivity.

@jagtalon jagtalon closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 9, 2013
  1. @mattlehning
Commits on Feb 14, 2013
  1. @mattlehning

    Added ddg easter egg

    mattlehning authored
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 70 deletions.
  1. +0 −48 lib/DDG/Goodie/ABC.pm
  2. +28 −0 lib/DDG/Goodie/AorB.pm
  3. +0 −22 t/ABC.t
  4. +25 −0 t/AorB.t
View
48 lib/DDG/Goodie/ABC.pm
@@ -1,48 +0,0 @@
-package DDG::Goodie::ABC;
-# ABSTRACT: Randomly pick one of different choices splitted by "or"
-
-use DDG::Goodie;
-
-triggers startend => "choose";
-
-zci answer_type => "rand";
-
-primary_example_queries 'choose yes or no';
-secondary_example_queries 'choose heads or tails', 'choose 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' ;
-
-handle query_parts => sub {
- return unless /or/;
- my @choices;
- my @collected_parts;
- while (my $part = shift) {
- next if $part eq 'choose';
- 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;
- return if scalar(@choices) <= 1;
- my $choice = int(rand(@choices));
-
- if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @choices) {
- return $duck[0]." (not random)", answer_type => 'egg';
- }
-
- return $choices[$choice]." (random)";
- return;
-};
-
-1;
View
28 lib/DDG/Goodie/AorB.pm
@@ -0,0 +1,28 @@
+package DDG::Goodie::AorB;
+
+use DDG::Goodie;
+
+triggers any => "pick", "choose", "or";
+
+zci answer_type => "rand";
+
+handle query_lc => sub {
+ return unless /^(?:pick|choose)?\s*((?:[^\s]+)(?:\s+[^\s]+)*(?:\s+or\s+(?:[^\s]+))+)$/;
+ my @query = split(' ', $1);
+ my @del = grep{ $query[$_] eq 'or' } 0..$#query;
+ my $pick;
+ my $a = 0;
+ for (@del) {
+ splice(@query, $_-$a, 1);
+ $a++;
+ }
+ if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @query) {
+ return $duck[0]." (not random)", answer_type => 'egg';
+ }
+
+ $pick = $query[int rand @query];
+ return $pick." (random)";
+
+};
+
+1;
View
22 t/ABC.t
@@ -1,22 +0,0 @@
-#!/usr/bin/env perl
-
-use strict;
-use warnings;
-use Test::More;
-use DDG::Test::Goodie;
-
-zci answer_type => 'rand';
-zci is_cached => 0;
-
-ddg_goodie_test(
- [qw(
- DDG::Goodie::ABC
- )],
- 'choose yes or no' => test_zci(qr/(yes|no) \(random\)/),
- 'choose this or that or none' => test_zci(qr/(this|that|none) \(random\)/),
- 'choose duckduckgo or google or bing or something' => test_zci("duckduckgo (not random)", answer_type => 'egg'),
- 'choose DuckDuckGo OR Google OR Bing or SOMETHING' => test_zci("DuckDuckGo (not random)", answer_type => 'egg'),
-);
-
-done_testing;
-
View
25 t/AorB.t
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use DDG::Test::Goodie;
+
+zci answer_type => 'rand';
+zci is_cached => 0;
+
+ddg_goodie_test(
+ [qw(
+ DDG::Goodie::AorB
+ )],
+ 'a or b' => test_zci(qr/(a|b) \(random\)/),
+ 'a b or c' => test_zci(qr/(a|b|c) \(random\)/),
+ 'choose a or b' => test_zci(qr/(a|b) \(random\)/),
+ 'choose a b or c' => test_zci(qr/(a|b|c) \(random\)/),
+ 'pick a or b' => test_zci(qr/(a|b) \(random\)/),
+ 'pick a b or c' => test_zci(qr/(a|b|c) \(random\)/),
+ '@#$ or 1i4' => test_zci(qr/(\@\#\$|1i4) \(random\)/),
+ 'duckduckgo or google' => test_zci("duckduckgo (not random)", answer_type => 'egg'),
+);
+
+done_testing;
Something went wrong with that request. Please try again.