Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Chained Babelfish Translations

by merlyn (Sage)
on Sep 07, 2000 at 07:22 UTC ( #31359=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info Randal L. Schwartz - merlyn
Description: The Babelfish service at Altavista is cool, but it's really fun to drag some random text to and from weird languages to see how much "it loses in the translation". So I wrote a nice short program for an upcoming Linux Magazine column to automate the chaining from one language to the next, showing the intermediate results. 95 lines (again {grin}).

This code is a review draft from a forthcoming Linux Magazine Perl column and is provided for review purposes only. Further copying and redistribution is not permitted. You can download this and put it on your site to use it for "evaluation purposes", but you cannot redistribute the source out of context. Once the magazine releases the code, in about three months, you can download it from my official site and do with it what you want. I'm sorry for being more restrictive than most open source stuff, but this is "work for hire", and I have to be careful.

#!/usr/bin/perl -Tw
# copyright (c) 2000, Randal L. Schwartz for Linux Magazine
# this draft provided for review purposes only
use strict;

my %LANGUAGES = qw(
  en English
  fr French
  ge German
  it Italian
  po Portuguese
  ru Russian
  sp Spanish

$PERMITTED{$_}++ for qw(
  enfr enge enit enpo ensp fren geen iten poen ruen spen frge gefr

use CGI qw(:all *table *Tr escapeHTML);

print header, start_html('babel linker'), h1('babel linker');

print                # text area form, translate button:
  textarea('text', "My hovercraft is full of eels!", 4, 50),

my $translate_wanted = defined param('translate');
Delete('translate');        # so language-changing URLs don't trigger

(my $pi = path_info()) =~ m{^/}g; # skip past leading slash if present

my @path = $pi =~ /\G(@{[join "|", keys %LANGUAGES]})/g;
@path = qw(en ge) unless @path;    # default to english-to-german if n
+o path

## start of language selection matrix...
  start_table({border => 0, cellspacing => 0, cellpadding => 2}),

my $pathstring = url()."/";

my @links = ("",@path,"");
while (@links > 1) {
  my ($from, $to) = @links;    # first two, ignore rest for now
    td($from ? $to ? "to" : "and then to" : "from"),
      td(links($pathstring, $from, $to));
  $pathstring .= $to;
  shift @links;

print end_Tr, end_table;
## ...end of language selection matrix

## now do the translation if needed:
if ($translate_wanted and @path > 1) {
  require WWW::Babelfish;
  my $text = param('text');
  my $linguist = WWW::Babelfish->new or die "no linguist";

  print start_table({border => 0, cellspacing => 0, cellpadding => 3})
  while (@path > 1) {
    my ($src, $dst) = @path;    # first two elements, rest ignored for
+ now
    $_ = $LANGUAGES{$_} for $src, $dst;
    my $result = $linguist->
      translate(source => $src, destination => $dst, text => $text);
    print Tr(td("... from $src to $dst becomes ..."),
         td(defined $result ? escapeHTML($result) :
        "... unintelligible (aborting) ..."));
    last unless defined $result;
    shift @path;        # slide it over
    $text = $result;
  print end_table;


print end_html;

sub links {
  my ($path, $from, $to) = @_;
  my @permitted = sort keys %LANGUAGES;

  ## strip bogus combos if this isn't the first in the chain:
  @permitted = grep { exists $PERMITTED{"$from$_"} } @permitted if $fr

  return map {
    my $lang = $LANGUAGES{$_};
    ($_ eq $to) ? b($lang) :
      a({-href => "$path$_?".query_string()}, $lang), br;
  } @permitted;
Replies are listed 'Best First'.
RE: Chained Babelfish Translations
by dmtelf (Beadle) on Sep 07, 2000 at 15:02 UTC
    There is a WWW::Babelfish module on CPAN.

    There is also an informative article on the Babelfish module & its design/implementation etc.

    Finally, there is an item on slashdot about Babelfish.

    Hope someone finds this useful!


    UPDATE merlyn has pointed out that I referred to the Babelfish module which his program uses anyway!

    My mistake - it's the last time I do a post before my first coffee of the day! :-) Still, I hope someone finds the other references useful.

RE: Chained Babelfish Translations
by vroom (Pope) on Sep 07, 2000 at 08:36 UTC
    Yeah that is always fun to do and automation just makes it that much cooler. Gotta love Perl.

    vroom | Tim Vroom |
Re: Chained Babelfish Translations
by davisagli (Scribe) on Jun 01, 2001 at 23:59 UTC
    Yes, that is fun. I transformed JAPH into...

    "Inside inside 1:00 this code, for that hour, you will split inside edge to respect, it connects in that outside person, inside the rhenium which starts one court of justice inside the string is putting in order in the lower part of grain, to under the accuracy which is, is, to be near in the hacker and it internal are they integral parts and the greenhouse which is a shape and."

    ...following at least 20 translations.

    Automating the process is a very cool idea.

Re: Chained Babelfish Translations
by Anonymous Monk on Jul 28, 2001 at 09:09 UTC
    Damn, I really wanted to do this, but was beaten to it :( /Amoe on vacation
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://31359]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-06-27 00:34 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (595 votes). Check out past polls.