ABC: updated with structured answer.

Includes a possible technique to encode multi-inputs for
auto-templating.
master
Matt Miller 2014-10-06 08:18:51 +02:00
parent 5457d4ec62
commit c6afdf5ad3
2 changed files with 88 additions and 18 deletions

View File

@ -6,8 +6,8 @@ use List::AllUtils qw/none/;
triggers startend => qw/choose pick select/;
zci answer_type => "rand";
zci is_cached => 0;
zci answer_type => "choice";
zci is_cached => 0;
primary_example_queries 'choose yes or no';
secondary_example_queries 'choose heads or tails', 'pick this or that or none';
@ -24,8 +24,6 @@ attribution twitter => 'crazedpsyc',
web => ["http://kablamo.org", "Eric Johnson"] ;
handle remainder => sub {
# Ensure rand is seeded for each process
srand();
my $query = $_;
@ -37,15 +35,31 @@ handle remainder => sub {
# rm every 'or' from the list
my @choices = grep { lc $_ ne 'or' } @words;
my $selection_type = 'random';
my $selection;
# Easter egg. For queries like:
# 'choose duckduckgo or google or bing or something'
if (my @duck = grep { / \A (?: duck (?: duckgo )? | ddg ) \z /ix } @choices) {
return $duck[0]." (not random)", answer_type => 'egg';
$selection_type = 'non-random';
$selection = $duck[0];
} else {
# Ensure rand is seeded for each process
srand();
# Choose randomly
$selection = $choices[int rand scalar @choices];
}
# Choose randomly
my $index = int rand scalar @choices;
return $choices[$index]." (random)";
# Multi-inputs to single input.
my $last = pop @choices;
my $choice_string = join(', ', @choices) . ' or ' . $last;
my $operation = $selection_type . ' selection from';
return $selection . " (" . $selection_type . ")",
structured_answer => {
input => [html_enc($choice_string)],
operation => $operation,
result => html_enc($selection),
};
};
# The query must look like

76
t/ABC.t
View File

@ -5,11 +5,11 @@ use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'rand';
zci answer_type => 'choice';
zci is_cached => 0;
ddg_goodie_test(
[qw( DDG::Goodie::ABC )],
[qw( DDG::Goodie::ABC )],
'choose' => undef,
'i choose' => undef,
'choose or' => undef,
@ -17,14 +17,70 @@ ddg_goodie_test(
'choose his or her house' => undef,
'choose his or or her house' => undef,
'choose from products like turkey or venison' => undef,
'choose pick or axe' => test_zci(qr/(pick|axe) \(random\)/),
'choose yes or no' => test_zci(qr/(yes|no) \(random\)/),
'choose this or that or none' => test_zci(qr/(this|that|none) \(random\)/),
'pick this or that or none' => test_zci(qr/(this|that|none) \(random\)/),
'select heads or tails' => test_zci(qr/(heads|tails) \(random\)/),
'choose heads or tails' => test_zci(qr/(heads|tails) \(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'),
'choose pick or axe' => test_zci(
qr/(pick|axe) \(random\)/,
structured_answer => {
input => ['pick or axe'],
operation => 'random selection from',
result => qr/^(?:pick|axe)$/,
}
),
'choose yes or no' => test_zci(
qr/(yes|no) \(random\)/,
structured_answer => {
input => ['yes or no'],
operation => 'random selection from',
result => qr/^(?:yes|no)$/,
}
),
'choose this or that or none' => test_zci(
qr/(this|that|none) \(random\)/,
structured_answer => {
input => ['this, that or none'],
operation => 'random selection from',
result => qr/^(?:this|that|none)$/,
}
),
'pick this or that or none' => test_zci(
qr/(this|that|none) \(random\)/,
structured_answer => {
input => ['this, that or none'],
operation => 'random selection from',
result => qr/^(?:this|that|none)$/,
}
),
'select heads or tails' => test_zci(
qr/(heads|tails) \(random\)/,
structured_answer => {
input => ['heads or tails'],
operation => 'random selection from',
result => qr/^(?:heads|tails)$/,
}
),
'choose heads or tails' => test_zci(
qr/(heads|tails) \(random\)/,
structured_answer => {
input => ['heads or tails'],
operation => 'random selection from',
result => qr/^(?:heads|tails)$/,
}
),
'choose duckduckgo or google or bing or something' => test_zci(
'duckduckgo (non-random)',
structured_answer => {
input => ['duckduckgo, google, bing or something'],
operation => 'non-random selection from',
result => 'duckduckgo',
}
),
'choose Google OR DuckDuckGo OR Bing OR SOMETHING' => test_zci(
'DuckDuckGo (non-random)',
structured_answer => {
input => ['Google, DuckDuckGo, Bing or SOMETHING'],
operation => 'non-random selection from',
result => 'DuckDuckGo',
}
),
);
done_testing;