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

Commit

Permalink
Conversions: Enable single unit triggers (#4316)
Browse files Browse the repository at this point in the history
* Add failing tests.

Enable single unit queries.

Add units for triggering.

* Update indentation.
  • Loading branch information
pjhampton authored and moollaza committed Jun 21, 2017
1 parent 7de983b commit e3b649c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/DDG/Goodie/Conversions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ handle query => sub {
my @matches = ($left_unit, $right_unit);

# ignore conversion when both units have a number
return if ($left_num && $right_num) || $left_unit && !($left_num || $right_unit);
return if ($left_num && $right_num);
return if length $left_unit <= 3 && !($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 Expand Up @@ -198,7 +199,7 @@ handle query => sub {

# handle case when there is no "to" unit
# e.g. "36 meters"
if ($left_unit && $left_num && !($right_unit || $right_num)) {
if ($left_unit && !($right_unit || $right_num)) {
$factor = $left_num;
}
# if the query is in the format <unit> in <num> <unit> we need to flip
Expand Down
57 changes: 53 additions & 4 deletions t/Conversions.t
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,48 @@ ddg_goodie_test(
})
),

# single unit queries with no number
'grams' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'gram',
to_unit => '',
physical_quantity => 'mass'
})
),
'meters' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'meter',
to_unit => '',
physical_quantity => 'length'
})
),
'feet' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'foot',
to_unit => '',
physical_quantity => 'length'
})
),
'miles ph' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'mi/h',
to_unit => '',
physical_quantity => 'speed'
})
),
'gigabytes' => test_zci(
'', structured_answer => make_answer({
raw_input => '1',
from_unit => 'GB',
to_unit => '',
physical_quantity => 'digital'
})
),

# natural language queries
'unit converter' => test_zci(
'', structured_answer => make_answer_with_base({
Expand Down Expand Up @@ -2222,11 +2264,18 @@ ddg_goodie_test(
'use a ton of stones' => undef,
'shoot onself in the foot' => undef,
'foot in both camps' => undef,
'Seconds' => undef,
'feet' => undef,
'minutes' => undef,
'99999999999000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 miles in mm' => undef,
'1E300 miles in metres' => undef,
'5 pas.i to atm' => undef,
'grams of ham' => undef,
'car was going 100 mph' => undef,
'length' => undef,
'speed' => undef,

# don't trigger on units where the string is <= 3
'cms' => undef,
'mph' => undef,
'ft' => undef,
'gs' => undef,
'ms' => undef,
);
done_testing;

0 comments on commit e3b649c

Please sign in to comment.