zeroclickinfo-goodies/lib/DDG/Goodie/Dewey.pm

87 lines
2.2 KiB
Perl
Raw Normal View History

2012-08-05 09:24:07 -07:00
package DDG::Goodie::Dewey;
# ABSTRACT: Identify and find dewey decimal system numbers
use strict;
2012-08-05 09:24:07 -07:00
use DDG::Goodie;
2012-08-05 10:19:59 -07:00
triggers any => 'dewey';
2012-08-05 09:24:07 -07:00
zci answer_type => 'dewey_decimal';
zci is_cached => 1;
my %nums = share('dewey.txt')->slurp;
2012-08-17 18:42:06 -07:00
my %types = reverse %nums;
2012-08-05 09:24:07 -07:00
# get description for a number
2012-08-05 09:24:07 -07:00
sub get_info {
my($num) = @_;
2012-08-17 18:42:06 -07:00
my $desc = $nums{"$num\n"} or return;
2012-08-05 09:24:07 -07:00
chomp $desc;
$desc =~ s/\[\[([^\]]+?)\|(.+?)\]\]/$2/g;
$desc =~ s/\[\[(.+?)\]\]/$1/g;
$desc =~ s/\[\[.+?\|(.+?)\]\]/$1/g;
$desc =~ s/\[\[(.+?)\]\]/$1/g;
2012-08-05 09:24:07 -07:00
return $desc;
}
# add a key-value pair with number and description to $data
sub add_line {
my($num, $data) = @_;
2012-08-17 18:42:06 -07:00
chomp $num;
if(exists($nums{"$num\n"})) {
$data->{$num} = get_info($num) or return;
}
2012-08-17 18:42:06 -07:00
}
2012-08-05 09:24:07 -07:00
handle remainder => sub {
return unless /^(?:the)?\s*(?:decimal)?\s*(?:system)?\s*(?:numbers?|\#)?\s*
2012-08-17 18:42:06 -07:00
(?:
(?<num>\d{1,3})(?:\.\d+)?(?<multi>s)? |
(?<word>[\w\s]+?)
2012-08-17 18:42:06 -07:00
)
\s*(?:in)?\s*(?:the)?\s*(?:decimal)?\s*(?:system)?$/ix;
2012-08-17 18:42:06 -07:00
my $word = $+{'word'};
my $output = {};
2012-08-17 18:42:06 -07:00
if (defined $word) {
return if lc($word) eq 'system';
2012-08-17 18:42:06 -07:00
my @results = grep(/$word/i, keys %types);
return unless @results;
add_line($types{$_}, $output) for @results;
2012-08-05 09:24:07 -07:00
}
2012-08-17 18:42:06 -07:00
else {
my $formatted_num = sprintf "%03d", $+{'num'};
unless($+{'multi'}) {
add_line($formatted_num, $output)
2012-08-17 18:42:06 -07:00
}
elsif ($formatted_num =~ /\d00/) {
for my $x ($formatted_num .. $formatted_num+99) {
add_line($x, $output) or next;
2012-08-17 18:42:06 -07:00
}
}
elsif ($formatted_num =~ /\d\d0/) {
for my $x ($formatted_num .. $formatted_num+9) {
add_line($x, $output) or next;
2012-08-17 18:42:06 -07:00
}
2012-08-05 09:24:07 -07:00
}
}
2014-10-28 11:36:15 -07:00
return $output, structured_answer => {
id => 'dewey_decimal',
name => 'Answer',
templates => {
group => 'list',
options => {
content => 'record',
moreAt => 0
}
},
data => {
title => 'Dewey Decimal System',
record_data => $output
}
};
2012-08-05 09:24:07 -07:00
};
1;