Merge pull request #2956 from zblair/master
Conversions: Add conversions for grains, carats, drams, cc's, cubic inches, mmHg, decades, centuries, millennia, and thousandths of an inchmaster
commit
2f507bb307
|
@ -28,7 +28,7 @@ my @triggers = map { lc $_ } @units;
|
|||
triggers any => @triggers;
|
||||
|
||||
# match longest possible key (some keys are sub-keys of other keys):
|
||||
my $keys = join '|', reverse sort { length($a) <=> length($b) } @units;
|
||||
my $keys = join '|', map { quotemeta $_ } reverse sort { length($a) <=> length($b) } @units;
|
||||
my $question_prefix = qr/(?<prefix>convert|what (?:is|are|does)|how (?:much|many|long) (?:is|are)?|(?:number of)|(?:how to convert))?/;
|
||||
|
||||
# guards and matches regex
|
||||
|
@ -51,6 +51,13 @@ my %plural_exceptions = (
|
|||
'mph' => 'mph',
|
||||
'm/s' => 'm/s',
|
||||
'ft/s' => 'ft/s',
|
||||
'dram avoirdupois' => 'drams avoirdupois',
|
||||
'thousandth of an inch' => 'thousandths of an inch',
|
||||
'century' => 'centuries',
|
||||
'millennium' => 'millennia',
|
||||
'mmHg' => 'mmHg',
|
||||
'torr' => 'torr',
|
||||
'cubic inch' => 'cubic inches'
|
||||
);
|
||||
my %singular_exceptions = reverse %plural_exceptions;
|
||||
|
||||
|
@ -255,6 +262,7 @@ sub get_matches {
|
|||
}
|
||||
sub convert {
|
||||
my ($conversion) = @_;
|
||||
|
||||
my @matches = get_matches($conversion->{'from_unit'}, $conversion->{'to_unit'});
|
||||
|
||||
return if $conversion->{'factor'} < 0 && !($matches[0]->{'can_be_negative'});
|
||||
|
|
|
@ -100,6 +100,33 @@ factor: 1.10231
|
|||
type: mass
|
||||
unit: ton
|
||||
---
|
||||
aliases:
|
||||
- grain
|
||||
- grains
|
||||
- gr
|
||||
- troy grain
|
||||
- troy grains
|
||||
factor: 15432358.3529414
|
||||
type: mass
|
||||
unit: grain
|
||||
---
|
||||
aliases:
|
||||
- carat
|
||||
- carats
|
||||
factor: 5000000
|
||||
type: mass
|
||||
unit: carat
|
||||
---
|
||||
aliases:
|
||||
- dram
|
||||
- drams
|
||||
- dram avoirdupois
|
||||
- drams avoirdupois
|
||||
- avoirdupois dram
|
||||
factor: 564383.3912
|
||||
type: mass
|
||||
unit: drams avoirdupois
|
||||
---
|
||||
aliases:
|
||||
- meters
|
||||
- metre
|
||||
|
@ -141,6 +168,17 @@ factor: 1000
|
|||
type: length
|
||||
unit: millimeter
|
||||
---
|
||||
aliases:
|
||||
- thou
|
||||
- thous
|
||||
- mil
|
||||
- mils
|
||||
- thousandth of an inch
|
||||
- thousandths of an inch
|
||||
factor: 39370.1
|
||||
type: length
|
||||
unit: thousandth of an inch
|
||||
---
|
||||
aliases:
|
||||
- miles
|
||||
- mi
|
||||
|
@ -395,6 +433,19 @@ factor: 1000
|
|||
type: volume
|
||||
unit: millilitre
|
||||
---
|
||||
aliases:
|
||||
- cubic inch
|
||||
- cubic inches
|
||||
- cubic in
|
||||
- cu inch
|
||||
- cu in
|
||||
- inch^3
|
||||
- in^3
|
||||
- cui
|
||||
factor: 61.024
|
||||
type: volume
|
||||
unit: cubic inch
|
||||
---
|
||||
aliases:
|
||||
- metre^3
|
||||
- meter^3
|
||||
|
@ -413,6 +464,8 @@ aliases:
|
|||
- centimeters^3
|
||||
- cm^3
|
||||
- cm³
|
||||
- cc
|
||||
- ccm
|
||||
factor: 1000
|
||||
type: volume
|
||||
unit: cubic centimetre
|
||||
|
@ -610,6 +663,27 @@ factor: 0.00273972602739726
|
|||
type: duration
|
||||
unit: year
|
||||
---
|
||||
aliases:
|
||||
- decade
|
||||
- decades
|
||||
factor: 0.000273972602739726
|
||||
type: duration
|
||||
unit: decade
|
||||
---
|
||||
aliases:
|
||||
- century
|
||||
- centuries
|
||||
factor: 0.0000273972602739726
|
||||
type: duration
|
||||
unit: century
|
||||
---
|
||||
aliases:
|
||||
- millennium
|
||||
- millennia
|
||||
factor: 0.00000273972602739726
|
||||
type: duration
|
||||
unit: millennium
|
||||
---
|
||||
aliases:
|
||||
- leap years
|
||||
- leapyear
|
||||
|
@ -677,6 +751,19 @@ factor: 0.000145036839357197
|
|||
type: pressure
|
||||
unit: pounds per square inch
|
||||
---
|
||||
aliases:
|
||||
- mmhg
|
||||
factor: 0.00750061683
|
||||
type: pressure
|
||||
unit: mmHg
|
||||
---
|
||||
aliases:
|
||||
- torr
|
||||
- torrs
|
||||
factor: 0.00750062
|
||||
type: pressure
|
||||
unit: torr
|
||||
---
|
||||
aliases:
|
||||
- joules
|
||||
- j
|
||||
|
|
123
t/Conversions.t
123
t/Conversions.t
|
@ -299,6 +299,90 @@ ddg_goodie_test(
|
|||
physical_quantity => 'mass'
|
||||
})
|
||||
),
|
||||
'124 grains in grams' => test_zci(
|
||||
'124 grains = 8.035 grams',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '124',
|
||||
raw_input => '124',
|
||||
from_unit => 'grains',
|
||||
styled_output => '8.035',
|
||||
raw_answer => '8.035',
|
||||
to_unit => 'grams',
|
||||
physical_quantity => 'mass'
|
||||
})
|
||||
),
|
||||
'1 carat in mg' => test_zci(
|
||||
'1 carat = 200 milligrams',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '1',
|
||||
raw_input => '1',
|
||||
from_unit => 'carat',
|
||||
styled_output => '200',
|
||||
raw_answer => '200',
|
||||
to_unit => 'milligrams',
|
||||
physical_quantity => 'mass'
|
||||
})
|
||||
),
|
||||
'1 dram in grams' => test_zci(
|
||||
'1 dram avoirdupois = 1.772 grams',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '1',
|
||||
raw_input => '1',
|
||||
from_unit => 'dram avoirdupois',
|
||||
styled_output => '1.772',
|
||||
raw_answer => '1.772',
|
||||
to_unit => 'grams',
|
||||
physical_quantity => 'mass'
|
||||
})
|
||||
),
|
||||
'1.5 mmHg to Pascals' => test_zci(
|
||||
'1.5 mmHg = 199.984 pascals',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '1.5',
|
||||
raw_input => '1.5',
|
||||
from_unit => 'mmHg',
|
||||
styled_output => '199.984',
|
||||
raw_answer => '199.984',
|
||||
to_unit => 'pascals',
|
||||
physical_quantity => 'pressure'
|
||||
})
|
||||
),
|
||||
'5.4 torr to Pascals' => test_zci(
|
||||
'5.4 torr = 719.940 pascals',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '5.4',
|
||||
raw_input => '5.4',
|
||||
from_unit => 'torr',
|
||||
styled_output => '719.940',
|
||||
raw_answer => '719.940',
|
||||
to_unit => 'pascals',
|
||||
physical_quantity => 'pressure'
|
||||
})
|
||||
),
|
||||
'2 thou to mm' => test_zci(
|
||||
'2 thousandths of an inch = 0.051 millimeters',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '2',
|
||||
raw_input => '2',
|
||||
from_unit => 'thousandths of an inch',
|
||||
styled_output => '0.051',
|
||||
raw_answer => '0.051',
|
||||
to_unit => 'millimeters',
|
||||
physical_quantity => 'length'
|
||||
})
|
||||
),
|
||||
'1 cubic inch to mL' => test_zci(
|
||||
'1 cubic inch = 16.387 millilitres',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '1',
|
||||
raw_input => '1',
|
||||
from_unit => 'cubic inch',
|
||||
styled_output => '16.387',
|
||||
raw_answer => '16.387',
|
||||
to_unit => 'millilitres',
|
||||
physical_quantity => 'volume'
|
||||
})
|
||||
),
|
||||
'2 miles to km' => test_zci(
|
||||
'2 miles = 3.219 kilometers',
|
||||
structured_answer => make_answer({
|
||||
|
@ -623,6 +707,42 @@ ddg_goodie_test(
|
|||
physical_quantity => 'duration'
|
||||
})
|
||||
),
|
||||
'3 decades in years' => test_zci(
|
||||
'3 decades = 30 years',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '3',
|
||||
raw_input => '3',
|
||||
from_unit => 'decades',
|
||||
styled_output => '30',
|
||||
raw_answer => '30',
|
||||
to_unit => 'years',
|
||||
physical_quantity => 'duration'
|
||||
})
|
||||
),
|
||||
'2 centuries in years' => test_zci(
|
||||
'2 centuries = 200 years',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '2',
|
||||
raw_input => '2',
|
||||
from_unit => 'centuries',
|
||||
styled_output => '200',
|
||||
raw_answer => '200',
|
||||
to_unit => 'years',
|
||||
physical_quantity => 'duration'
|
||||
})
|
||||
),
|
||||
'2 millennia in years' => test_zci(
|
||||
'2 millennia = 2,000 years',
|
||||
structured_answer => make_answer({
|
||||
markup_input => '2',
|
||||
raw_input => '2',
|
||||
from_unit => 'millennia',
|
||||
styled_output => '2,000',
|
||||
raw_answer => '2000',
|
||||
to_unit => 'years',
|
||||
physical_quantity => 'duration'
|
||||
})
|
||||
),
|
||||
'5 GB to megabyte' => test_zci(
|
||||
'5 gigabytes = 5,000 megabytes',
|
||||
structured_answer => make_answer({
|
||||
|
@ -2657,6 +2777,7 @@ ddg_goodie_test(
|
|||
'feet' => undef,
|
||||
'minutes' => undef,
|
||||
'99999999999000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 miles in mm' => undef,
|
||||
'1E300 miles in metres' => undef
|
||||
'1E300 miles in metres' => undef,
|
||||
'5 pas.i to atm' => undef,
|
||||
);
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue