Skip to content
This repository was archived by the owner on Oct 15, 2022. It is now read-only.

Commit 549ee53

Browse files
gargamlmoollaza
authored andcommitted
Roman numerals (#4413)
* First shot at improving roman numeral IA. Added a new converter. Added new default settings. Added some tests. * Added un upper bound on arabic numbers. * Remove some typo… * Added some new triggers. Allowed uncorrect roman number to load the UI anyway. Fixed a test. * Added new triggers. Fixed regular expressions involved in the UI configuration. Added new tests. * Renamed file. * Variables defined outside the handle function. * Modified template. * Init logic now runs only once. * Added a prefix to ensure there are no collisions. * Fixed tests. * Added more agressive validation on user inputs. * Fixed a test case for aggressive validation. The ia doesn't trigger is the input is invalid with respect to roman numeral system. * Removed forEach operation (replaced with a for loop). Removed firstLetterUpperCase in favor of DDG util capitalize. * Fixed design on narrow screens. * Added a new test (roman man => undef). * Fixed regular expressions to avoid displaying if roman or arabic is used without numbers. * Added warnings when inputs are out of range. * Added a state variable for warnings. * Empty if removed. * Fixed regexps. * Fixed subtitle. * Css fixed. Both for mobile and desktop. * Fixed tests. * Use horizontal layout on desktop, vertical on mobile
1 parent 2287be9 commit 549ee53

File tree

5 files changed

+480
-33
lines changed

5 files changed

+480
-33
lines changed

lib/DDG/Goodie/Roman.pm

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,80 @@ use strict;
55
use DDG::Goodie;
66

77
use Roman;
8+
use List::Util qw/any/;
89
use utf8;
910

10-
triggers any => "roman", "arabic";
11+
triggers startend => "roman", "roman numeral", "roman numerals", "roman number",
12+
"arabic", "arabic numeral", "arabic numerals", "arabic number";
1113

1214
zci is_cached => 1;
1315
zci answer_type => "roman_numeral_conversion";
1416

15-
handle remainder => sub {
16-
my $in = uc shift;
17-
$in =~ s/(?:\s*|in|to|numerals?|number)//gi;
17+
# These two lists are used to load the converter without any answer.
18+
my @roman_to_arabic = (
19+
qr/^convert\s+(?:into|to)\s+arabic\s*(numerals?)?$/i
20+
);
21+
my @arabic_to_roman = (
22+
qr/^convert\s+(?:into|to)\s+roman\s*(numerals?)?$/i
23+
);
24+
25+
# These two lists are used to load the converter with an answer.
26+
my @roman_number_to_arabic = (
27+
qr/^convert\s+(\D+)\s+(?:into|in|to)\s*arabic\s*(numerals?)?$/i,
28+
qr/^roman\s+(?:numerals?)?\s*(\D+)$/i,
29+
qr/^arabic\s+(?:numerals?)?\s*(\D+)$/i,
30+
qr/^([ivxlcdm]+)\s+(?:into|in|to)?\s+arabic\s*(numerals?)?$/i
31+
);
32+
my @arabic_number_to_roman = (
33+
qr/^convert\s+(\d+)\s+(?:into|in|to)\s*roman\s*(numerals?)?$/i,
34+
qr/^roman\s+(?:numerals?)?\s*(\d+)$/i,
35+
qr/^arabic\s+(?:numerals?)?\s*(\d+)$/i,
36+
qr/^(\d+)\s+(?:into|in|to)?\s+roman\s*(numerals?)?$/i
37+
);
1838

19-
return unless $in;
39+
handle query => sub {
40+
my ($query) = @_;
2041

21-
my $out;
22-
if ($in =~ /^\d+$/) {
23-
$out = uc(roman($in));
24-
} elsif ($in =~ /^[mdclxvi]+$/i) {
25-
$in = uc($in);
26-
$out = arabic($in);
42+
# By default, we convert from roman to arabic.
43+
my $input = 'roman';
44+
my $input_value = '';
45+
my $output = 'arabic';
46+
my $output_value = '';
47+
48+
if (any { $query =~ $_ } @arabic_to_roman) {
49+
$input = 'arabic';
50+
$output = 'roman';
51+
} elsif (any { ($input_value) = $query =~ $_ } @roman_number_to_arabic) {
52+
if (isroman $input_value) {
53+
$input_value = uc $input_value;
54+
$output_value = arabic $input_value;
55+
} else {
56+
# Malformed input (not a roman number), we don't trigger the ia.
57+
return;
58+
}
59+
} elsif (any { ($input_value) = $query =~ $_ } @arabic_number_to_roman) {
60+
$input = 'arabic';
61+
$output = 'roman';
62+
$input_value = $input_value;
63+
$output_value = Roman $input_value;
64+
} else {
65+
$input_value = ''; # because of previous assignments with undef.
66+
return unless any { $query =~ $_ } @roman_to_arabic;
2767
}
28-
return unless $out;
2968

30-
return $out . ' (roman numeral conversion)', structured_answer => {
69+
return 'roman numeral converter', structured_answer => {
3170
data => {
32-
title => $out,
33-
subtitle => "Roman numeral conversion: $in"
71+
subtitle => 'Accepts inputs from 1 - 3999, I - MMMCMXCIX',
72+
input => $input,
73+
input_value => $input_value,
74+
output => $output,
75+
output_value => $output_value
3476
},
3577
templates => {
36-
group => 'text'
78+
group => 'text',
79+
options => {
80+
title_content => 'DDH.roman.content',
81+
}
3782
}
3883
};
3984
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<form class="converter">
2+
<div class="converter__input">
3+
<label class="converter__input__label">Roman</label>
4+
<input type="text" class="converter__input__field" />
5+
</div>
6+
<div class="converter__output">
7+
<label class="converter__output__label">Arabic</label>
8+
<input type="text" class="converter__output__field" />
9+
</div>
10+
</form>

share/goodie/roman/roman.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.zci--roman .converter {
2+
width: 100%;
3+
}
4+
5+
.zci--roman .converter__input,
6+
.zci--roman .converter__output {
7+
display: inline-block;
8+
margin-bottom: 1em;
9+
width: 45%;
10+
}
11+
12+
.zci--roman .converter__input {
13+
margin-right: 1em;
14+
}
15+
16+
.zci--roman .converter__input__label,
17+
.zci--roman .converter__output__label {
18+
display: block;
19+
text-align: left;
20+
font-size: 1.5em;
21+
}
22+
23+
.zci--roman .converter__input__field,
24+
.zci--roman .converter__output__field {
25+
width: 100%;
26+
text-align: center;
27+
font-size: 2em;
28+
}
29+
30+
.is-mobile .zci--roman .converter__input,
31+
.is-mobile .zci--roman .converter__output {
32+
display: block;
33+
width: 100%;
34+
}
35+
36+
/* Mobile UI */
37+
.is-mobile .zci--roman .converter__input {
38+
margin-right: 0em;
39+
}
40+
41+
.is-mobile .zci--roman .converter__output {
42+
margin-top: 1em;
43+
}

0 commit comments

Comments
 (0)