Merge pull request #2775 from duckduckgo/gd/calc-roots-full-template

CalcRoots: Update to use full template
master
Zaahir Moolla 2016-06-08 19:10:20 -04:00
commit 404420734c
3 changed files with 77 additions and 81 deletions

View File

@ -11,6 +11,8 @@ zci is_cached => 1;
zci answer_type => 'root';
my $UPPER_BOUND = 1000000000; # 1 Billion
handle query => sub {
# The 'root' trigger is very ambigous so this regex provides the specific triggers
return unless m/^((?:.*square|.*cube(?:d|)|.*th|.*rd|.*nd|.*st|.*[0-9]+)) root(?: of|) (?!of)(.*)/i;
@ -53,6 +55,7 @@ handle query => sub {
# Solve using the absolute value of the base
$base = abs($base);
return unless $base < $UPPER_BOUND;
my $calc = $base ** (1/$exp);
if($sign eq "-"){$calc = -$calc;}
@ -85,59 +88,57 @@ handle query => sub {
$base = $';
$base = str2nbr($base) if $base =~ m/[^0-9]/;
$base =~ s/[^0-9\.]//g;
if ($base ne '') {
return unless $base ne '' && $base < $UPPER_BOUND;
my $calc = $base ** (1/$exp) * -1;
if($sign eq "-"){$calc = -$calc;}
my $secondsign = "-"; #sign of the calcuated answer
if($calc>0){$secondsign="";}
# Try and simplify the radical if answer is not a whole number
unless(($calc - int($calc))==0){
my $count = int(abs($calc));
while ($count > 1) {
my $calc = $base ** (1/$exp) * -1;
if($sign eq "-"){$calc = -$calc;}
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
my $secondsign = "-"; #sign of the calcuated answer
if($calc>0){$secondsign="";}
if ( ($newBase - int($newBase)) == 0) {
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc ($secondsign$count times the $sign$exp-root of $newBase).", qq|$sign<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($secondsign$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
# Try and simplify the radical if answer is not a whole number
unless(($calc - int($calc))==0){
my $count = int(abs($calc));
while ($count > 1) {
$count--;
}
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
if ( ($newBase - int($newBase)) == 0) {
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc ($secondsign$count times the $sign$exp-root of $newBase).", qq|$sign<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($secondsign$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
$count--;
}
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc.", qq|$sign<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc.", qq|$sign<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
elsif ($exp =~ m/[0-9]+/) {
# Solve normally
$base = str2nbr($base) if $base =~ m/[^0-9]/;
$base =~ s/[^0-9\.]//g;
if ($base ne '') {
my $calc = $base ** (1/$exp);
if($sign eq "-"){$calc = -$calc;}
#If the answer is not a whole number, try to simplify the radical
unless(($calc - int($calc))==0){
my $count = int(abs($calc));
while ($count > 1) {
return unless $base ne '' && $base < $UPPER_BOUND;
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
my $calc = $base ** (1/$exp);
if($sign eq "-"){$calc = -$calc;}
if ( ($newBase - int($newBase)) == 0) {
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc ($sign$count times the $exp-root of $newBase).", qq|$sign<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($sign$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
#If the answer is not a whole number, try to simplify the radical
unless(($calc - int($calc))==0){
my $count = int(abs($calc));
while ($count > 1) {
$count--;
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
if ( ($newBase - int($newBase)) == 0) {
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc ($sign$count times the $exp-root of $newBase).", qq|$sign<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($sign$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
$count--;
}
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc.", qq|$sign<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc.", qq|$sign<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
return;
@ -145,13 +146,19 @@ handle query => sub {
sub structured{
my($exp,$base,$text, $html) = @_;
return $text,
heading => "Root Calculator",
structured_answer => {
input => ["$exp-root of $base"],
operation => 'Calculate',
result => $html,
};
return $text,
structured_answer => {
data => {
title => $html,
subtitle => "Calculate $exp-root of $base",
},
templates => {
group => 'text',
options => {
title_content => 'DDH.calc_roots.title',
},
},
};
}
1;

View File

@ -0,0 +1 @@
<h3 class="c-base__title">{{{title}}}</h3>

View File

@ -9,45 +9,33 @@ use DDG::Test::Goodie;
zci answer_type => 'root';
zci is_cached => 1;
sub build_structured_answer {
my ($exp, $base, $text, $html) = @_;
return $text,
structured_answer => {
data => {
title => $html,
subtitle => "Calculate $exp-root of $base",
},
templates => {
group => 'text',
options => {
title_content => 'DDH.calc_roots.title',
},
},
};
}
sub build_test { test_zci(build_structured_answer(@_)) }
ddg_goodie_test(
[qw(
DDG::Goodie::CalcRoots
)],
'square root of negative 9' => test_zci(
"The 2-root of -9 is 3 i",
heading => "Root Calculator",
structured_answer => {
input => ["2-root of -9"],
operation => 'Calculate',
result => "<sup>2</sup>&radic;-9 = 3<em>i</em>"
}
),
'negative square root of negative 25' =>test_zci(
"The -2-root of -25 is -5 i",
heading => "Root Calculator",
structured_answer => {
input => ["-2-root of -25"],
operation => 'Calculate',
result => "-<sup>2</sup>&radic;-25 = -5<em>i</em>"
}
),
'2nd root of 100' => test_zci(
"The 2-root of 100 is 10.",
heading => "Root Calculator",
structured_answer => {
input => ["2-root of 100"],
operation => 'Calculate',
result => qq|<sup>2</sup>&radic;100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a>|
}
),
'cube root of 33' => test_zci(
"The 3-root of 33 is 3.20753432999583.",
heading => "Root Calculator",
structured_answer => {
input => ["3-root of 33"],
operation => 'Calculate',
result => qq|<sup>3</sup>&radic;33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>|
}
),
[qw( DDG::Goodie::CalcRoots )],
'square root of negative 9' => build_test('2', '-9', 'The 2-root of -9 is 3 i', '<sup>2</sup>&radic;-9 = 3<em>i</em>'),
'negative square root of negative 25' => build_test('-2', '-25', 'The -2-root of -25 is -5 i', '-<sup>2</sup>&radic;-25 = -5<em>i</em>'),
'2nd root of 100' => build_test('2', '100', 'The 2-root of 100 is 10.',
qq|<sup>2</sup>&radic;100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a>|),
'cube root of 33' => build_test('3', '33', 'The 3-root of 33 is 3.20753432999583.',
qq|<sup>3</sup>&radic;33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>|),
'2nd root of 9999999999' => undef
);
done_testing;