Merge pull request #752 from duckduckgo/mwm/styler
NumberStyler: allow for space and underscore separation.master
commit
afe13a63ea
|
@ -32,15 +32,15 @@ triggers query_nowhitespace => qr<
|
|||
|
||||
(?: [0-9 \. ,]* )
|
||||
(?: gross | dozen | pi | e | c | squared | score |)
|
||||
[\( \) x X * % + / \^ 0-9 \. , \$ -]*
|
||||
[\( \) x X * % + / \^ 0-9 \. , _ \$ -]*
|
||||
|
||||
(?(1) (?: -? [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 | squared )+
|
||||
|
||||
(?: [0-9 \. ,]* )
|
||||
(?: gross | dozen | pi | e | c | squared | score |)
|
||||
|
||||
[\( \) x X * % + / \^ 0-9 \. , \$ -]* =?
|
||||
[\( \) x X * % + / \^ 0-9 \. , _ \$ -]* =?
|
||||
|
||||
$
|
||||
>xi;
|
||||
|
@ -145,6 +145,7 @@ sub prepare_for_display {
|
|||
|
||||
# Equals varies by output type.
|
||||
$query =~ s/\=$//;
|
||||
$query =~ s/(\d)[ _](\d)/$1$2/g; # Squeeze out spaces and underscores.
|
||||
# Show them how 'E' was interpreted. This should use the number styler, too.
|
||||
$query =~ s/((?:\d+?|\s))E(-?\d+)/\($1 * 10^$2\)/i;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ sub _build_number_regex {
|
|||
my $self = shift;
|
||||
my ($decimal, $thousands, $exponential) = ($self->decimal, $self->thousands, $self->exponential);
|
||||
|
||||
return qr/-?[\d\Q$decimal\E\Q$thousands\E]+(?:\Q$exponential\E-?\d+)?/;
|
||||
return qr/-?[\d_ \Q$decimal\E\Q$thousands\E]+(?:\Q$exponential\E-?\d+)?/;
|
||||
}
|
||||
|
||||
sub understands {
|
||||
|
@ -34,7 +34,7 @@ sub understands {
|
|||
# This assumes the exponentials are not included to give better answers.
|
||||
return (
|
||||
# The number must contain only things we understand: numerals and separators for this style.
|
||||
$number =~ /^-?(|\d|\Q$thousands\E|\Q$decimal\E)+$/
|
||||
$number =~ /^-?(|\d|_| |\Q$thousands\E|\Q$decimal\E)+$/
|
||||
&& (
|
||||
# The number is not required to contain thousands separators
|
||||
$number !~ /\Q$thousands\E/
|
||||
|
@ -64,6 +64,7 @@ sub for_computation {
|
|||
my ($self, $number_text) = @_;
|
||||
my ($decimal, $thousands, $exponential) = ($self->decimal, $self->thousands, $self->exponential);
|
||||
|
||||
$number_text =~ s/[ _]//g; # Remove spaces and underscores as visuals.
|
||||
$number_text =~ s/\Q$thousands\E//g; # Remove thousands seps, since they are just visual.
|
||||
$number_text =~ s/\Q$decimal\E/./g; # Make sure decimal mark is something perl knows how to use.
|
||||
if ($number_text =~ s/^([\d$decimal$thousands]+)\Q$exponential\E(-?[\d$decimal$thousands]+)$/$1e$2/ig) {
|
||||
|
@ -78,6 +79,7 @@ sub for_display {
|
|||
my ($self, $number_text) = @_;
|
||||
my ($decimal, $thousands, $exponential) = ($self->decimal, $self->thousands, $self->exponential);
|
||||
|
||||
$number_text =~ s/[ _]//g; # Remove spaces and underscores as visuals.
|
||||
if ($number_text =~ /(.*)\Q$exponential\E([+-]?\d+)/i) {
|
||||
$number_text = $self->for_display($1) . ' * 10^' . $self->for_display(int $2);
|
||||
} else {
|
||||
|
|
14
t/00-roles.t
14
t/00-roles.t
|
@ -24,8 +24,9 @@ subtest 'NumberStyler' => sub {
|
|||
[['4,431', '4.321'] => 'perl'],
|
||||
[['4,431', '4,32'] => 'euro'],
|
||||
[['4534,345.0', '1'] => 'perl'], # Unenforced commas.
|
||||
[['4,431', '4,32', '5,42'] => 'euro'],
|
||||
[['4,431', '4.32', '5.42'] => 'perl'],
|
||||
[['4,431', '4,32', '5,42'] => 'euro'],
|
||||
[['4,431', '4.32', '5.42'] => 'perl'],
|
||||
[['4_431_123', '4 32', '99.999 999'] => 'perl'],
|
||||
);
|
||||
|
||||
foreach my $tc (@valid_test_cases) {
|
||||
|
@ -39,10 +40,11 @@ subtest 'NumberStyler' => sub {
|
|||
subtest 'Invalid numbers' => sub {
|
||||
my @invalid_test_cases = (
|
||||
[['5234534.34.54', '1'] => 'has a mal-formed number'],
|
||||
[['4,431', '4,32', '4.32'] => 'is confusingly ambiguous'],
|
||||
[['4,431', '4.32.10', '5.42'] => 'is hard to figure'],
|
||||
[['4,431', '4,32,100', '5.42'] => 'has a mal-formed number'],
|
||||
[['4,431', '4,32,100', '5,42'] => 'is too crazy to work out'],
|
||||
[['4,431', '4,32', '4.32'] => 'is confusingly ambiguous'],
|
||||
[['4,431', '4.32.10', '5.42'] => 'is hard to figure'],
|
||||
[['4,431', '4,32,100', '5.42'] => 'has a mal-formed number'],
|
||||
[['4,431', '4,32,100', '5,42'] => 'is too crazy to work out'],
|
||||
[['4_431_123', "4\t32", '99.999 999'] => 'no tabs in numbers'],
|
||||
);
|
||||
|
||||
foreach my $tc (@invalid_test_cases) {
|
||||
|
|
|
@ -382,7 +382,16 @@ ddg_goodie_test(
|
|||
heading => 'Calculator',
|
||||
html => qr/./,
|
||||
),
|
||||
|
||||
'1 234 + 5 432' => test_zci(
|
||||
'1234 + 5432 = 6,666',
|
||||
heading => 'Calculator',
|
||||
html => qr/./,
|
||||
),
|
||||
'1_234 + 5_432' => test_zci(
|
||||
'1234 + 5432 = 6,666',
|
||||
heading => 'Calculator',
|
||||
html => qr/./,
|
||||
),
|
||||
'123.123.123.123/255.255.255.255' => undef,
|
||||
'83.166.167.160/27' => undef,
|
||||
'9 + 0 x 07' => undef,
|
||||
|
|
Loading…
Reference in New Issue