Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
SolarSystem: Use unicode superscripts, multiplication symbol (#4050)
Browse files Browse the repository at this point in the history
* Use unicode superscripts, multiplication symbol

* Update tests with new unicode

* Put trans in sub
  • Loading branch information
zachthompson authored and moollaza committed Sep 5, 2017
1 parent 50bb72d commit 781856b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
13 changes: 9 additions & 4 deletions lib/DDG/Goodie/SolarSystem.pm
Expand Up @@ -5,6 +5,7 @@ use DDG::Goodie;
use YAML::XS 'LoadFile';
use POSIX;
use Text::Trim;
use utf8;
use strict;

zci answer_type => "solarsystem";
Expand Down Expand Up @@ -75,15 +76,17 @@ handle query_lc => sub {
# Superscript for km3, mi3, km2 or mi2
if($result =~ m/(km|mi)(\d)/) {
my ($symbol, $superscript) = ($1, $2);
$result =~ s/$symbol$superscript/$symbol<sup>$superscript<\/sup>/;
my $unisuper = unicode_superscript($superscript);
$result =~ s/$symbol$superscript/$symbol$unisuper/;
}

# Superscript for scientific notation
# Convert x to HTML entity &times;
# Convert x to unicode
if($result =~ m/x\s(10)(\d\d)/) {
my ($number, $exponent) = ($1, $2);
$result =~ s/$number$exponent/$number<sup>$exponent<\/sup>/;
$result =~ s/x/&times;/;
my $uniexp = unicode_superscript($exponent);
$result =~ s/$number$exponent/$number$uniexp/;
$result =~ s/x/×/;
}

#$saturn var is provided to handlebars template to set size of image
Expand Down Expand Up @@ -112,4 +115,6 @@ handle query_lc => sub {
}
};
};

sub unicode_superscript { return $_[0] =~ tr/0123456789/⁰ⁱ²³⁴⁵⁶⁷⁸⁹/r }
1;
37 changes: 19 additions & 18 deletions t/SolarSystem.t
Expand Up @@ -6,6 +6,7 @@ use Test::More;
use Test::Deep;
use DDG::Test::Goodie;
use DDG::Test::Location;
use utf8;

