This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Dewey.pm
executable file
·86 lines (76 loc) · 2.22 KB
/
Dewey.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package DDG::Goodie::Dewey;
# ABSTRACT: Identify and find dewey decimal system numbers
use strict;
use DDG::Goodie;
triggers any => 'dewey';
zci answer_type => 'dewey_decimal';
zci is_cached => 1;
my %nums = share('dewey.txt')->slurp;
my %types = reverse %nums;
# get description for a number
sub get_info {
my($num) = @_;
my $desc = $nums{"$num\n"} or return;
chomp $desc;
$desc =~ s/\[\[([^\]]+?)\|(.+?)\]\]/$2/g;
$desc =~ s/\[\[(.+?)\]\]/$1/g;
$desc =~ s/\[\[.+?\|(.+?)\]\]/$1/g;
$desc =~ s/\[\[(.+?)\]\]/$1/g;
return $desc;
}
# add a key-value pair with number and description to $data
sub add_line {
my($num, $data) = @_;
chomp $num;
if(exists($nums{"$num\n"})) {
$data->{$num} = get_info($num) or return;
}
}
handle remainder => sub {
return unless /^(?:the)?\s*(?:decimal)?\s*(?:system)?\s*(?:numbers?|\#)?\s*
(?:
(?<num>\d{1,3})(?:\.\d+)?(?<multi>s)? |
(?<word>[\w\s]+?)
)
\s*(?:in)?\s*(?:the)?\s*(?:decimal)?\s*(?:system)?$/ix;
my $word = $+{'word'};
my $output = {};
if (defined $word) {
return if lc($word) eq 'system';
my @results = grep(/$word/i, keys %types);
return unless @results;
add_line($types{$_}, $output) for @results;
}
else {
my $formatted_num = sprintf "%03d", $+{'num'};
unless($+{'multi'}) {
add_line($formatted_num, $output)
}
elsif ($formatted_num =~ /\d00/) {
for my $x ($formatted_num .. $formatted_num+99) {
add_line($x, $output) or next;
}
}
elsif ($formatted_num =~ /\d\d0/) {
for my $x ($formatted_num .. $formatted_num+9) {
add_line($x, $output) or next;
}
}
}
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
}
};
};
1;