]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/babelfish.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / Modules / babelfish.pl
1 # This program is copyright Jonathan Feinberg 1999.
2 # This program is distributed under the same terms as infobot.
3
4 # Jonathan Feinberg
5 # jdf@pobox.com
6 # http://pobox.com/~jdf/
7
8 # Version 1.0
9 # First public release.
10
11 # hacked by Tim@Rikers.org to handle new URL and layout
12
13 package babelfish;
14 use strict;
15
16 my $no_babelfish;
17 my $url = 'http://babelfish.av.com/tr';
18
19 BEGIN {
20     eval "use URI::Escape";    # utility functions for encoding the
21     if ($@) { $no_babelfish++};    # babelfish request
22     eval "use LWP::UserAgent";
23     if ($@) { $no_babelfish++};
24 }
25
26 BEGIN {
27   # Translate some feasible abbreviations into the ones babelfish
28   # expects.
29     use vars qw!%lang_code $lang_regex!;
30     %lang_code = (
31                 'de' => 'de',
32                 'ge' => 'de',
33                 'gr' => 'el',
34                 'el' => 'el',
35                 'sp' => 'es',
36                 'es' => 'es',
37                 'en' => 'en',
38                 'fr' => 'fr',
39                 'it' => 'it',
40                 'ja' => 'ja',
41                 'jp' => 'ja',
42                 'ko' => 'ko',
43                 'kr' => 'ko',
44                 'nl' => 'nl',
45                 'po' => 'pt',
46                 'pt' => 'pt',
47                 'ru' => 'ru',
48                 'zh' => 'zh',
49                 'zt' => 'zt'
50                );
51
52   # Here's how we recognize the language you're asking for.  It looks
53   # like RTSL saves you a few keystrokes in #perl, huh?
54   $lang_regex = join '|', keys %lang_code;
55 }
56
57 sub babelfishParam {
58     return '' if $no_babelfish;
59   my ($from, $to, $phrase) = @_;
60   &::DEBUG("babelfish($from, $to, $phrase)");
61
62   $from = $lang_code{$from};
63   $to = $lang_code{$to};
64
65   my $ua = new LWP::UserAgent;
66   $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam('httpProxy'));
67   # Let's pretend
68   $ua->agent("Mozilla/5.0 " . $ua->agent);
69   $ua->timeout(5);
70
71   my $req = HTTP::Request->new('POST', $url);
72
73 # babelfish ignored this, but it SHOULD work
74 # Accept-Charset: iso-8859-1
75 #  $req->header('Accept-Charset' => 'iso-8859-1');
76 #  print $req->header('Accept-Charset');
77   $req->header('Accept-Language' => 'en');
78   $req->content_type('application/x-www-form-urlencoded');
79
80   return translate($phrase, "${from}_${to}", $req, $ua);
81 }
82
83 sub translate {
84     return '' if $no_babelfish;
85   my ($phrase, $languagepair, $req, $ua) = @_;
86   &::DEBUG("translate($phrase, $languagepair, $req, $ua)");
87
88   my $trtext = uri_escape($phrase);
89   $req->content("trtext=$trtext&lp=$languagepair");
90   &::DEBUG("$url??trtext=$trtext&lp=$languagepair");
91
92   my $res = $ua->request($req);
93   my $translated;
94
95   if ($res->is_success) {
96     my $html = $res->content;
97     # This method subject to change with the whims of Altavista's design
98     # staff.
99     ($translated) = $html;
100
101     $translated =~ s/<[^>]*>//sg;
102     $translated =~ s/&nbsp;/ /sg;
103     $translated =~ s/\s+/ /sg;
104     #&::DEBUG("$translated\n===remove <attributes>\n");
105
106     $translated =~ s/\s*Translate again.*//i;
107     &::DEBUG("$translated\n===remove after 'Translate again'\n");
108
109     $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
110     &::DEBUG("len=" . length($translated) . " $translated\n===remove to first ':', optional Help\n");
111
112     $translated =~ s/\n/ /g;
113     # FIXME: should we do unicode->iso (no. use utf8!)
114   } else {
115     $translated = ":("; # failure
116   }
117   $translated = "babelfish.pl: result too long, probably an error" if (length($translated) > 700);
118
119   return $translated
120 }
121
122 sub babelfish {
123   my ($message) = @_;
124   my $babel_lang_regex = "de|ge|gr|el|sp|es|en|fr|it|ja|jp|ko|kr|nl|po|pt|ru|zh|zt";
125   if ($message =~ m{
126     ($babel_lang_regex)\w*      # from language?
127     \s+
128     ($babel_lang_regex)\w*      # to language?
129     \s*
130     (.+)                        # The phrase to be translated
131   }xoi) {
132     &::performStrictReply(&babelfishParam(lc $1, lc $2, lc $3));
133   }
134   return;
135 }
136
137 if (0) {
138     if (-t STDIN) {
139         #my $result = babelfish::babelfish('en sp hello world');
140         #my $result = babelfish::babelfish('en sp The cheese is old and moldy, where is the bathroom?');
141         my $result = babelfish::babelfish('en gr doesn\'t seem to translate things longer than 40 characters');
142         $result =~ s/; /\n/g;
143         print "Babelfish says: \"$result\"\n";
144     }
145 }
146
147 1;
148
149 # vim:ts=4:sw=4:expandtab:tw=80