zci answer_type => "solarsystem";
zci is_cached => 1;
Expand Down Expand Up @@ -34,41 +35,41 @@ ddg_goodie_test(
# Unit - km, kg
"size earth km" => test_zci("Earth - Radius is 6,371 km", structured_answer => $templateData),
"size earth in km" => test_zci("Earth - Radius is 6,371 km", structured_answer => $templateData),
'volume of mars km3' => test_zci("Mars - Volume is 163,115,609,799 km<sup>3</sup>", structured_answer => $templateData),
'volume of mars km' => test_zci("Mars - Volume is 163,115,609,799 km<sup>3</sup>", structured_answer => $templateData),
'area of saturn km2' => test_zci("Saturn - Surface Area is 42,612,133,285 km<sup>2</sup>", structured_answer => $templateData),
'mass of neptune kg' => test_zci("Neptune - Mass is 1.024 &times; 10<sup>26</sup> kg", structured_answer => $templateData),
'volume of mars km3' => test_zci("Mars - Volume is 163,115,609,799 km³", structured_answer => $templateData),
'volume of mars km' => test_zci("Mars - Volume is 163,115,609,799 km³", structured_answer => $templateData),
'area of saturn km2' => test_zci("Saturn - Surface Area is 42,612,133,285 km²", structured_answer => $templateData),
'mass of neptune kg' => test_zci("Neptune - Mass is 1.024 × 10²⁶ kg", structured_answer => $templateData),

# Unit type - metric
"size earth metric" => test_zci("Earth - Radius is 6,371 km", structured_answer => $templateData),
"size earth in metric" => test_zci("Earth - Radius is 6,371 km", structured_answer => $templateData),
'volume of mars metric' => test_zci("Mars - Volume is 163,115,609,799 km<sup>3</sup>", structured_answer => $templateData),
'volume of mars metric' => test_zci("Mars - Volume is 163,115,609,799 km³", structured_answer => $templateData),

# Imperial - using search trigger

# Unit - mi, lbs
"size earth mi" => test_zci("Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
"size earth in mi" => test_zci("Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
'volume of mars mi3' => test_zci("Mars - Volume is 39,133,515,914 mi<sup>3</sup>", structured_answer => $templateData),
'volume of mars mi' => test_zci("Mars - Volume is 39,133,515,914 mi<sup>3</sup>", structured_answer => $templateData),
'area of saturn mi' => test_zci("Saturn - Surface Area is 16,452,636,641 mi<sup>2</sup>", structured_answer => $templateData),
'mass of neptune lbs' => test_zci("Neptune - Mass is 2.258 &times; 10<sup>26</sup> lbs", structured_answer => $templateData),
'volume of mars mi3' => test_zci("Mars - Volume is 39,133,515,914 mi³", structured_answer => $templateData),
'volume of mars mi' => test_zci("Mars - Volume is 39,133,515,914 mi³", structured_answer => $templateData),
'area of saturn mi' => test_zci("Saturn - Surface Area is 16,452,636,641 mi²", structured_answer => $templateData),
'mass of neptune lbs' => test_zci("Neptune - Mass is 2.258 × 10²⁶ lbs", structured_answer => $templateData),

# Unit type - imperial
"size earth imperial" => test_zci("Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
"size earth in imperial" => test_zci("Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
'volume of mars imperial' => test_zci("Mars - Volume is 39,133,515,914 mi<sup>3</sup>", structured_answer => $templateData),
'volume of mars imperial' => test_zci("Mars - Volume is 39,133,515,914 mi³", structured_answer => $templateData),

# Imperial

"size earth" => test_zci("Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
"what is the size of earth" => test_zci( "Earth - Radius is 3,958.8 mi", structured_answer => $templateData),
'size of jupiter' => test_zci("Jupiter - Radius is 43,440.7 mi", structured_answer => $templateData),
'size of object jupiter' => test_zci("Jupiter - Radius is 43,440.7 mi", structured_answer => $templateData),
'volume of mars' => test_zci("Mars - Volume is 39,133,515,914 mi<sup>3</sup>", structured_answer => $templateData),
'surface area of mercury' => test_zci("Mercury - Surface Area is 28,879,000 mi<sup>2</sup>", structured_answer => $templateData),
'mass of neptune' => test_zci("Neptune - Mass is 2.258 &times; 10<sup>26</sup> lbs", structured_answer => $templateData),
'area of saturn' => test_zci("Saturn - Surface Area is 16,452,636,641 mi<sup>2</sup>", structured_answer => $templateData),
'volume of mars' => test_zci("Mars - Volume is 39,133,515,914 mi³", structured_answer => $templateData),
'surface area of mercury' => test_zci("Mercury - Surface Area is 28,879,000 mi²", structured_answer => $templateData),
'mass of neptune' => test_zci("Neptune - Mass is 2.258 × 10²⁶ lbs", structured_answer => $templateData),
'area of saturn' => test_zci("Saturn - Surface Area is 16,452,636,641 mi²", structured_answer => $templateData),
'radius of uranus' => test_zci("Uranus - Radius is 15,759.2 mi", structured_answer => $templateData),
'size of venus' => test_zci("Venus - Radius is 3,760.4 mi", structured_answer => $templateData),
'size of pluto' => test_zci("Pluto - Radius is 715.2 mi", structured_answer => $templateData),
Expand All @@ -86,13 +87,13 @@ ddg_goodie_test(

DDG::Request->new(query_raw => "size of object jupiter", location => test_location("au")) => test_zci("Jupiter - Radius is 69,911 km", structured_answer => $templateData),

DDG::Request->new(query_raw => "volume of mars", location => test_location("au")) => test_zci("Mars - Volume is 163,115,609,799 km<sup>3</sup>", structured_answer => $templateData),
DDG::Request->new(query_raw => "volume of mars", location => test_location("au")) => test_zci("Mars - Volume is 163,115,609,799 km³", structured_answer => $templateData),

DDG::Request->new(query_raw => "surface area of mercury", location => test_location("au")) => test_zci("Mercury - Surface Area is 74,797,000 km<sup>2</sup>", structured_answer => $templateData),
DDG::Request->new(query_raw => "surface area of mercury", location => test_location("au")) => test_zci("Mercury - Surface Area is 74,797,000 km²", structured_answer => $templateData),

DDG::Request->new(query_raw => "mass of neptune", location => test_location("au")) => test_zci("Neptune - Mass is 1.024 &times; 10<sup>26</sup> kg", structured_answer => $templateData),
DDG::Request->new(query_raw => "mass of neptune", location => test_location("au")) => test_zci("Neptune - Mass is 1.024 × 10²⁶ kg", structured_answer => $templateData),

DDG::Request->new(query_raw => "area of saturn", location => test_location("au")) => test_zci("Saturn - Surface Area is 42,612,133,285 km<sup>2</sup>", structured_answer => $templateData),
DDG::Request->new(query_raw => "area of saturn", location => test_location("au")) => test_zci("Saturn - Surface Area is 42,612,133,285 km²", structured_answer => $templateData),

DDG::Request->new(query_raw => "radius of uranus", location => test_location("au")) => test_zci("Uranus - Radius is 25,362 km", structured_answer => $templateData),

Expand Down

0 comments on commit 781856b

Please sign in to comment.