1 # This program is copyright Jonathan Feinberg 1999.
2 # This program is distributed under the same terms as infobot.
6 # http://pobox.com/~jdf/
9 # First public release.
15 # Translate some feasible abbreviations into the ones babelfish
17 use vars qw!%lang_code $lang_regex!;
30 # Here's how we recognize the language you're asking for. It looks
31 # like RTSL saves you a few keystrokes in #perl, huh?
32 $lang_regex = join '|', keys %lang_code;
36 my ($direction, $lang, $phrase) = @_;
38 return unless &::loadPerlModule("URI::Escape");
39 return unless &::loadPerlModule("LWP::UserAgent");
41 $lang = $lang_code{$lang};
43 my $ua = new LWP::UserAgent;
45 $ua->proxy('http', $::param{'httpProxy'}) if &::IsParam("httpProxy");
47 my $url = 'http://babelfish.altavista.com/raging/translate.dyn';
48 my $req = HTTP::Request->new('POST',$url);
50 $req->content_type('application/x-www-form-urlencoded');
52 my $tolang = "en_$lang";
53 my $toenglish = "${lang}_en";
55 if ($direction eq 'to') {
56 my $xlate = translate($phrase, $tolang, $req, $ua);
57 &::pSReply($xlate) if ($xlate);
59 } elsif ($direction eq 'from') {
60 my $xlate = translate($phrase, $toenglish, $req, $ua);
61 &::pSReply($xlate) if ($xlate);
64 &::DEBUG("what's this junk?");
66 my $last_english = $phrase;
71 last if $results{$phrase}++; # REMOVE!
72 $last_lang = $phrase = translate($phrase, $tolang, $req, $ua);
73 last if $results{$phrase}++; # REMOVE!
74 $last_english = $phrase = translate($phrase, $toenglish, $req, $ua);
77 &::pSReply($last_english);
81 my ($phrase, $languagepair, $req, $ua) = @_;
83 my $urltext = URI::Escape::uri_escape($phrase);
84 $req->content("urltext=$urltext&lp=$languagepair&doit=done");
86 my $res = $ua->request($req);
89 if ($res->is_success) { # success.
90 my $html = $res->content;
92 foreach (split "\n", $html) {
93 $textarea = 1 if (/<textarea/i);
94 next unless ($textarea);
98 $textarea = 0 if (/<\/textarea/i);
102 $html =~ s/\n\s*\n/\n/g;
103 $html =~ s/\n/ /g; # ...
105 if ($html =~ /<textarea.*?>(.*?)<\/textarea/si) {
107 $translated =~ s/^[\n ]|[\n ]$//g;
109 &::WARN("failed regex for babelfish.");
113 $translated = "FAILURE w/ babelfish";
116 $translated ||= "NULL reply from babelfish.";