Merge pull request #1974 from GuiltyDolphin/sigfigs-fix

SigFigs: Update to use templates + other improvements
master
Zaahir Moolla 2016-01-31 12:41:34 -05:00
commit f24d1d8322
2 changed files with 89 additions and 40 deletions

View File

@ -3,39 +3,60 @@ package DDG::Goodie::SigFigs;
use strict;
use DDG::Goodie;
with 'DDG::GoodieRole::NumberStyler';
triggers start => 'sigfigs', 'sigdigs', 'sf', 'sd', 'significant';
triggers any => 'sigfigs', 'sig figs', 'sf', 'significant';
zci answer_type => "sig_figs";
zci is_cached => 1;
handle remainder => sub {
$_ =~ s/^(figures|digits)\s*//g;
return unless /^-?\d+(?:\.(?:\d+)?)?$/;
$_ =~ s/-//;
$_ =~ s/^0+//;
my @arr = split('\\.', $_);
my $v = @arr;
my $len = 0;
# there's a decimal
unless ($v eq 1) {
# the string doesn't have integers on the left
# this means we can strip the leading zeros on the right
if ($_ < 1) {
$arr[1] =~ s/^0+//;
$len = length $arr[1];
}
#there are integers on the left
else {
$len = length($arr[0]) + length($arr[1]);
}
}
# no decimal
else {
# lose the trailing zeros and count
$_ =~ s/\.?0*$//;
$len = length $_;
}
return "Significant figures: $len";
sub get_sig_figs {
my $num = shift;
$num =~ s/^[-+]+//;
# Leading digits NEVER contribute towards significant figures.
$num =~ s/^0+//;
$num =~ /^(?<int_part>\d*+)(\.(?<frac_part>\d*))?$/;
my $int_part = $+{'int_part'};
my $frac_part = $+{'frac_part'};
my $sigfigs = length $int_part;
if (defined $frac_part) {
# Leading zeros after decimal point aren't significant if there
# was no integer part or the integer part consisted of only zeros.
$frac_part =~ s/^0+// if $sigfigs == 0;
return $sigfigs + length $frac_part;
};
# This isn't necessarily correct - significant figures can be
# ambiguous when not using scientific notation.
$int_part =~ s/0+$//;
return length $int_part;
}
my $number_re = number_style_regex();
handle query_raw => sub {
my $query = $_;
$query =~ s/.*?(sf|sig(nificant)? ?(fig(ure)?|digit)s)[^,.\d]*+//i;
return if $query eq '';
$query =~ /^($number_re)\D*?$/ or return;
my $number_match = $1;
my $style = number_style_for($number_match);
return unless $style;
my $formatted_input = $style->for_display($number_match);
my $to_compute = $style->for_computation($number_match);
my $sigfigs = get_sig_figs $to_compute;
return unless defined $sigfigs;
return $sigfigs, structured_answer => {
id => 'sig_figs',
name => 'Answer',
data => {
title => "$sigfigs",
subtitle => "Number of Significant Figures in $formatted_input",
},
templates => {
group => 'text',
moreAt => 0,
},
};
};
1;

View File

@ -8,18 +8,46 @@ use DDG::Test::Goodie;
zci answer_type => 'sig_figs';
zci is_cached => 1;
sub build_structured_answer {
my ($exp_result, $exp_input) = @_;
return $exp_result, structured_answer => {
id => 'sig_figs',
name => 'Answer',
data => {
title => "$exp_result",
subtitle => "Number of Significant Figures in $exp_input",
},
templates => {
group => 'text',
moreAt => 0,
},
};
}
sub build_test { test_zci(build_structured_answer(@_)) }
ddg_goodie_test(
[qw(
DDG::Goodie::SigFigs
)],
'sf 78' => test_zci('Significant figures: 2'),
"sigfigs 10.12404" => test_zci("Significant figures: 7"),
"sigdigs 3030." => test_zci("Significant figures: 4"),
"significant figures 0.0100235" => test_zci("Significant figures: 6"),
"sf 302.056" => test_zci("Significant figures: 6"),
"sd 045.30" => test_zci("Significant figures: 4"),
'sigfigs 01.1234000' => test_zci('Significant figures: 8'),
'significant figures 000123000' => test_zci('Significant figures: 3'),
[qw( DDG::Goodie::SigFigs)],
'sf 78' => build_test('2', '78'),
"sigfigs 10.12404" => build_test('7', '10.12404'),
"significant digits of 3030." => build_test('4', '3,030.'),
"significant figures 0.0100235" => build_test('6', '0.0100235'),
"sf 302.056" => build_test('6', '302.056'),
"significant figs 045.30" => build_test('4', '045.30'),
'sigfigs 01.1234000' => build_test('8', '01.1234000'),
'significant figures 000123000' => build_test('3', '000,123,000'),
'sigfigs 0' => build_test('0', '0'),
'sf 001,70' => build_test('3', '001,70'),
'sf 1_200.' => build_test('4', '1,200.'),
'sf 1. 5h' => build_test('2', '1.5'),
'sig figs 501,.3' => build_test('4', '501,.3'),
'sf' => undef,
'significant figures a' => undef,
'significant figures of 1.230' => build_test('4', '1.230'),
'sig figs 1h1' => undef,
'sig figs 000.' => build_test('0', '000.'),
'What are the significant figures of 312?' => build_test('3', '312'),
'How many sig figs are there in 11.3' => build_test('3', '11.3'),
);
done_testing;