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
master
Rob Emery 2017-03-07 04:39:09 +00:00 committed by Zaahir Moolla
parent ed1b1f3916
commit 0cffa6161e
2 changed files with 21 additions and 10 deletions

View File

@ -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+(?<iswas>is|was)\s*(?:the\s*)?(?<num>.*))
|(?<num>.*)
$/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;

View File

@ -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
);