<commit_before>package DDG::Spice::Translate::Detect;

use DDG::Spice;
use Moo;

with('DDG::SpiceRole::Translate');

attribution github  => ['https://github.com/ghedo', 'ghedo'      ],
            web     => ['http://ghedini.me', '<NAME>'];

spice to   => 'http://ws.detectlanguage.com/0.2/detect?q=$1&key={{ENV{DDG_SPICE_DETECTLANGUAGE_APIKEY}}}';
spice wrap_jsonp_callback => 1;

triggers start => "translate";

handle query_lc => sub {
    my $query = $_;

    ##TODO:
    # Find a better way of getting the $lang->local to javascript
    # Currently we parse the API Call script tag and grab the last param
    # https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/share/spice/translate/detect/spice.js#L84

    # Don't need to detect when "from" language is given
    # return if ($query =~ (/from (?:$langs)/));

    $query =~ s/\s+/ /; #merge multiple spaces

    # NEED TO MATCH UNICODE!
    if ($query =~ /^translate (\S+)$/ ) {
        my $phrase = $1;
        # NEED TO ENCODE UNICODE!
        return $phrase;

    # NEED TO MATCH UNICODE!
    } elsif ($query =~ /^translate (\S+) to ($langs)$/) {
        my ($phrase, $to) = ($1, $2);
        # NEED TO ENCODE UNICODE!
        return $phrase;

    } else {
        return;
    }

};

1;<commit_msg>Translate: Revert back to e0246a4e3f8ad782c0e509cc50350b90860c7d25.<commit_after>package DDG::Spice::Translate::Detect;

use DDG::Spice;
use Moo;

with('DDG::SpiceRole::Translate');

attribution github  => ['https://github.com/ghedo', 'ghedo'      ],
            web     => ['http://ghedini.me', '<NAME>'];

my $langs = 'arabic|ar|chinese|zh|czech|cz|english|en|french|fr|greek|gr|italian|it|japanese|ja|korean|ko|polish|pl|portuguese|pt|romanian|ro|spanish|es|turkish|tr';

spice to   => 'http://ws.detectlanguage.com/0.2/detect?q=$1&key={{ENV{DDG_SPICE_DETECTLANGUAGE_APIKEY}}}';
spice from => '(.+)\/(.+)';
spice wrap_jsonp_callback => 1;

triggers start => "translate";

handle query_lc => sub {
    my $query = $_;

    ##TODO:
    # Find a better way of getting the $lang->local to javascript
    # Currently we parse the API Call script tag and grab the last param
    # https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/share/spice/translate/detect/spice.js#L84

    # Don't need to detect when "from" language is given
    # return if ($query =~ (/from (?:$langs)/));

    $query =~ s/\s+/ /; #merge multiple spaces

    # NEED TO MATCH UNICODE!
    if ($query =~ /^translate (\S+)$/ ) {
        my $phrase = $1;
        # NEED TO ENCODE UNICODE!
        return ($phrase, substr($lang->locale, 0, 2));

    # NEED TO MATCH UNICODE!
    } elsif ($query =~ /^translate (\S+) to ($langs)$/) {
        my ($phrase, $to) = ($1, $2);
        # NEED TO ENCODE UNICODE!
        return ($phrase, shorten_lang($to));

    } else {
        return;
    }

};

1;
{'fraction_non_alphanumeric': 0.17423442449841606, 'fraction_numerical': 0.017247448081661388, 'mean_word_length': 3.0716332378223496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2626811', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1035, 'n_words': 238}