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

Commit

Permalink
TextConverter: Sorted static trigger words into to and from categories (
Browse files Browse the repository at this point in the history
#4490)

* TextConverter: Sorted static trigger words into to and from categories

* TextConverter: Moved to and from trigger words out of handle
  • Loading branch information
tossj authored and mintsoft committed Sep 17, 2017
1 parent 6b16ccd commit 2287be9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
14 changes: 13 additions & 1 deletion lib/DDG/Goodie/TextConverter.pm
Expand Up @@ -35,6 +35,14 @@ my @merged_triggers = (@single_triggers, @triggers);
my $triggers_re = join "|", @merged_triggers;
my $generics_re = join "|", @generics;

# for static language based triggers e.g. 'base64 decode'
# these words mean we want to go from the type in the query to text
my @from_words = ('decoder', 'decode', 'converter', 'translator');
my $from_words_re = join '|', @from_words;
# these words mean we want to go from text to the type in the query
my @to_words = ('encode', 'encoder', 'translation', 'translate', 'convert', 'conversion');
my $to_words_re = join '|', @to_words;

for my $trig (@triggers) {
push @lang_triggers, map { "$trig $_" } @generics;
}
Expand Down Expand Up @@ -89,7 +97,11 @@ handle query_lc => sub {
# check to see if query is a static language based trigger
# eg. binary converter, hex encoder
if(grep(/^$query$/, @lang_triggers)) {
$to_type = get_type_information($query);
if ($query =~ /${from_words_re}/gi) {
$from_type = get_type_information($query);
} elsif ($query =~ /${to_words_re}/gi) {
$to_type = get_type_information($query);
}

return '',
structured_answer => {
Expand Down
33 changes: 27 additions & 6 deletions t/TextConverter.t
Expand Up @@ -50,22 +50,22 @@ ddg_goodie_test(

'binary converter' => test_zci(
'', structured_answer => build_structured_answer({
from_type => '',
to_type => 'binary'
from_type => 'binary',
to_type => ''
})
),

'hex converter' => test_zci(
'', structured_answer => build_structured_answer({
from_type => '',
to_type => 'hexadecimal'
from_type => 'hexadecimal',
to_type => ''
})
),

'ascii converter' => test_zci(
'', structured_answer => build_structured_answer({
from_type => '',
to_type => 'text'
from_type => 'text',
to_type => ''
})
),

Expand All @@ -83,6 +83,27 @@ ddg_goodie_test(
})
),

'base64 decoder' => test_zci(
'', structured_answer => build_structured_answer({
from_type => 'base64',
to_type => ''
})
),

'hex translator' => test_zci(
'', structured_answer => build_structured_answer({
from_type => 'hexadecimal',
to_type => ''
})
),

'binary translation' => test_zci(
'', structured_answer => build_structured_answer({
from_type => '',
to_type => 'binary'
})
),

##
## 2. LANGUAGE BASED QUERIES
##
Expand Down

0 comments on commit 2287be9

Please sign in to comment.