Merge pull request #303 from elohmrow/conversions

Adding conversions Goodie
master
Michael Smith 2014-02-08 08:46:30 -07:00
commit bb12adeb1e
4 changed files with 168 additions and 1 deletions

View File

@ -27,6 +27,7 @@ DateTime = 0.74
Lingua::EN::Numericalize = 1.52
Lingua::PigLatin = 0.01
Locale::SubCountry = 1.47
Scalar::Util = 0
; causing problems because not pp: HTML::Barcode::QRCode = 0.09
Unicode::Char = 0.02
Number::UN = 0.002

View File

@ -0,0 +1,131 @@
package DDG::Goodie::Conversions;
# ABSTRACT: convert between various weights and measures
use DDG::Goodie;
use Scalar::Util qw(looks_like_number);
# metric ton is base unit
# known SI units and aliases / plurals
my %units = (
# metric ton:
'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',
'kilograms' => '1000',
'kgs' => '1000',
'kilos' => '1000',
'kilogrammes' => '1000',
# gram:
'gram' => '1000000',
'g' => '1000000',
'gm' => '1000000',
'gramme' => '1000000',
'grams' => '1000000',
'gs' => '1000000',
'gms' => '1000000',
'grammes' => '1000000',
# milligram:
'milligram' => '1000000000',
'mg' => '1000000000',
'milligrams' => '1000000000',
'mgs' => '1000000000',
# microgram:
'microgram' => '1000000000',
'mcg' => '1000000000',
'micrograms' => '1000000000',
'mcgs' => '1000000000',
# long ton:
'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 tons' => '1.10231',
'tons' => '1.10231',
# stone:
'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',
'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;
name 'Conversions';
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', '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'];
zci answer_type => 'conversions';
handle query => sub {
my @matches = ($_ =~ /\b($keys)\b/gi);
return unless scalar @matches == 2; # conversion requires two triggers
# get factor:
my @args = split(/\s+/, $_);
my $factor = 1;
foreach my $arg (@args) {
if (looks_like_number($arg)) {
$factor = $arg unless $factor != 1; # drop n > 1 #s
return if $factor < 0; # negative weights seem impossible :)
}
}
# run the conversion:
return "$factor $matches[0] is " . sprintf("%.3f", $factor * ($units{$matches[1]} / $units{$matches[0]})) . " $matches[1]";
};
1;

35
t/Conversions.t Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'conversions';
ddg_goodie_test(
[
'DDG::Goodie::Conversions'
],
# @todo: could create a complete test suite for all toUnits->fromUnits
'convert 5 oz to g' => test_zci('5 oz is 141.747 g',),
'convert 1 ton to long ton' => test_zci('1 ton is 0.893 long ton',),
'convert 158 ounce to lbm' => test_zci('158 ounce is 9.875 lbm',),
'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 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;

View File

@ -37,4 +37,4 @@ ddg_goodie_test(
"sudoku toys" => undef,
);
done_testing;
done_testing;