2012-08-05 09:24:07 -07:00
|
|
|
package DDG::Goodie::Dewey;
|
|
|
|
# ABSTRACT: Identify and find dewey decimal system numbers
|
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
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
|
|
|
|
2016-05-31 17:23:35 -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;
|
2016-05-31 17:23:35 -07:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-07-02 15:32:00 -07:00
|
|
|
# add a key-value pair with number and description to $data
|
2016-07-02 15:50:39 -07:00
|
|
|
sub add_line {
|
2016-07-02 15:32:00 -07:00
|
|
|
my($num, $data) = @_;
|
2012-08-17 18:42:06 -07:00
|
|
|
chomp $num;
|
2016-05-31 17:23:35 -07:00
|
|
|
if(exists($nums{"$num\n"})) {
|
2016-07-02 15:32:00 -07:00
|
|
|
$data->{$num} = get_info($num) or return;
|
2016-05-31 17:23:35 -07:00
|
|
|
}
|
2012-08-17 18:42:06 -07:00
|
|
|
}
|
|
|
|
|
2012-08-05 09:24:07 -07:00
|
|
|
handle remainder => sub {
|
2016-07-03 03:23:53 -07:00
|
|
|
return unless /^(?:the)?\s*(?:decimal)?\s*(?:system)?\s*(?:numbers?|\#)?\s*
|
2012-08-17 18:42:06 -07:00
|
|
|
(?:
|
2016-07-02 15:50:39 -07:00
|
|
|
(?<num>\d{1,3})(?:\.\d+)?(?<multi>s)? |
|
|
|
|
(?<word>[\w\s]+?)
|
2012-08-17 18:42:06 -07:00
|
|
|
)
|
2016-07-03 03:23:53 -07:00
|
|
|
\s*(?:in)?\s*(?:the)?\s*(?:decimal)?\s*(?:system)?$/ix;
|
2012-08-17 18:42:06 -07:00
|
|
|
|
2016-07-02 15:50:39 -07:00
|
|
|
my $word = $+{'word'};
|
2016-07-02 15:36:54 -07:00
|
|
|
my $output = {};
|
2016-07-02 15:32:00 -07:00
|
|
|
|
2012-08-17 18:42:06 -07:00
|
|
|
if (defined $word) {
|
2016-07-02 15:50:39 -07:00
|
|
|
return if lc($word) eq 'system';
|
2012-08-17 18:42:06 -07:00
|
|
|
my @results = grep(/$word/i, keys %types);
|
|
|
|
return unless @results;
|
2016-07-02 15:50:39 -07:00
|
|
|
add_line($types{$_}, $output) for @results;
|
2012-08-05 09:24:07 -07:00
|
|
|
}
|
2012-08-17 18:42:06 -07:00
|
|
|
else {
|
2016-07-02 15:50:39 -07:00
|
|
|
my $formatted_num = sprintf "%03d", $+{'num'};
|
2016-07-03 03:23:53 -07:00
|
|
|
unless($+{'multi'}) {
|
2016-07-02 15:50:39 -07:00
|
|
|
add_line($formatted_num, $output)
|
2012-08-17 18:42:06 -07:00
|
|
|
}
|
2016-07-02 15:50:39 -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
|
|
|
}
|
|
|
|
}
|
2016-07-02 15:50:39 -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
|
|
|
|
2016-07-02 15:36:54 -07:00
|
|
|
return $output, structured_answer => {
|
2016-07-02 15:50:39 -07:00
|
|
|
id => 'dewey_decimal',
|
|
|
|
name => 'Answer',
|
|
|
|
templates => {
|
|
|
|
group => 'list',
|
|
|
|
options => {
|
|
|
|
content => 'record',
|
|
|
|
moreAt => 0
|
2016-05-31 17:23:35 -07:00
|
|
|
}
|
2016-07-02 15:50:39 -07:00
|
|
|
},
|
|
|
|
data => {
|
|
|
|
title => 'Dewey Decimal System',
|
|
|
|
record_data => $output
|
|
|
|
}
|
|
|
|
};
|
2012-08-05 09:24:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
1;
|