From 781856b4e01f11b355c7267a86488b2b9a197f4d Mon Sep 17 00:00:00 2001 From: Zach Thompson Date: Tue, 5 Sep 2017 15:43:43 -0600 Subject: [PATCH] SolarSystem: Use unicode superscripts, multiplication symbol (#4050) * Use unicode superscripts, multiplication symbol * Update tests with new unicode * Put trans in sub --- lib/DDG/Goodie/SolarSystem.pm | 13 ++++++++---- t/SolarSystem.t | 37 ++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/DDG/Goodie/SolarSystem.pm b/lib/DDG/Goodie/SolarSystem.pm index 1f6f29093..a1697e32d 100644 --- a/lib/DDG/Goodie/SolarSystem.pm +++ b/lib/DDG/Goodie/SolarSystem.pm @@ -5,6 +5,7 @@ use DDG::Goodie; use YAML::XS 'LoadFile'; use POSIX; use Text::Trim; +use utf8; use strict; zci answer_type => "solarsystem"; @@ -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$superscript<\/sup>/; + my $unisuper = unicode_superscript($superscript); + $result =~ s/$symbol$superscript/$symbol$unisuper/; } # Superscript for scientific notation - # Convert x to HTML entity × + # Convert x to unicode if($result =~ m/x\s(10)(\d\d)/) { my ($number, $exponent) = ($1, $2); - $result =~ s/$number$exponent/$number$exponent<\/sup>/; - $result =~ s/x/×/; + 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 @@ -112,4 +115,6 @@ handle query_lc => sub { } }; }; + +sub unicode_superscript { return $_[0] =~ tr/0123456789/⁰ⁱ²³⁴⁵⁶⁷⁸⁹/r } 1; diff --git a/t/SolarSystem.t b/t/SolarSystem.t index 21f4da127..238a683f2 100644 --- a/t/SolarSystem.t +++ b/t/SolarSystem.t @@ -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; @@ -34,30 +35,30 @@ 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 km3", structured_answer => $templateData), - 'volume of mars km' => test_zci("Mars - Volume is 163,115,609,799 km3", structured_answer => $templateData), - 'area of saturn km2' => test_zci("Saturn - Surface Area is 42,612,133,285 km2", structured_answer => $templateData), - 'mass of neptune kg' => test_zci("Neptune - Mass is 1.024 × 1026 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 km3", 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 mi3", structured_answer => $templateData), - 'volume of mars mi' => test_zci("Mars - Volume is 39,133,515,914 mi3", structured_answer => $templateData), - 'area of saturn mi' => test_zci("Saturn - Surface Area is 16,452,636,641 mi2", structured_answer => $templateData), - 'mass of neptune lbs' => test_zci("Neptune - Mass is 2.258 × 1026 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 mi3", structured_answer => $templateData), + 'volume of mars imperial' => test_zci("Mars - Volume is 39,133,515,914 mi³", structured_answer => $templateData), # Imperial @@ -65,10 +66,10 @@ ddg_goodie_test( "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 mi3", structured_answer => $templateData), - 'surface area of mercury' => test_zci("Mercury - Surface Area is 28,879,000 mi2", structured_answer => $templateData), - 'mass of neptune' => test_zci("Neptune - Mass is 2.258 × 1026 lbs", structured_answer => $templateData), - 'area of saturn' => test_zci("Saturn - Surface Area is 16,452,636,641 mi2", 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), @@ -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 km3", 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 km2", 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 × 1026 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 km2", 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),