Support translation to+from Pig Latin and casing

Swap to using the `Lingua::PigLatin::Bidirectional` package which
provides much better support for case-preservation as well as
translation to and from Pig Latin.
master
Ben Moon 2015-12-29 11:35:48 +00:00
parent d3108e2de2
commit 8f2a36ccf5
2 changed files with 19 additions and 10 deletions

View File

@ -3,24 +3,28 @@ package DDG::Goodie::PigLatin;
use strict;
use DDG::Goodie;
use Lingua::PigLatin 'piglatin';
use Lingua::PigLatin::Bidirectional;
triggers startend => 'pig latin', 'piglatin';
triggers any => 'pig latin', 'piglatin';
zci answer_type => "translation";
zci is_cached => 1;
handle remainder => sub {
handle query_raw => sub {
my $query = shift;
$query =~ s/\s*(?<action>to|from|in)\s*pig ?latin\s*$//i;
$query =~ s/^\s*(?<action>to|from|in)?+\s*pig ?latin\s*//i unless $+{'action'};
return unless $query;
my $action = lc ($+{'action'} // 'to');
$action = 'to' if $action eq 'in';
my $result = $action eq 'to' ? to_piglatin($query) : from_piglatin($query);
my $result = piglatin($query);
return $result, structured_answer => {
id => 'pig_latin',
name => 'Answer',
data => {
title => "$result",
subtitle => "Translate to Pig Latin: $query",
subtitle => "Translate $action Pig Latin: $query",
},
templates => {
group => 'text',

View File

@ -9,13 +9,13 @@ zci answer_type => 'translation';
zci is_cached => 1;
sub build_structured_answer {
my ($expected_result, $expected_formatted_input) = @_;
my ($expected_result, $expected_action, $expected_formatted_input) = @_;
return $expected_result, structured_answer => {
id => 'pig_latin',
name => 'Answer',
data => {
title => "$expected_result",
subtitle => "Translate to Pig Latin: $expected_formatted_input",
subtitle => "Translate $expected_action Pig Latin: $expected_formatted_input",
},
templates => {
group => 'text',
@ -28,9 +28,14 @@ sub build_test { test_zci(build_structured_answer(@_)) }
ddg_goodie_test(
[qw( DDG::Goodie::PigLatin )],
'pig latin will this work?' => build_test('illway isthay orkway?', 'will this work?'),
'piglatin i love duckduckgo' => build_test('iway ovelay uckduckgoday', 'i love duckduckgo'),
'pig latin i love duckduckgo' => build_test('iway ovelay uckduckgoday', 'i love duckduckgo'),
'pig latin will this work?' => build_test('illway isthay orkway?', 'to', 'will this work?'),
'piglatin i love duckduckgo' => build_test('iway ovelay uckduckgoday', 'to', 'i love duckduckgo'),
'pig latin i love duckduckgo' => build_test('iway ovelay uckduckgoday', 'to', 'i love duckduckgo'),
'What is this? in piglatin' => build_test('Atwhay isway isthay?', 'to', 'What is this?'),
'in piglatin foo' => build_test('oofay', 'to', 'foo'),
'from pigLatiN oofay' => build_test('foo', 'from', 'oofay'),
'piglatin in piglatin' => build_test('iglatinpay', 'to', 'piglatin'),
'iglatinpay from piglatin' => build_test('piglatin', 'from', 'iglatinpay'),
'pig latin' => undef,
'piglatin' => undef,
);