diff --git a/lib/DDG/Goodie/POTUS.pm b/lib/DDG/Goodie/POTUS.pm index b7e8cf89e..b101a428e 100755 --- a/lib/DDG/Goodie/POTUS.pm +++ b/lib/DDG/Goodie/POTUS.pm @@ -16,18 +16,24 @@ zci is_cached => 1; my @presidents = @{LoadFile(share('presidents.yml'))}; my $prez_count = scalar @presidents; +my $potus_or_president = qr/(potus|president of the (us|united states))/i; -handle remainder => sub { - my $rem = shift; - return if $rem =~ /vice/i; - $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; +handle query_lc => sub { + + # workaround for president-elect of the united states being classed as a trigger + s/(^$potus_or_president\s)|(\s$potus_or_president\s?[\?]?$)//i; + + /^ + (who\s+(?is|was)\s*(?:the\s*)?(?.*)) + |(?.*) + $/gix or return; + + my $num; + $num = $prez_count if $+{num} eq ""; + $num = $prez_count -1 if $+{num} eq "" and $+{iswas} eq "was"; + $num = words2nums($+{num}) if words2nums($+{num}); + return unless $num; - my $num = ($rem =~ /^\d+$/) ? $rem : words2nums($rem) || $prez_count; my $index = $num - 1; return if $index < 0 or $index > $#presidents; diff --git a/t/POTUS.t b/t/POTUS.t index 55b889473..aa35c83a4 100755 --- a/t/POTUS.t +++ b/t/POTUS.t @@ -26,6 +26,8 @@ sub build_test ddg_goodie_test( [qw( DDG::Goodie::POTUS)], + 'who is the president of the united states' => build_test('Donald J. Trump', 'is',"45th"), + 'who was the president of the united states' => build_test('Barack Obama', 'was', '44th'), 'who is president of the united states' => build_test('Donald J. Trump', 'is',"45th"), 'who is the fourth president of the united states' => build_test('James Madison', 'was', '4th'), 'who is the nineteenth president of the united states' => build_test('Rutherford B. Hayes', 'was','19th'), @@ -38,6 +40,9 @@ ddg_goodie_test( 'potus 16' => build_test('Abraham Lincoln', 'was', '16th'), 'who is the vice president of the united states?' => undef, 'vice president of the united states' => undef, + 'who is the worst president of the united states' => undef, + 'who is the president elect of the united states' => undef, + 'who is the president-elect of the us' => undef, 'VPOTUS' => undef );