From 0cffa6161e871bb33477ce94e1c6e794eb5e15b5 Mon Sep 17 00:00:00 2001 From: Rob Emery Date: Tue, 7 Mar 2017 04:39:09 +0000 Subject: [PATCH] POTUS: Triggering is way too keen (#3938) * POTUS: Adding failing test for overtrigger * POTUS: the previous || $prez_count meant that it defaulted to the current president if it didn't recognise the number; hence why it was showing so many false positives * POTUS: Now with stricter handling on the $num we can trust the validation later on to keep us safe from overtriggering * POTUS: basic queries that really should work! * POTUS: Switching to using a guard regex to match things rather than using replacements * POTUS: Guard regex now protects the query space. and "was" is used to imply the previous president (currently obama) * POTUS: switched to query_lc and implementing both a guard regex and a replacement for the triggers so that the code below behaves as if it is remainder =>, except that this doesn't erroneously replace "president-elect of the us" with "" * POTUS: We don't need $rem --- lib/DDG/Goodie/POTUS.pm | 26 ++++++++++++++++---------- t/POTUS.t | 5 +++++ 2 files changed, 21 insertions(+), 10 deletions(-) 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 );