ColorCodes: Triggers on any query ending in "color <name>" (#4220)

* ColorCodes: Triggers on any query ending in "color <name>"

- Added logic to prevent triggering when excess words are involved in
  the sentence that are not expected

Fixes #3294

* - Fixed regex for filler words to be simpler
- Fixed filler count return statement by removing the double negative

* Simplified regex to catch filler words

* Fixed whitespace changes

* Added more tests for when the IA should not trigger. Existing tests already sufficiently cover when the IA should trigger.
master
Kyle Daruwalla 2017-07-12 06:19:17 -05:00 committed by Zaahir Moolla
parent af0a70222d
commit 215857e8dd
2 changed files with 14 additions and 2 deletions

View File

@ -41,7 +41,7 @@ triggers query_raw => qr/^
(.*?)\s*(.+?)\brgb(?:\s+code)?| # handles "red rgb code", etc
(.*?)\s*colou?r(?:\s+code)?(?:\s+for)?\s+(.+?)| # handles "rgb color code for red", "red color code for html", etc
(.*?)(rgba)\s*:?\s*\(?\s*(.+?)\s*\)?| # handles "rgba( red )", "rgba:255,0,0", "rgba(255 0 0)", etc
([^\s]*?)\s*($typestr)\s*:?\s*\(?\s*(.+?)\s*\)?| # handles "rgb( red )", "rgb:255,0,0", "rgb(255 0 0)", etc
([^\s]*?)\s*($typestr)\s*:?\s*\(?\s*(.+?)\s*\)?|# handles "rgb( red )", "rgb:255,0,0", "rgb(255 0 0)", etc
\#?([0-9a-f]{6})|\#([0-9a-f]{3}) # handles #00f, #0000ff, etc
)
(?:(?:'?s)?\s+(inverse|negative|opposite))?
@ -65,6 +65,7 @@ sub percentify {
handle matches => sub {
my $color;
my $filler_count;
my $inverse;
my $type = 'rgb8'; # Default type, can be overridden below.
@ -72,6 +73,7 @@ handle matches => sub {
s/\sto\s(?:$typestr)//;
$filler_count = 0;
foreach my $q (map { lc $_ } grep { defined $_ } @matches) {
# $q now contains the defined normalized matches which can be:
if (exists $types{$q}) {
@ -79,13 +81,19 @@ handle matches => sub {
} elsif ($trigger_invert{$q}) {
$inverse = 1; # - An inversion trigger
} elsif (!$trigger_filler{$q}) { # - A filler word for more natural querying
$color = $q; # - A presumed color
if ($q =~ /(?:^[a-z]+\s)+/) {
$filler_count = $filler_count + 1;
} else {
$color = $q; # - A presumed color
}
}
}
return unless $color; # Need a color to continue!
$color =~ s/\sto\s//;
return if $filler_count;
my $alpha = "1";
$color =~ s/(,\s*|\s+)/,/g; # Spaces to commas for things like "hsl 194 0.53 0.79"
if ($color =~ s/#?([0-9a-f]{3,6})$/$1/) { # Color looks like a hex code, strip the leading #

View File

@ -109,6 +109,10 @@ ddg_goodie_test(
'bluishblack html' => undef,
'HTML email' => undef,
'wield color' => undef,
'whats the symbolism of the color red' => undef,
'red color porsche 911' => undef,
'yoda lightsaber green color' => undef,
'product red color iphone' => undef,
);
done_testing;