Conversions: kB and kb (and so on) restored! (#4077)

* Conversions: Unphysical units like millibits (mb) and millibytes(mB) should be assumed as mega instead

* Conversions: Now has multiple symbols (case sensitive matches) meaning that Kb and kb both mean kilobits and KB and kB both mean kilobytes :D
master
Rob Emery 2017-04-10 17:01:50 +01:00 committed by Zaahir Moolla
parent 44278eb98c
commit 394cf054f3
3 changed files with 269 additions and 135 deletions

View File

@ -25,7 +25,7 @@ foreach my $type (@types) {
push(@units, $type->{'unit'});
push(@units, $type->{'plural'}) unless lc $type->{'unit'} eq lc $type->{'plural'};
push(@units, @{$type->{'aliases'}});
push(@units, $type->{'symbol'}) if $type->{'symbol'};
push(@units, @{$type->{'symbols'}}) if $type->{'symbols'};
$unit_to_plural{lc $type->{'unit'}} = $type->{'plural'};
$plural_to_unit{lc $type->{'plural'}} = $type->{'unit'};
}
@ -90,7 +90,7 @@ handle query => sub {
# gets factors for comparison
foreach my $type (@types) {
if( lc $+{'left_unit'} eq lc $type->{'unit'} || $type->{'symbol'} && $+{'left_unit'} eq $type->{'symbol'}) {
if( lc $+{'left_unit'} eq lc $type->{'unit'} || $type->{'symbols'} && grep {$_ eq $+{'left_unit'} } @{$type->{'symbols'}}) {
push(@factor1, $type->{'factor'});
}
@ -101,7 +101,7 @@ handle query => sub {
}
}
if(lc $+{'right_unit'} eq lc $type->{'unit'} || $type->{'symbol'} && $+{'right_unit'} eq $type->{'symbol'}) {
if(lc $+{'right_unit'} eq lc $type->{'unit'} || $type->{'symbols'} && grep {$_ eq $+{'right_unit'} } @{$type->{'symbols'}}) {
push(@factor2, $type->{'factor'});
}
@ -237,7 +237,7 @@ sub get_matches {
my @output_matches = ();
foreach my $match (@input_matches) {
foreach my $type (@types) {
if ($type->{'symbol'} && $match eq $type->{'symbol'}
if (($type->{'symbols'} && grep { $_ eq $match } @{$type->{'symbols'}})
|| lc $match eq lc $type->{'unit'}
|| lc $match eq lc $type->{'plural'}
|| grep { $_ eq lc $match } @{$type->{'aliases'}} ) {

File diff suppressed because it is too large Load Diff

View File

@ -2936,6 +2936,140 @@ ddg_goodie_test(
physical_quantity => 'digital'
})
),
'convert 100MB in KB' => test_zci(
'100 megabytes = 100,000 kilobytes',
structured_answer => make_answer({
markup_input => '100',
raw_input => '100',
from_unit => 'megabytes',
styled_output => '100,000',
raw_answer => '100000',
to_unit => 'kilobytes',
physical_quantity => 'digital'
})
),
'40 pb to tb' => test_zci(
'40 petabits = 40,000 terabits',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'petabits',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'terabits',
physical_quantity => 'digital'
})
),
'40 tb to gb' => test_zci(
'40 terabits = 40,000 gigabits',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'terabits',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'gigabits',
physical_quantity => 'digital'
})
),
'40 gb to mb' => test_zci(
'40 gigabits = 40,000 megabits',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'gigabits',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'megabits',
physical_quantity => 'digital'
})
),
'40 mb to kb' => test_zci(
'40 megabits = 40,000 kilobits',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'megabits',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'kilobits',
physical_quantity => 'digital'
})
),
'40 kb to b' => test_zci(
'40 kilobits = 40,000 bits',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'kilobits',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'bits',
physical_quantity => 'digital'
})
),
'40 pB to tB' => test_zci(
'40 petabytes = 40,000 terabytes',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'petabytes',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'terabytes',
physical_quantity => 'digital'
})
),
'40 tB to gB' => test_zci(
'40 terabytes = 40,000 gigabytes',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'terabytes',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'gigabytes',
physical_quantity => 'digital'
})
),
'40 gB to mB' => test_zci(
'40 gigabytes = 40,000 megabytes',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'gigabytes',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'megabytes',
physical_quantity => 'digital'
})
),
'40 mB to kB' => test_zci(
'40 megabytes = 40,000 kilobytes',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'megabytes',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'kilobytes',
physical_quantity => 'digital'
})
),
'40 kB to B' => test_zci(
'40 kilobytes = 40,000 bytes',
structured_answer => make_answer({
markup_input => '40',
raw_input => '40',
from_unit => 'kilobytes',
styled_output => '40,000',
raw_answer => '40000',
to_unit => 'bytes',
physical_quantity => 'digital'
})
),
'50 mph in kph' => test_zci(
'50 mph = 80.467 km/h',
structured_answer => make_answer({