2014-11-27 23:02:08 -08:00
|
|
|
package DDG::Goodie::PeriodicTable;
|
|
|
|
# ABSTRACT: Atomic masses and numbers for chemical elements
|
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2014-11-27 23:02:08 -08:00
|
|
|
use DDG::Goodie;
|
|
|
|
use YAML::XS qw(Load);
|
2015-01-03 20:59:13 -08:00
|
|
|
use List::Util qw(first);
|
2014-11-27 23:02:08 -08:00
|
|
|
|
|
|
|
zci answer_type => 'periodic_table';
|
|
|
|
zci is_cached => 1;
|
|
|
|
|
|
|
|
name 'Periodic Table';
|
|
|
|
description 'Atomic masses and numbers for chemical elements';
|
|
|
|
primary_example_queries 'atomic mass of Nitrogen', 'atomic number of Oxygen';
|
|
|
|
secondary_example_queries 'atomic weight of Na';
|
|
|
|
category 'physical_properties';
|
|
|
|
topics 'science';
|
|
|
|
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/PeriodicTable.pm';
|
|
|
|
attribution github => [ 'zblair', 'Zachary D Blair' ];
|
|
|
|
|
|
|
|
my @elements = @{ Load( scalar share('elements.yml')->slurp ) };
|
|
|
|
|
|
|
|
# Triggers
|
2015-01-03 20:59:13 -08:00
|
|
|
triggers any => 'atomic mass', 'atomic weight', 'atomic number', 'proton number';
|
2014-11-27 23:02:08 -08:00
|
|
|
|
|
|
|
# Handle statement
|
|
|
|
handle query_lc => sub {
|
|
|
|
|
|
|
|
my $query = $_;
|
|
|
|
|
|
|
|
# Determine if this is a query for atomic mass or atomic number
|
|
|
|
my $is_mass_query = $query =~ /atomic mass|atomic weight/;
|
|
|
|
|
|
|
|
# Strip out irrelevant words in the query
|
2015-01-03 20:59:13 -08:00
|
|
|
$query =~ s/(?:atomic (?:mass|weight|number)|proton number|of|the|element|elemental)//g;
|
2014-11-27 23:02:08 -08:00
|
|
|
$query =~ s/^\s+|\s+$//g;
|
|
|
|
return unless $query;
|
|
|
|
|
|
|
|
# Look for a matching element in the table
|
2015-01-03 20:59:13 -08:00
|
|
|
my $match = first { lc $_->[2] eq $query || lc $_->[3] eq $query } @elements or return;
|
|
|
|
my ( $atomic_number, $atomic_mass, $element_name, $element_symbol ) = @{$match};
|
2014-11-27 23:02:08 -08:00
|
|
|
|
|
|
|
# Return the result if the element was found
|
|
|
|
if ($is_mass_query) {
|
|
|
|
return "$element_name ($element_symbol), Atomic mass $atomic_mass u", structured_answer => {
|
|
|
|
input => ["$element_name ($element_symbol)"],
|
2015-01-07 23:13:34 -08:00
|
|
|
operation => 'Atomic Mass',
|
2014-11-27 23:02:08 -08:00
|
|
|
result => "$atomic_mass u"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "$element_name ($element_symbol), Atomic number $atomic_number", structured_answer => {
|
|
|
|
input => ["$element_name ($element_symbol)"],
|
2015-01-07 23:13:34 -08:00
|
|
|
operation => 'Atomic Number',
|
2015-01-03 20:59:13 -08:00
|
|
|
result => $atomic_number
|
2014-11-27 23:02:08 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
1;
|