From da11a8d9711396ad8c3ded06753d8f0cbf36bcb9 Mon Sep 17 00:00:00 2001 From: timriker Date: Wed, 20 Mar 2013 18:46:30 +0000 Subject: [PATCH] translate git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk@1930 c11ca15a-4712-0410-83d8-924469b57eb5 --- doc/USAGE | 8 +- files/infobot.help | 10 +-- files/sample/infobot.chan | 2 +- src/CommandStubs.pl | 2 +- src/Modules/babelfish.pl | 157 -------------------------------------- src/Modules/translate.pl | 56 ++++++++++++++ 6 files changed, 67 insertions(+), 168 deletions(-) delete mode 100644 src/Modules/babelfish.pl create mode 100644 src/Modules/translate.pl diff --git a/doc/USAGE b/doc/USAGE index 23786fe..ae6c80b 100644 --- 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: ... diff --git a/files/infobot.help b/files/infobot.help index 8a2019e..89d8a2d 100644 --- a/files/infobot.help +++ b/files/infobot.help @@ -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 -babelfish: U: translate -babelfish: E: x en de your cars rock - bandel: D: FIXME: bandel: U: ## 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 +translate: U: translate +translate: E: x en de your cars rock + uc: D: upper case a given string uc: U: ## uc: E: ## When will infobot achieve world domination? diff --git a/files/sample/infobot.chan b/files/sample/infobot.chan index 07a2811..0d422dc 100644 --- a/files/sample/infobot.chan +++ b/files/sample/infobot.chan @@ -54,7 +54,6 @@ _default addressCharacter ~ +allowConv +allowTelling - +babelfish +botmail +case +cookie @@ -100,6 +99,7 @@ _default +slashdot +spell +tell + +translate +upsidedown +verstats +wikipedia diff --git a/src/CommandStubs.pl b/src/CommandStubs.pl index 4ed20b8..d2d3ecf 100644 --- a/src/CommandStubs.pl +++ b/src/CommandStubs.pl @@ -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 index 920bf2e..0000000 --- a/src/Modules/babelfish.pl +++ /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/.*
]*>//sg; - # clean after first form - $translated =~ s/<\/form>.*//sg; - # convert back to spaces - $translated =~ s/ / /sg; - &::DEBUG("================================\n$translated\n========================\n"); - # strip up to result - $translated =~ s/.*
//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 index 0000000..4853147 --- /dev/null +++ b/src/Modules/translate.pl @@ -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 -- 2.39.2