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
/
Roman.pm
executable file
·86 lines (75 loc) · 2.64 KB
/
Roman.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::Roman;
# ABSTRACT: Convert between Roman and Arabic numeral systems.
use strict;
use DDG::Goodie;
use Roman;
use List::Util qw/any/;
use utf8;
triggers startend => "roman", "roman numeral", "roman numerals", "roman number",
"arabic", "arabic numeral", "arabic numerals", "arabic number";
zci is_cached => 1;
zci answer_type => "roman_numeral_conversion";
# These two lists are used to load the converter without any answer.
my @roman_to_arabic = (
qr/^convert\s+(?:into|to)\s+arabic\s*(numerals?)?$/i
);
my @arabic_to_roman = (
qr/^convert\s+(?:into|to)\s+roman\s*(numerals?)?$/i
);
# These two lists are used to load the converter with an answer.
my @roman_number_to_arabic = (
qr/^convert\s+(\D+)\s+(?:into|in|to)\s*arabic\s*(numerals?)?$/i,
qr/^roman\s+(?:numerals?)?\s*(\D+)$/i,
qr/^arabic\s+(?:numerals?)?\s*(\D+)$/i,
qr/^([ivxlcdm]+)\s+(?:into|in|to)?\s+arabic\s*(numerals?)?$/i
);
my @arabic_number_to_roman = (
qr/^convert\s+(\d+)\s+(?:into|in|to)\s*roman\s*(numerals?)?$/i,
qr/^roman\s+(?:numerals?)?\s*(\d+)$/i,
qr/^arabic\s+(?:numerals?)?\s*(\d+)$/i,
qr/^(\d+)\s+(?:into|in|to)?\s+roman\s*(numerals?)?$/i
);
handle query => sub {
my ($query) = @_;
# By default, we convert from roman to arabic.
my $input = 'roman';
my $input_value = '';
my $output = 'arabic';
my $output_value = '';
if (any { $query =~ $_ } @arabic_to_roman) {
$input = 'arabic';
$output = 'roman';
} elsif (any { ($input_value) = $query =~ $_ } @roman_number_to_arabic) {
if (isroman $input_value) {
$input_value = uc $input_value;
$output_value = arabic $input_value;
} else {
# Malformed input (not a roman number), we don't trigger the ia.
return;
}
} elsif (any { ($input_value) = $query =~ $_ } @arabic_number_to_roman) {
$input = 'arabic';
$output = 'roman';
$input_value = $input_value;
$output_value = Roman $input_value;
} else {
$input_value = ''; # because of previous assignments with undef.
return unless any { $query =~ $_ } @roman_to_arabic;
}
return 'roman numeral converter', structured_answer => {
data => {
subtitle => 'Accepts inputs from 1 - 3999, I - MMMCMXCIX',
input => $input,
input_value => $input_value,
output => $output,
output_value => $output_value
},
templates => {
group => 'text',
options => {
title_content => 'DDH.roman.content',
}
}
};
};
1;