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

99 lines
2.7 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;
2012-11-06 13:09:19 -08:00
primary_example_queries 'dewey 644';
secondary_example_queries 'etymology in the dewey decimal system', 'dewey decimal system 640s', 'dewey decimal system naturalism';
description 'get the topic or reference number of a Dewey Decimal class';
name 'Dewey';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Dewey.pm';
category 'reference';
topics 'special_interest';
attribution twitter => 'crazedpsyc',
cpan => 'CRZEDPSYC' ;
2012-08-05 09:24:07 -07:00
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
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;
return $desc;
}
sub line {
my($num) = @_;
2012-08-17 18:42:06 -07:00
chomp $num;
return "<td>$num</td><td>&nbsp;".(get_info($num) or return)."</td>";
2012-08-05 09:24:07 -07:00
}
2012-08-17 18:42:06 -07:00
sub single_format {
"$_[0] is $_[1] in the Dewey Decimal System.";
}
2012-08-05 09:24:07 -07:00
handle remainder => sub {
2012-08-17 18:42:06 -07:00
return unless s/^(?:the)?\s*(?:decimal)?\s*(?:system)?\s*(?:numbers?|\#)?\s*
(?:
(\d{1,3})(?:\.\d+)?(s)? |
([\w\s]+?)
)
\s*(?:in)?\s*(?:the)?\s*(?:decimal)?\s*(?:system)?$
/defined $1?$1:$3/eix;
2012-08-05 09:24:07 -07:00
my ($out_html, $out) = ("","");
my $multi = $2;
2012-08-17 18:42:06 -07:00
my $word = $3;
2012-08-05 09:24:07 -07:00
2012-08-17 18:42:06 -07:00
if (defined $word) {
return if lc($word) eq 'system'; # don't respond to "dewey decimal system"
my @results = grep(/$word/i, keys %types);
return unless @results;
2014-10-28 11:36:15 -07:00
if (@results > 1) {
$out_html .= "<tr>".line($types{$_})."</tr>" for @results;
2012-08-17 18:42:06 -07:00
$multi = 1;
} else {
my $num = $types{$results[0]};
chomp $num;
$out .= single_format($num, lc(get_info($num) or return));
$out_html = $out;
2012-08-05 09:24:07 -07:00
}
}
2012-08-17 18:42:06 -07:00
else {
$_ = sprintf "%03d", $_;
2014-10-28 11:36:15 -07:00
unless ($multi) {
2012-08-17 18:42:06 -07:00
$out .= single_format $_, lc((get_info($_) or return));
$out_html = $out;
}
elsif (/\d00/) {
for ($_..$_+99) {
$out_html .= "<tr>" .(line($_) or next). "</tr>";
}
}
elsif (/\d\d0/) {
for ($_..$_+9) {
$out_html .= "<tr>" .(line($_) or next). "</tr>";
}
2012-08-05 09:24:07 -07:00
}
}
2014-10-28 11:36:15 -07:00
2012-08-05 10:19:59 -07:00
$out_html =~ s/\[\[([^\]]+?)\|(.+?)\]\]/<a href="\/\?q=$1">$2<\/a>/g;
$out_html =~ s/\[\[(.+?)\]\]/<a href="\/?q=$1">$1<\/a>/g;
2012-08-05 09:24:07 -07:00
$out =~ s/\[\[.+?\|(.+?)\]\]/$1/g;
$out =~ s/\[\[(.+?)\]\]/$1/g;
return ($multi) ? "" : $out, html => ($multi) ? "<table cellpadding=1>$out_html</table>" : $out_html;
2012-08-05 09:24:07 -07:00
};
1;