zeroclickinfo-goodies/lib/DDG/Goodie/PaleoIngredientCheck.pm

53 lines
1.4 KiB
Perl
Raw Normal View History

2015-08-28 13:42:46 -07:00
package DDG::Goodie::PaleoIngredientCheck;
# ABSTRACT: Indicates if a given food item is known to be safe or unsafe on the paleo diet.
use DDG::Goodie;
2015-09-29 10:12:56 -07:00
triggers startend => share('triggers.txt')->slurp;
2015-08-28 13:42:46 -07:00
zci answer_type => "paleo_ingredient_check";
zci is_cached => 1;
2015-10-30 15:00:25 -07:00
my @safe_array = share('safe.txt')->slurp;
my %safe_keywords = map { chomp; $_ => 1 } @safe_array;
my @unsafe_array = share('unsafe.txt')->slurp;
my %unsafe_keywords = map { chomp; $_ => 1 } @unsafe_array;
2015-09-29 10:12:56 -07:00
2015-10-30 15:00:25 -07:00
# p(%safe_keywords);
# warn($safe_keywords{"apple"});
2015-08-28 13:42:46 -07:00
2015-10-30 15:00:25 -07:00
handle remainder => sub {
return unless $_;
2015-09-29 10:12:56 -07:00
my $item = lc($_);
2015-08-28 13:42:46 -07:00
2015-09-29 10:12:56 -07:00
# Remove any preceding "is" or "are" text from the query.
$item =~ s/^(is|are)[\W]+//;
2015-08-28 13:42:46 -07:00
2015-09-29 10:12:56 -07:00
my $is_plural = substr($item, -1) eq "s";
2015-10-30 15:00:25 -07:00
my $itemAlt = $is_plural ? substr($item, 0, -1) : $item."s"; # pluralized or unpluralized form
my $result;
# warn "ITEM IS: $item";
# warn "ITEM ALT IS: $itemAlt";
2015-08-28 13:42:46 -07:00
2015-10-30 15:00:25 -07:00
if (exists $safe_keywords{$item} || exists $safe_keywords{$itemAlt}) {
$result = "Yes";
} elsif (exists $unsafe_keywords{$item} || exists $unsafe_keywords{$itemAlt}) {
$result = "No";
2015-09-29 10:12:56 -07:00
}
2015-10-30 10:20:35 -07:00
2015-10-30 15:00:25 -07:00
return unless $result; # ensure we have a result
return $result, structured_answer => {
data => {
title => $result,
subtitle => "Paleo Friendly: ".html_enc($item)
},
templates => {
group => 'text'
}
2015-10-30 15:00:25 -07:00
};
2015-08-28 13:42:46 -07:00
};
2015-11-11 10:14:02 -08:00
1;