handle plural units, general cleanup

master
elohmrow 2014-02-08 02:12:43 +00:00
parent 6bfad61a04
commit 95cf8f5863
2 changed files with 70 additions and 36 deletions

View File

@ -3,61 +3,93 @@ package DDG::Goodie::Conversions;
use DDG::Goodie;
use Scalar::Util qw(looks_like_number);
use Math::Round;
# metric ton is base unit
# known SI units and aliases
# known SI units and aliases / plurals
my %units = (
# metric ton:
'metric ton' => '1',
'tonne' => '1',
't' => '1',
'mt' => '1',
'te' => '1',
'metric ton' => '1',
'tonne' => '1',
't' => '1',
'mt' => '1',
'te' => '1',
'metric tons' => '1',
'tonnes' => '1',
'ts' => '1',
'mts' => '1',
'tes' => '1',
# kilogram:
'kilogram' => '1000',
'kg' => '1000',
'kilo' => '1000',
'kilogramme' => '1000',
'kilogram' => '1000',
'kg' => '1000',
'kilo' => '1000',
'kilogramme' => '1000',
'kilograms' => '1000',
'kgs' => '1000',
'kilos' => '1000',
'kilogrammes' => '1000',
# gram:
'gram' => '1000000',
'g' => '1000000',
'gm' => '1000000',
'gramme' => '1000000',
'gram' => '1000000',
'g' => '1000000',
'gm' => '1000000',
'gramme' => '1000000',
'grams' => '1000000',
'gs' => '1000000',
'gms' => '1000000',
'grammes' => '1000000',
# milligram:
'milligram' => '1000000000',
'mg' => '1000000000',
'milligram' => '1000000000',
'mg' => '1000000000',
'milligrams' => '1000000000',
'mgs' => '1000000000',
# microgram:
'microgram' => '1000000000',
'mcg' => '1000000000',
'microgram' => '1000000000',
'mcg' => '1000000000',
'micrograms' => '1000000000',
'mcgs' => '1000000000',
# long ton:
'long ton' => '0.984207',
'weight ton' => '0.984207',
'imperial ton' => '0.984207',
'long ton' => '0.984207',
'weight ton' => '0.984207',
'imperial ton' => '0.984207',
'long tons' => '0.984207',
'weight tons' => '0.984207',
'imperial tons' => '0.984207',
# short ton:
'short ton' => '1.10231',
'ton' => '1.10231',
'short ton' => '1.10231',
'ton' => '1.10231',
'short tons' => '1.10231',
'tons' => '1.10231',
# stone:
'stone' => '157.473',
'st' => '157.473',
'stone' => '157.473',
'st' => '157.473',
'stones' => '157.473',
'sts' => '157.473',
# pound:
'pound' => '2204.62',
'lb' => '2204.62',
'lbm' => '2204.62',
'pound mass' => '2204.62',
'pound' => '2204.62',
'lb' => '2204.62',
'lbm' => '2204.62',
'pound mass' => '2204.62',
'pounds' => '2204.62',
'lbs' => '2204.62',
'lbms' => '2204.62',
'pounds mass' => '2204.62',
# ounce:
'ounce' => '35274',
'oz' => '35274',
'ounces' => '35274',
'ozs' => '35274',
);
# match longest possible key (some keys are sub-keys of other keys):
my $keys = join '|', reverse sort { length($a) <=> length($b) } keys %units;
# build triggers based on available conversion units:
triggers startend => keys %units;
@ -67,7 +99,7 @@ description 'convert between various weights and measures';
category 'calculations';
topics 'computing', 'math';
primary_example_queries 'convert 5 oz to grams';
secondary_example_queries '5 ounces to g';
secondary_example_queries '5 ounces to g', 'metric ton stone';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Conversions.pm';
attribution github => ['https://github.com/elohmrow', '/bda'],
email => ['bradley@pvnp.us'];
@ -75,9 +107,7 @@ attribution github => ['https://github.com/elohmrow', '/bda'],
zci answer_type => 'conversions';
handle query => sub {
# match longest possible key (some keys are sub-keys of other keys):
my $keys = join '|', reverse sort { length($a) <=> length($b) } keys %units;
my @matches = ($_ =~ /\b($keys)s?\b/gi);
my @matches = ($_ =~ /\b($keys)\b/gi);
return unless scalar @matches == 2; # conversion requires two triggers

View File

@ -21,11 +21,15 @@ ddg_goodie_test(
'convert 0.111 stone to pound' => test_zci('0.111 stone is 1.554 pound',),
'mcg to mcg' => test_zci('1 mcg is 1.000 mcg',),
'3 kilogramme to pound' => test_zci('3 kilogramme is 6.614 pound',),
'1.3 tonnes to ton' => test_zci('1.3 tonne is 1.433 ton',),
'1.3 tonnes to ton' => test_zci('1.3 tonnes is 1.433 ton',),
'stone pound' => test_zci('1 stone is 14.000 pound',),
'gram pound convert' => test_zci('1 gram is 0.002 pound',),
"convert 1 ton to long ton" => test_zci('1 ton is 0.893 long ton',),
'puff toke to kludge' => undef,
'2 tons to kg' => test_zci('2 tons is 1814.372 kg',),
'1 ton to kilos' => test_zci('1 ton is 907.186 kilos',),
'3.9 oz g' => test_zci('3.9 oz is 110.563 g',),
'convert -9 g to ozs' => undef,
);
done_testing;