Calculator: improve triggers; add 'squared'

Some of the problems inherent in the old trigger regex made themselves
obvious once the 'squared' operation was added. This simplification
should make maintenance a smidge easier.

Resolves #482.
master
Matt Miller 2014-06-10 07:43:59 +08:00
parent 8e291ac0cd
commit 5e6c1abb9a
2 changed files with 32 additions and 23 deletions

View File

@ -21,27 +21,6 @@ attribution
github => ['https://github.com/duckduckgo', 'duckduckgo'],
twitter => ['http://twitter.com/duckduckgo', 'duckduckgo'];
triggers query_nowhitespace => qr<
^
( what is | calculate | solve | math )? !?
[\( \) x X * % + / \^ \$ -]*
(?: [0-9 \. ,]* )
(?: gross | dozen | pi | e | c |)
[\( \) x X * % + / \^ 0-9 \. , \$ -]*
(?(1) (?: -? [0-9 \. ,]+ |) |)
(?: [\( \) x X * % + / \^ \$ -] | times | divided by | plus | minus | cos | sin | tan | cotan | log | ln | log[_]?\d{1,3} | exp | tanh | sec | csc)+
(?: [0-9 \. ,]* )
(?: gross | dozen | pi | e | c |)
[\( \) x X * % + / \^ 0-9 \. , \$ -]* =?
$
>xi;
# This is probably YAGNI territory, but since I have to reference it in two places
# and there are a multitude of other notation systems (although some break the
# 'thousands' assumption) I am going to pretend that I do need it.
@ -85,8 +64,11 @@ my %named_operations = (
'plus' => '+',
'divided\sby' => '/',
'ln' => 'log', # perl log() is natural log.
'squared' => '**2',
);
my $ored_operations = join('|', keys %named_operations);
my %named_constants = (
dozen => 12,
e => 2.71828182845904523536028747135266249, # This should be computed.
@ -96,12 +78,19 @@ my %named_constants = (
my $ored_constants = join('|', keys %named_constants); # For later substitutions
my $extra_trigger_words = qr/^(?:whatis|calculate|solve|math)/;
triggers query_nowhitespace => qr<
$extra_trigger_words?
(\s|$funcy|$ored_constants|$ored_operations|$numbery)*
$
>xi;
handle query_nowhitespace => sub {
my $results_html;
my $results_no_html;
my $query = $_;
$query =~ s/^(?:whatis|calculate|solve|math)//;
$query =~ s/$extra_trigger_words//;
if ($query !~ /[xX]\s*[\*\%\+\-\/\^]/ && $query !~ /^-?[\d]{2,3}\.\d+,\s?-?[\d]{2,3}\.\d+$/) {
my $tmp_result = '';
@ -195,7 +184,7 @@ sub spacing {
$text =~ s/(\s*(?<!<)(?:[\+\-\^xX\*\/\%]|times|plus|minus|dividedby)+\s*)/ $1 /ig;
$text =~ s/\s*dividedby\s*/ divided by /ig;
$text =~ s/(\d+?)((?:dozen|pi|gross))/$1 $2/ig;
$text =~ s/(\d+?)((?:dozen|pi|gross|squared))/$1 $2/ig;
$text =~ s/(\d+?)e/$1 e/g; # E == *10^n
$text =~ s/([\(\)\$])/ $1 /g if ($space_for_parse);

View File

@ -290,6 +290,26 @@ ddg_goodie_test(
heading => 'Calculator',
html => qq(<div>cos(2 pi) = <a href="javascript:;" onClick="document.x.q.value='1';document.x.q.focus();">1</a></div>),
),
'5 squared' => test_zci(
'5 squared = 25',
heading => 'Calculator',
html => qq(<div>5 squared = <a href="javascript:;" onClick="document.x.q.value='25';document.x.q.focus();">25</a></div>),
),
'1.0 + 5 squared' => test_zci(
'1.0 + 5 squared = 26.0',
heading => 'Calculator',
html => qq(<div>1.0 + 5 squared = <a href="javascript:;" onClick="document.x.q.value='26.0';document.x.q.focus();">26.0</a></div>),
),
'3 squared + 4 squared' => test_zci(
'3 squared + 4 squared = 25',
heading => 'Calculator',
html => qq(<div>3 squared + 4 squared = <a href="javascript:;" onClick="document.x.q.value='25';document.x.q.focus();">25</a></div>),
),
'2,2 squared' => test_zci(
'2,2 squared = 4,8',
heading => 'Calculator',
html => qq(<div>2,2 squared = <a href="javascript:;" onClick="document.x.q.value='4,8';document.x.q.focus();">4,8</a></div>),
),
'sin(1.0) + 1,05' => undef,
'4,24,334+22,53,828' => undef,
'5234534.34.54+1' => undef,