2015-01-20 16:22:59 -08:00
|
|
|
package DDG::Goodie::SolarSystem;
|
2015-01-20 16:54:20 -08:00
|
|
|
# ABSTRACT: Return various attributes of a object
|
2014-12-24 03:48:46 -08:00
|
|
|
|
|
|
|
use DDG::Goodie;
|
2015-05-29 15:18:03 -07:00
|
|
|
use YAML::XS 'LoadFile';
|
2014-12-24 04:33:24 -08:00
|
|
|
use POSIX;
|
2015-04-12 18:56:33 -07:00
|
|
|
use Text::Trim;
|
2015-05-15 15:38:18 -07:00
|
|
|
use strict;
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-01-20 16:22:59 -08:00
|
|
|
zci answer_type => "solarsystem";
|
2014-12-24 03:48:46 -08:00
|
|
|
zci is_cached => 1;
|
|
|
|
|
2015-01-20 16:22:59 -08:00
|
|
|
name "SolarSystem";
|
2014-12-24 03:48:46 -08:00
|
|
|
primary_example_queries 'size of venus';
|
|
|
|
secondary_example_queries 'what is the size of venus', 'volume of venus';
|
2015-01-20 16:54:20 -08:00
|
|
|
description 'Lookup various object attributes';
|
|
|
|
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/objects.pm";
|
2015-01-15 22:14:10 -08:00
|
|
|
category 'random';
|
2014-12-24 03:48:46 -08:00
|
|
|
topics 'special_interest';
|
2015-01-03 17:39:37 -08:00
|
|
|
attribution github => ["MrChrisW", "Chris Wilson"],
|
|
|
|
web => ["http://chrisjwilson.com", "Chris Wilson"];
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-04-09 16:00:36 -07:00
|
|
|
# Get Goodie version for use with image paths
|
|
|
|
my $goodieVersion = $DDG::GoodieBundle::OpenSourceDuckDuckGo::VERSION // 999;
|
|
|
|
|
2015-01-20 01:27:27 -08:00
|
|
|
my @attributesArray = ( 'size', 'radius', 'volume', 'mass', 'surface area', 'area');
|
2015-04-12 18:56:33 -07:00
|
|
|
my $attributesString = join('|', @attributesArray);
|
2015-01-20 01:27:27 -08:00
|
|
|
|
2015-04-05 04:13:35 -07:00
|
|
|
my @unitTriggers = ( 'kg', 'km2', 'km3', 'km', 'mi2', 'mi3', 'mi', 'lbs', 'metric', 'imperial');
|
2015-04-12 18:56:33 -07:00
|
|
|
my $unitsString = join('|', @unitTriggers);
|
2015-04-05 04:13:35 -07:00
|
|
|
|
2015-04-12 18:56:33 -07:00
|
|
|
triggers any => 'earth', 'jupiter', 'mars', 'mercury', 'neptune', 'saturn', 'uranus', 'venus', 'pluto', 'sun', 'moon';
|
2014-12-24 04:33:24 -08:00
|
|
|
|
2015-01-20 16:54:20 -08:00
|
|
|
# Load object data
|
2015-05-29 15:18:03 -07:00
|
|
|
my $objects = LoadFile(share('objects.yml'));
|
2014-12-24 03:48:46 -08:00
|
|
|
|
|
|
|
# Handle statement
|
2014-12-24 04:33:24 -08:00
|
|
|
handle query_lc => sub {
|
2015-04-12 18:13:06 -07:00
|
|
|
# Declare vars
|
2015-04-12 18:56:33 -07:00
|
|
|
my ($attribute, $result, $objectObj, $objectName, $saturn, $unitType, $operation);
|
2015-04-12 18:13:06 -07:00
|
|
|
|
2015-04-12 18:56:33 -07:00
|
|
|
s/(^what is)|(the)|(of)|(object)|(in)//g; # Remove common words
|
2015-04-12 18:13:06 -07:00
|
|
|
|
|
|
|
return unless /$attributesString/; # Ensure we match at least one attribute, eg. size, volume
|
|
|
|
|
|
|
|
# Set attribute depending on search query
|
2015-04-12 18:56:33 -07:00
|
|
|
if(m/size|radius/) { $attribute = "radius" }
|
|
|
|
elsif(m/volume/) { $attribute = "volume" }
|
|
|
|
elsif(m/mass/) { $attribute = "mass" }
|
|
|
|
elsif(m/area/) { $attribute = "surface_area" }
|
2015-04-12 18:13:06 -07:00
|
|
|
|
|
|
|
s/$attributesString//g; # Remove attributes
|
|
|
|
|
2015-04-12 18:56:33 -07:00
|
|
|
# Set unitType based on query string
|
2015-04-12 18:13:06 -07:00
|
|
|
# kg, km, metric | lbs mi imperial
|
|
|
|
if(m/kg|km\d?|metric/) {
|
2015-04-12 18:56:33 -07:00
|
|
|
$unitType = $attribute;
|
2015-04-12 18:13:06 -07:00
|
|
|
} elsif (m/lbs|mi\d?|imperial/) {
|
2015-04-12 18:56:33 -07:00
|
|
|
$unitType = $attribute."_imperial";
|
|
|
|
} else {
|
|
|
|
# Switch to imperial for non-metric countries based on location
|
|
|
|
# https://en.wikipedia.org/wiki/Metrication
|
|
|
|
if ($loc->country_code =~ m/US|MM|LR/i) {
|
|
|
|
$unitType = $attribute."_imperial";
|
|
|
|
} else {
|
|
|
|
$unitType = $attribute;
|
|
|
|
}
|
|
|
|
}
|
2015-04-12 18:13:06 -07:00
|
|
|
|
|
|
|
s/$unitsString//g; # Remove unit/unit type
|
2015-04-12 18:56:33 -07:00
|
|
|
trim($_); # Trim
|
2015-04-12 18:13:06 -07:00
|
|
|
|
|
|
|
return unless $_; # Return if empty query
|
|
|
|
$objectObj = $objects->{$_}; # Get object data
|
|
|
|
return unless $objectObj; # Return if we don't have a valid object
|
2015-04-12 18:56:33 -07:00
|
|
|
return unless $unitType; # Guard against no $unitType - should never occur
|
|
|
|
$result = $objectObj->{$unitType}; # Get data using correct unit type
|
|
|
|
$objectName = $_;
|
2015-04-12 18:13:06 -07:00
|
|
|
|
2015-04-12 18:56:33 -07:00
|
|
|
# Convert attribute surface_area = Surface Area
|
2015-04-12 18:13:06 -07:00
|
|
|
# Human friendly object name + attribute
|
2015-04-12 18:56:33 -07:00
|
|
|
if($attribute =~ "surface_area") { $attribute = "Surface Area"; }
|
|
|
|
$operation = ucfirst($_)." - ".ucfirst($attribute);
|
2015-04-12 18:13:06 -07:00
|
|
|
|
|
|
|
# Superscript for km3, mi3, km2 or mi2
|
|
|
|
if($result =~ m/(km|mi)(\d)/) {
|
2015-04-12 18:56:33 -07:00
|
|
|
my ($symbol, $superscript) = ($1, $2);
|
|
|
|
$result =~ s/$symbol$superscript/$symbol<sup>$superscript<\/sup>/;
|
2015-04-12 18:13:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Superscript for scientific notation
|
|
|
|
# Convert x to HTML entity ×
|
|
|
|
if($result =~ m/x\s(10)(\d\d)/) {
|
2015-04-12 18:56:33 -07:00
|
|
|
my ($number, $exponent) = ($1, $2);
|
|
|
|
$result =~ s/$number$exponent/$number<sup>$exponent<\/sup>/;
|
|
|
|
$result =~ s/x/×/;
|
2015-04-12 18:13:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#$saturn var is provided to handlebars template to set size of image
|
|
|
|
$saturn = ($objectName eq "saturn") ? 1 : 0;
|
|
|
|
|
|
|
|
#Return result and html
|
|
|
|
return $operation." is ".$result,
|
|
|
|
structured_answer => {
|
|
|
|
id => 'solar_system',
|
|
|
|
name => 'Answer',
|
|
|
|
data => {
|
|
|
|
attributes => $result,
|
|
|
|
operation => $operation,
|
|
|
|
imageName => $objectName,
|
|
|
|
saturn => $saturn,
|
|
|
|
goodie_version => $goodieVersion
|
|
|
|
},
|
|
|
|
meta => {
|
|
|
|
sourceUrl => "https://solarsystem.nasa.gov/planets/index.cfm",
|
|
|
|
sourceName => "NASA"
|
|
|
|
},
|
|
|
|
templates => {
|
|
|
|
group => 'base',
|
|
|
|
detail_mobile => 'DDH.solar_system.mobile',
|
|
|
|
options => {
|
|
|
|
content => 'DDH.solar_system.content',
|
2015-03-27 23:46:06 -07:00
|
|
|
}
|
2015-04-12 18:13:06 -07:00
|
|
|
}
|
|
|
|
};
|
2015-01-07 18:43:34 -08:00
|
|
|
};
|
2015-05-15 15:38:18 -07:00
|
|
|
1;
|