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

57 lines
1.6 KiB
Perl
Raw Normal View History

2013-07-18 23:11:08 -07:00
package DDG::Goodie::POTUS;
# ABSTRACT: Returns requested President of the United States
use strict;
2013-07-18 23:11:08 -07:00
use DDG::Goodie;
2013-08-13 12:03:07 -07:00
use Lingua::EN::Numbers::Ordinate qw(ordsuf ordinate);
use Lingua::EN::Words2Nums;
2015-05-29 15:18:03 -07:00
use YAML::XS 'LoadFile';
2013-07-18 23:11:08 -07:00
triggers startend => 'potus';
triggers any => 'president of the united states', 'president of the us';
2014-10-07 01:45:04 -07:00
zci answer_type => 'potus';
zci is_cached => 1;
name 'POTUS';
description 'returns the President of the United States';
category 'reference';
topics 'trivia';
primary_example_queries 'potus 16';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/POTUS.pm';
attribution github => ['numbertheory', 'John-Peter Etcheber'],
twitter => ['jpscribbles', 'John-Peter Etcheber'];
2015-05-29 15:18:03 -07:00
my @presidents = @{LoadFile(share('presidents.yml'))};
2014-10-07 01:45:04 -07:00
my $prez_count = scalar @presidents;
2014-10-28 11:36:15 -07:00
handle remainder => sub {
2014-10-07 01:45:04 -07:00
my $rem = shift;
$rem =~ s/
|who\s+(is|was)\s+the\s+
|^POTUS\s+
|\s+(POTUS|president\s+of\s+the\s+united\s+states)$
|^(POTUS|president\s+of\s+the\s+united\s+states)\s+
//gix;
2014-10-07 01:45:04 -07:00
my $num = ($rem =~ /^\d+$/) ? $rem : words2nums($rem) || $prez_count;
my $index = $num - 1;
return if $index < 0 or $index > $#presidents;
2013-07-18 23:11:08 -07:00
2014-10-07 01:45:04 -07:00
my $fact = ($num == $prez_count ? 'is' : 'was') . ' the';
2013-08-13 12:03:07 -07:00
2014-10-07 01:45:04 -07:00
my $POTUS = 'President of the United States';
my $the_guy = $presidents[$index];
my $which = ordinate($num);
2014-10-07 01:45:04 -07:00
return "$the_guy $fact $which $POTUS.",
structured_answer => {
input => [$which],
operation => $POTUS,
result => $the_guy,
};
};
2013-07-18 23:11:08 -07:00
1;