]> git.donarmstrong.com Git - infobot.git/commitdiff
translate
authortimriker <timriker@c11ca15a-4712-0410-83d8-924469b57eb5>
Wed, 20 Mar 2013 18:46:30 +0000 (18:46 +0000)
committertimriker <timriker@c11ca15a-4712-0410-83d8-924469b57eb5>
Wed, 20 Mar 2013 18:46:30 +0000 (18:46 +0000)
git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk@1930 c11ca15a-4712-0410-83d8-924469b57eb5

doc/USAGE
files/infobot.help
files/sample/infobot.chan
src/CommandStubs.pl
src/Modules/babelfish.pl [deleted file]
src/Modules/translate.pl [new file with mode: 0644]

index 23786fee9982bc3d994f4417eb0e36382eca6a2d..ae6c80bf9d1a4560aafb2f9baf6c20bb9851373e 100644 (file)
--- a/doc/USAGE
+++ b/doc/USAGE
@@ -438,13 +438,13 @@ Example:
                                MODULE COMMANDS
                     ======================================
 
-Command: babelfish
+Command: countdown
 =============
 Description:
        ...
 
 Usage:
-       x from [language]: phrase
+       countdown ...
 
 Example:
        ...
@@ -642,13 +642,13 @@ Example:
        ...
 
 
-Command: countdown
+Command: translate
 =============
 Description:
        ...
 
 Usage:
-       countdown ...
+       x from [language]: phrase
 
 Example:
        ...
index 8a2019ec65c7e333ec2282f4ceb29ff82f585338..89d8a2d776a65b0db2d93b5a2f2e61cc65871980 100644 (file)
@@ -14,11 +14,6 @@ alternation: The || symbol in an entry causes an infobot to choose one of the re
 
 author: oznoid (mailto:lenzo@ri.cmu.edu) is my original author.
 
-babelfish: D: Frontend to babelfish translating service provided by http://babelfish.altavista.com/ Note that utf8 is used for non-ascii characters.
-babelfish: U: x <fromLang> <toLang> <words>
-babelfish: U: translate <fromLang> <toLang> <words>
-babelfish: E: x en de your cars rock
-
 bandel: D: FIXME:
 bandel: U: ## <mask|user>
 bandel: E: ## *!*@owns.org
@@ -476,6 +471,11 @@ topic: NOTE: #chan arg is only required if command is sent over private message
 topic: NOTE: commands can be preceeded? with '-' in order not to enforce changes to topic.
 topic: End of help.
 
+translate: D: Frontend to Google Translate. Note that utf8 is used for non-ascii characters.
+translate: U: x <fromLang> <toLang> <words>
+translate: U: translate <fromLang> <toLang> <words>
+translate: E: x en de your cars rock
+
 uc: D: upper case a given string
 uc: U: ## <string>
 uc: E: ## When will infobot achieve world domination?
index 07a2811ca327f307364cc0fcb9f5137661d4798c..0d422dc15309d37e40a674fd1fa0a04244821a24 100644 (file)
@@ -54,7 +54,6 @@ _default
     addressCharacter ~
     +allowConv
     +allowTelling
-    +babelfish
     +botmail
     +case
     +cookie
@@ -100,6 +99,7 @@ _default
     +slashdot
     +spell
     +tell
+    +translate
     +upsidedown
     +verstats
     +wikipedia
