]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/babelfish.pl
more babelfish cleanup
[infobot.git] / src / Modules / babelfish.pl
index 6d3b0927d9a1f972690379140761f777775472c0..3a2e9f019075d347f04ed30ca3c9f3de0ce62c59 100644 (file)
@@ -53,7 +53,7 @@ BEGIN {
   $lang_regex = join '|', keys %lang_code;
 }
 
-sub babelfish {
+sub babelfishParam {
     return '' if $no_babelfish;
   my ($from, $to, $phrase) = @_;
   &main::DEBUG("babelfish($from, $to, $phrase)");
@@ -94,37 +94,52 @@ sub translate {
   my $translated;
 
   if ($res->is_success) {
-      my $html = $res->content;
-      # This method subject to change with the whims of Altavista's design
-      # staff.
-      ($translated) = $html;
+    my $html = $res->content;
+    # This method subject to change with the whims of Altavista's design
+    # staff.
+    ($translated) = $html;
 
-      $translated =~ s/<[^>]*>//sg;
-      $translated =~ s/&nbsp;/ /sg;
-      $translated =~ s/\s+/ /sg;
-      #&main::DEBUG("$translated\n===remove <attributes>\n");
+    $translated =~ s/<[^>]*>//sg;
+    $translated =~ s/&nbsp;/ /sg;
+    $translated =~ s/\s+/ /sg;
+    #&main::DEBUG("$translated\n===remove <attributes>\n");
 
-      $translated =~ s/\s*Translate again.*//i;
-      &main::DEBUG("$translated\n===remove after 'Translate again'\n");
+    $translated =~ s/\s*Translate again.*//i;
+    &main::DEBUG("$translated\n===remove after 'Translate again'\n");
 
-      $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
-      &main::DEBUG("len=" . length($translated) . " $translated\n===remove to first ':', optional Help\n");
+    $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
+    &main::DEBUG("len=" . length($translated) . " $translated\n===remove to first ':', optional Help\n");
 
-      $translated =~ s/\n/ /g;
-      # FIXME: should we do unicode->iso (no. use utf8!)
+    $translated =~ s/\n/ /g;
+    # FIXME: should we do unicode->iso (no. use utf8!)
   } else {
-      $translated = ":("; # failure
+    $translated = ":("; # failure
   }
   $translated = "babelfish.pl: result too long, probably an error" if (length($translated) > 700);
 
-  &main::pSReply($translated);
+  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;
 }
 
 if (0) {
     if (-t STDIN) {
-       #my $result = babelfish::babelfish('en','sp','hello world');
-       #my $result = babelfish::babelfish('en','sp','The cheese is old and moldy, where is the bathroom?');
-       my $result = babelfish::babelfish('en','gr','doesn\'t seem to translate things longer than 40 characters');
+       #my $result = babelfish::babelfish('en sp hello world');
+       #my $result = babelfish::babelfish('en sp The cheese is old and moldy, where is the bathroom?');
+       my $result = babelfish::babelfish('en gr doesn\'t seem to translate things longer than 40 characters');
        $result =~ s/; /\n/g;
        print "Babelfish says: \"$result\"\n";
     }