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

Commit 2287be9

Browse files
tossjmintsoft
authored andcommitted
TextConverter: Sorted static trigger words into to and from categories (#4490)
* TextConverter: Sorted static trigger words into to and from categories * TextConverter: Moved to and from trigger words out of handle
1 parent 6b16ccd commit 2287be9

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/DDG/Goodie/TextConverter.pm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ my @merged_triggers = (@single_triggers, @triggers);
3535
my $triggers_re = join "|", @merged_triggers;
3636
my $generics_re = join "|", @generics;
3737

38+
# for static language based triggers e.g. 'base64 decode'
39+
# these words mean we want to go from the type in the query to text
40+
my @from_words = ('decoder', 'decode', 'converter', 'translator');
41+
my $from_words_re = join '|', @from_words;
42+
# these words mean we want to go from text to the type in the query
43+
my @to_words = ('encode', 'encoder', 'translation', 'translate', 'convert', 'conversion');
44+
my $to_words_re = join '|', @to_words;
45+
3846
for my $trig (@triggers) {
3947
push @lang_triggers, map { "$trig $_" } @generics;
4048
}
@@ -89,7 +97,11 @@ handle query_lc => sub {
8997
# check to see if query is a static language based trigger
9098
# eg. binary converter, hex encoder
9199
if(grep(/^$query$/, @lang_triggers)) {
92-
$to_type = get_type_information($query);
100+
if ($query =~ /${from_words_re}/gi) {
101+
$from_type = get_type_information($query);
102+
} elsif ($query =~ /${to_words_re}/gi) {
103+
$to_type = get_type_information($query);
104+
}
93105

94106
return '',
95107
structured_answer => {

t/TextConverter.t

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ ddg_goodie_test(
5050

5151
'binary converter' => test_zci(
5252
'', structured_answer => build_structured_answer({
53-
from_type => '',
54-
to_type => 'binary'
53+
from_type => 'binary',
54+
to_type => ''
5555
})
5656
),
5757

5858
'hex converter' => test_zci(
5959
'', structured_answer => build_structured_answer({
60-
from_type => '',
61-
to_type => 'hexadecimal'
60+
from_type => 'hexadecimal',
61+
to_type => ''
6262
})
6363
),
6464

6565
'ascii converter' => test_zci(
6666
'', structured_answer => build_structured_answer({
67-
from_type => '',
68-
to_type => 'text'
67+
from_type => 'text',
68+
to_type => ''
6969
})
7070
),
7171

@@ -83,6 +83,27 @@ ddg_goodie_test(
8383
})
8484
),
8585

86+
'base64 decoder' => test_zci(
87+
'', structured_answer => build_structured_answer({
88+
from_type => 'base64',
89+
to_type => ''
90+
})
91+
),
92+
93+
'hex translator' => test_zci(
94+
'', structured_answer => build_structured_answer({
95+
from_type => 'hexadecimal',
96+
to_type => ''
97+
})
98+
),
99+
100+
'binary translation' => test_zci(
101+
'', structured_answer => build_structured_answer({
102+
from_type => '',
103+
to_type => 'binary'
104+
})
105+
),
106+
86107
##
87108
## 2. LANGUAGE BASED QUERIES
88109
##

0 commit comments

Comments
 (0)