index 4ed20b82171137ac3eaf14d0b9a3eb447e568847..d2d3ecf2fe267eb9c662d9ea176ff85e51f98746 100644 (file)
@@ -785,7 +785,7 @@ sub do_text_counters {
 ###
 ### START ADDING HOOKS.
 ###
-&addCmdHook('(babel(fish)?|x|xlate|translate)', ('CODEREF' => 'babelfish::babelfish', 'Identifier' => 'babelfish', 'Cmdstats' => 'babelfish', 'Forker' => 1, 'Help' => 'babelfish', 'Module' => 'babelfish') );
+&addCmdHook('(x|xlate|translate)', ('CODEREF' => 'translate::translate', 'Identifier' => 'translate', 'Cmdstats' => 'translate', 'Forker' => 1, 'Help' => 'translate', 'Module' => 'translate') );
 &addCmdHook('(botmail|message)', ('CODEREF' => 'botmail::parse', 'Identifier' => 'botmail', 'Cmdstats' => 'botmail') );
 &addCmdHook('bzflist17', ('CODEREF' => 'BZFlag::list17', 'Identifier' => 'BZFlag', 'Cmdstats' => 'BZFlag', 'Forker' => 1, 'Module' => 'BZFlag') );
 &addCmdHook('bzflist', ('CODEREF' => 'BZFlag::list', 'Identifier' => 'BZFlag', 'Cmdstats' => 'BZFlag', 'Forker' => 1, 'Module' => 'BZFlag') );
diff --git a/src/Modules/babelfish.pl b/src/Modules/babelfish.pl
deleted file mode 100644 (file)
index 920bf2e..0000000
+++ /dev/null
@@ -1,157 +0,0 @@
-# This program is copyright Jonathan Feinberg 1999.
-# This program is distributed under the same terms as infobot.
-
-# Jonathan Feinberg
-# jdf@pobox.com
-# http://pobox.com/~jdf/
-
-# Version 1.0
-# First public release.
-
-# hacked by Tim@Rikers.org to handle new URL and layout
-
-package babelfish;
-use strict;
-
-my $no_babelfish;
-#my $url = 'http://babelfish.av.com/tr';
-my $url = 'http://babelfish.yahoo.com/translate_txt';
-
-BEGIN {
-    eval "use URI::Escape";    # utility functions for encoding the
-    if ($@) { $no_babelfish++ }
-    ;                          # babelfish request
-    eval "use LWP::UserAgent";
-    if ($@) { $no_babelfish++ }
-}
-
-BEGIN {
-
-    # Translate some feasible abbreviations into the ones babelfish
-    # expects.
-    use vars qw!%lang_code $lang_regex!;
-    %lang_code = (
-        'de' => 'de',
-        'ge' => 'de',
-        'gr' => 'el',
-        'el' => 'el',
-        'sp' => 'es',
-        'es' => 'es',
-        'en' => 'en',
-        'fr' => 'fr',
-        'it' => 'it',
-        'ja' => 'ja',
-        'jp' => 'ja',
-        'ko' => 'ko',
-        'kr' => 'ko',
-        'nl' => 'nl',
-        'po' => 'pt',
-        'pt' => 'pt',
-        'ru' => 'ru',
-        'zh' => 'zh',
-        'zt' => 'zt'
-    );
-
-    # Here's how we recognize the language you're asking for.  It looks
-    # like RTSL saves you a few keystrokes in #perl, huh?
-    $lang_regex = join '|', keys %lang_code;
-}
-
-sub babelfishParam {
-    return '' if $no_babelfish;
-    my ( $from, $to, $phrase ) = @_;
-    &::DEBUG("babelfish($from, $to, $phrase)");
-
-    $from = $lang_code{$from};
-    $to   = $lang_code{$to};
-
-    my $ua = new LWP::UserAgent;
-    $ua->proxy( 'http', $::param{'httpProxy'} ) if ( &::IsParam('httpProxy') );
-
-    # Let's pretend
-    $ua->agent( "Mozilla/5.0 " . $ua->agent );
-    $ua->timeout(5);
-
-    my $req = HTTP::Request->new( 'POST', $url );
-
-    $req->header('Accept-Language' => 'en-us');
-    $req->header('Accept-Charset' => 'UTF-8,*');
-    $req->content_type('application/x-www-form-urlencoded');
-
-    return translate( $phrase, "${from}_${to}", $req, $ua );
-}
-
-sub translate {
-    return '' if $no_babelfish;
-    my ( $phrase, $languagepair, $req, $ua ) = @_;
-    &::DEBUG("translate($phrase, $languagepair, $req, $ua)");
-
-    my $trtext = uri_escape($phrase);
-    $req->content("trtext=$trtext&lp=$languagepair");
-    &::DEBUG("$url??trtext=$trtext&lp=$languagepair");
-
-    my $res = $ua->request($req);
-    my $translated;
-
-    if ( $res->is_success ) {
-        my $html = $res->content;
-
-        # This method subject to change with the whims of Babelfish design staff.
-        ($translated) = $html;
-        # strip page head
-        $translated =~ s/.*<\/head>//sg;
-        # clean before doc-body
-        $translated =~ s/.*<div id="doc-body"[^>]*>//sg;
-        # clean after first form
-        $translated =~ s/<\/form>.*//sg;
-        # convert back to spaces
-        $translated =~ s/&nbsp;/ /sg;
-        &::DEBUG("================================\n$translated\n========================\n");
-        # strip up to result
-        $translated =~ s/.*<div id="result">//sg;
-        # strip rest of page
-        $translated =~ s/<\/div.*//sg;
-        # strip all markup
-        $translated =~ s/<[^>]*>/ /sg;
-        # \n to space
-        $translated =~ s/[\n\r\t]/ /g;
-        # strip leading whitespace
-        $translated =~ s/^\s+//sg;
-        # strip trailing whitespace
-        $translated =~ s/\s+$//sg;
-        # strip multiple whitespace
-        $translated =~ s/\s+/ /sg;
-
-        # FIXME: any entities to utf8?
-    }
-    else {
-        $translated = ":(";    # failure
-    }
-    $translated = "babelfish.pl: result too long, probably an error"
-      if ( length($translated) > 700 );
-
-    return $translated;
-}
-
-sub babelfish {
-    my ($message) = @_;
-    my $babel_lang_regex =
-      "de|ge|gr|el|sp|es|en|fr|it|ja|jp|ko|kr|nl|po|pt|ru|zh|zt";
-    if (
-        $message =~ m{
-    ($babel_lang_regex)\w*     # from language?
-    \s+
-    ($babel_lang_regex)\w*     # to language?
-    \s*
-    (.+)                       # The phrase to be translated
-  }xoi
-      )
-    {
-        &::performStrictReply( &babelfishParam( lc $1, lc $2, lc $3 ) );
-    }
-    return;
-}
-
-1;
-
-# vim:ts=4:sw=4:expandtab:tw=80
diff --git a/src/Modules/translate.pl b/src/Modules/translate.pl
new file mode 100644 (file)
index 0000000..4853147
--- /dev/null
@@ -0,0 +1,56 @@
+# This program is distributed under the same terms as infobot.
+# hacked by Tim@Rikers.org
+
+package translate;
+use strict;
+use Data::Dumper;
+
+my $no_translate;
+my $url = 'http://translate.google.com/translate_a/t';
+
+BEGIN {
+       eval "use URI::Escape"; # utility functions for encoding the
+       if ($@) { $no_translate++ }
+       eval "use LWP::UserAgent";
+       if ($@) { $no_translate++ }
+       eval "use JSON";
+       if ($@) { $no_translate++ }
+}
+
+sub translateParam {
+       return '' if $no_translate;
+       my ( $from, $to, $phrase ) = @_;
+       &::DEBUG("translate($from, $to, $phrase)");
+
+       my $ua = new LWP::UserAgent;
+       $ua->proxy( 'http', $::param{'httpProxy'} ) if ( &::IsParam('httpProxy') );
+
+       # Let's pretend
+       $ua->agent( "Mozilla/5.0 " . $ua->agent );
+       $ua->timeout(5);
+
+       my $req = HTTP::Request->new('GET', 'http://translate.google.com/translate_a/t?client=t&&sl='.$from.'&tl='.$to.'&text='.$phrase);
+
+       $req->header('Accept-Language' => 'en-us');
+       $req->header('Accept-Charset' => 'UTF-8,*');
+       my $json = JSON->new->utf8;
+       my $json_text=$ua->request($req)->content;
+       $json_text =~ s/,,/,"",/g;
+       $json_text =~ s/,,/,"",/g;
+       #&::DEBUG($json_text);
+       my @decoded_json = from_json($json_text);
+
+       return $decoded_json[0][0][0][0];
+}
+
+sub translate {
+       my ($message) = @_;
+       if ($message =~ m{(\S*)\s+(\S*)\s+(.+)}xoi) {
+               &::performStrictReply( &translateParam( lc $1, lc $2, lc $3 ) );
+       }
+       return;
+}
+
+1;
+
+# vim:ts=4:sw=4:expandtab:tw=80