Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Conversions: Add expanded triggers. (#4328)
Browse files Browse the repository at this point in the history
* Add expanded triggers.

* Update triggering / tests.

* Remove convert as a trigger.
  • Loading branch information
pjhampton authored and moollaza committed Jun 29, 2017
1 parent 65f69da commit 0fd68b0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
33 changes: 29 additions & 4 deletions lib/DDG/Goodie/Conversions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ foreach my $trigger (@natlang_array) {
delete $natlang_hash{'misc'};

my @natural_language_triggers;
my @generics = qw/calculator converter conversion conversions/;
my @expanded_triggers;
my @generics = (
"calculator",
"calc",
"converter",
"unit converter",
"unit conversion",
"conversion",
"conversions" ,
"convertisseur",
"convertidor",
);

for my $trig (@triggers) {
push @expanded_triggers, map { "$trig $_" } @generics;
}

# appends the above generics to the /values/ in the yml file
# unit -> unit converter, unit calculator, ...
Expand All @@ -69,11 +84,16 @@ for my $key (keys %natlang_hash) {
## Declares the triggering scheme
##

triggers any => ( @general_triggers, @natural_language_triggers, @triggers );
triggers any => (
@general_triggers, # eg. conversion calculator, conversions
@expanded_triggers, # eg. cm calculator, grams converter
@natural_language_triggers, # eg. mass converter, volume calc
@triggers # eg. grams, mils, ergs
);

# match longest possible key (some keys are sub-keys of other keys):
my $keys = join '|', map { quotemeta $_ } reverse sort { length($a) <=> length($b) } @units;
my $question_prefix = qr/(?<prefix>conver(?:t|sion)|what (?:is|are|does)|how (?:much|many|long) (?:is|are)?|(?:number of)|(?:how to convert)|(?:convert from))?/i;
my $question_prefix = qr/(?<prefix>conver(?:t|sion)|calculate|what (?:is|are|does)|how (?:much|many|long) (?:is|are)?|(?:number of)|(?:how to convert)|(?:convert from))?/i;

# guards and matches regex
my $factor_re = join('|', ('a', 'an', number_style_regex()));
Expand Down Expand Up @@ -151,6 +171,11 @@ handle query => sub {
};
}

my $generic_postfixes = join "|", @generics;
my $perserved_query = $_;
$_ =~ s/$generic_postfixes//;
$_ =~ s/\s+$//;

# hack around issues with feet and inches for now
$_ =~ s/"/inch/;
$_ =~ s/'/foot/;
Expand Down Expand Up @@ -178,7 +203,7 @@ handle query => sub {

# ignore conversion when both units have a number
return if ($left_num && $right_num);
return if (length $left_unit <= 3 && !grep(/^$left_unit$/, @safe_abbrevs)) && !($left_num || $right_unit);
return if (length $left_unit <= 3 && !grep(/^$perserved_query$/, @expanded_triggers) && !grep(/^$left_unit$/, @safe_abbrevs)) && !($left_num || $right_unit);

# Compare factors of both units to ensure proper order when ambiguous
# also, check the <connecting_word> of regex for possible user intentions
Expand Down
2 changes: 1 addition & 1 deletion share/goodie/conversions/langTriggers.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
misc:
- convert
- converter
- conversion
- conversion calculator
- metric conversion calculator
Expand Down
63 changes: 49 additions & 14 deletions t/Conversions.t
Original file line number Diff line number Diff line change
Expand Up @@ -2179,21 +2179,36 @@ ddg_goodie_test(
physical_quantity => 'digital'
})
),

'centigrade to fahrenheit conversion table' => test_zci(
'miles calculator' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'celsius',
to_unit => 'fahrenheit',
physical_quantity => 'temperature'
from_unit => 'mile',
to_unit => '',
physical_quantity => 'length'
})
),
'centigrade to fahrenheit conversion table' => test_zci(
'km converter' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'celsius',
to_unit => 'fahrenheit',
physical_quantity => 'temperature'
from_unit => 'kilometer',
to_unit => '',
physical_quantity => 'length'
})
),
'erg calc' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'ergfixed',
to_unit => '',
physical_quantity => 'energy'
})
),
'grams convertisseur' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'gram',
to_unit => '',
physical_quantity => 'mass'
})
),
'fluid ounces to milliliters' => test_zci(
Expand All @@ -2204,6 +2219,31 @@ ddg_goodie_test(
physical_quantity => 'volume'
})
),
'cm unit converter' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'cm',
to_unit => '',
physical_quantity => 'length'
})
),
'mm unit conversion' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'millimeter',
to_unit => '',
physical_quantity => 'length'
})
),
'unit converter kj' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'kilojoule',
to_unit => '',
physical_quantity => 'energy'
})
),


# abbreviations allowed though
'cm' => test_zci(
Expand Down Expand Up @@ -2396,11 +2436,6 @@ ddg_goodie_test(
physical_quantity => 'length'
})
),
'convert' => test_zci(
'', structured_answer => make_answer_with_base({
physical_quantity => 'length'
})
),

# Intentionally untriggered
'BTU to KwH' => undef,
Expand Down

0 comments on commit 0fd68b0

Please sign in to comment.