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

Commit

Permalink
Add plural aliases (#4285)
Browse files Browse the repository at this point in the history
Add speed generic trigs.

Update the natural language triggering scheme.

Update yml file. Remove dep

Reduce yaml file.

mint/mool refactor requests.

Update stuff.
  • Loading branch information
pjhampton authored and moollaza committed Jun 20, 2017
1 parent f4effd1 commit 5daa945
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 87 deletions.
74 changes: 57 additions & 17 deletions lib/DDG/Goodie/Conversions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ zci is_cached => 1;

use bignum;

##
## Handles the normal /with unit/ triggering
##

my @types = LoadFile(share('triggers.yml'));
my %natlang_hash = %{ LoadFile(share('langTriggers.yml')) };

my @units = ();
foreach my $type (@types) {
Expand All @@ -27,12 +32,33 @@ foreach my $type (@types) {

# build triggers based on available conversion units:
my @triggers = map { lc $_ } @units;
triggers any => @triggers;

my @lang_triggers = share('langTriggers.txt')->slurp(chomp => 1);
##
## Handles the natural language triggering
##

my @natural_language_triggers;
my @generics = qw/calculator converter conversion conversions/;

# appends the above generics to the /values/ in the yml file
# unit -> unit converter, unit calculator, ...
for my $array (values %natlang_hash) {
for my $val (@$array) {
push @natural_language_triggers, map { "$val $_" } @generics;
}
}

# appends the above generics to the /keys/ in the yml file
# length -> length converter, length calculator, ...
for my $key (keys %natlang_hash) {
push @natural_language_triggers, map { "$key $_" } @generics;
}

triggers any => @lang_triggers;
my %lang_triggers = map { $_ => 1 } @lang_triggers;
##
## Declares the triggering scheme
##

triggers any => (@triggers, @natural_language_triggers);

# match longest possible key (some keys are sub-keys of other keys):
my $keys = join '|', map { quotemeta $_ } reverse sort { length($a) <=> length($b) } @units;
Expand Down Expand Up @@ -67,13 +93,38 @@ sub magnitude_order {
}
my $maximum_input = 10**100;

# checks to see if input is natural language trigger
sub is_natural_language_trigger {
my $input = shift;
return any { $_ eq $input } @natural_language_triggers;
}

# checks the base of the query
# eg. velocity converter --> speed
sub get_base_information {
my $input = shift;
$input =~ s/calculator|converter|conversion\s?|\s//gi;

foreach my $key (keys %natlang_hash) {
return $key if $key eq $input;
next unless exists $natlang_hash{$key};

my @hash_kv = @{$natlang_hash{$key}};
foreach my $value (@hash_kv) {
return $key if $input eq $value;
}
}
# if not assigned such as 'unit calculator' we'll default to length
return 'length';
}

handle query => sub {

# for natural language queries, settle with default template / data
if (exists($lang_triggers{$_}) && $_=~ m/(angle|area|(?:digital storage)|duration|energy|force|mass|power|pressure|temperature|volume)/) {
if (is_natural_language_trigger($_)) {
return '', structured_answer => {
data => {
physical_quantity => $1
physical_quantity => get_base_information($_)
},
templates => {
group => 'base',
Expand All @@ -83,17 +134,6 @@ handle query => sub {
}
};
}
elsif(exists($lang_triggers{$_})) {
return '', structured_answer => {
data => {},
templates => {
group => 'base',
options => {
content => 'DDH.conversions.content'
}
}
};
}

# hack around issues with feet and inches for now
$_ =~ s/"/inch/;
Expand Down
6 changes: 3 additions & 3 deletions share/goodie/conversions/conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ DDH.conversions = DDH.conversions || {};
name: "Speed",
units: [
{ symbol: 'mi/h', name: 'Miles per hour' },
{ symbol: 'ft/s', name: 'Foot per second' },
{ symbol: 'm/s', name: 'Metre per second' },
{ symbol: 'km/h', name: 'Kilometre per hour'},
{ symbol: 'ft/s', name: 'Feet per second' },
{ symbol: 'm/s', name: 'Metres per second' },
{ symbol: 'km/h', name: 'Kilometres per hour'},
{ symbol: 'knot', name: 'Knot'},
],
defaults: ['mi/h', 'km/h']
Expand Down
46 changes: 0 additions & 46 deletions share/goodie/conversions/langTriggers.txt

This file was deleted.

21 changes: 21 additions & 0 deletions share/goodie/conversions/langTriggers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
length:
- online
- unit
- unit
- units
mass:
angle:
area:
digital:
- digital storage
duration:
energy:
force:
power:
pressure:
temperature:
- temp
speed:
- velocity
volume:
- vol
56 changes: 35 additions & 21 deletions t/Conversions.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Test::More;
use Test::Deep;
use DDG::Test::Goodie;
use utf8;

zci answer_type => 'conversions';
zci is_cached => 1;

Expand All @@ -27,18 +28,6 @@ sub make_answer(%){
};
}

sub make_answer_lang {
return {
data => {},
templates => {
group => 'base',
options => {
content => 'DDH.conversions.content'
}
}
};
}

sub make_answer_with_base(%){
my ($input) = @_;

Expand Down Expand Up @@ -1535,15 +1524,27 @@ ddg_goodie_test(
# NATURAL LANGUAGE QUERIES
'unit converter' => test_zci(
'',
structured_answer => make_answer_lang()
structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
'unit conversion' => test_zci(
'',
structured_answer => make_answer_lang()
structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
'online converter' => test_zci(
'',
structured_answer => make_answer_lang()
structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
'velocity converter' => test_zci(
'',
structured_answer => make_answer_with_base({
physical_quantity => 'speed',
}),
),

# INTENTIONALLY UNTRIGGERED
Expand Down Expand Up @@ -2147,16 +2148,19 @@ ddg_goodie_test(

# natural language queries
'unit converter' => test_zci(
'',
structured_answer => make_answer_lang()
'', structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
'unit conversion' => test_zci(
'',
structured_answer => make_answer_lang()
'', structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
'online converter' => test_zci(
'',
structured_answer => make_answer_lang()
'', structured_answer => make_answer_with_base({
physical_quantity => 'length',
}),
),
# natural language queries containing triggers
'volume converter' => test_zci(
Expand Down Expand Up @@ -2184,6 +2188,16 @@ ddg_goodie_test(
physical_quantity => 'pressure'
})
),
'speed conversion' => test_zci(
'', structured_answer => make_answer_with_base({
physical_quantity => 'speed'
})
),
'speed calculator' => test_zci(
'', structured_answer => make_answer_with_base({
physical_quantity => 'speed'
})
),
'temperature conversion' => test_zci(
'', structured_answer => make_answer_with_base({
physical_quantity => 'temperature'
Expand Down

0 comments on commit 5daa945

Please sign in to comment.