http://qs321.pair.com?node_id=291701
Category: Fun Stuff
Author/Contact Info #include
email: include at riotmod dot com
Description: garble.pl

A fun little toy that mangles text using Alta Vista's Babelfish Service by repeatedly translating the text. It uses WWW::Babelfish (and thusly IO::String) to do the translation, and is extremely configurable. As an example, if the script is run with

$ perl garble.pl -i file.txt -o mangle.txt -m French,Spanish,German

It will translate file.txt first into French, then into English, then into Spanish, then into English, then into German, and then back into English. Then, it saves the "mangled" text to mangle.txt.

You can see some example output posted up on my weblog, "The Steb-by-Step Guide To World Domination".

Usage:
perl garble.pl options

Options:
Options marked with a * are required
-i <filename> - Loads a file to mangle*
-o <filename> - File to write mangled text to*
-s <language> - Singlepass mode, with the specified language
-b <language> - Base language. English by default.
-m <language>,... - Multipass mode. Any number of languages seperated by commas. Default is -m French,Spanish
-t <language> - Translates one language to another

UPDATED: I added a "simple" translation mode, allowing people to use the script to actually translate text documents.
#!/usr/bin/perl
#
# garble.pl
#
# Uses Babelfish to mangle text.  Useless but fun :-)
#
# Usage:
# perl garble.pl <options>
#
# Options:
# Options marked with a * are required
#
# -i <filename>       -  Loads a file to mangle*
# -o <filename>       -  File to write mangled text to*
# -s <language>       -  Singlepass mode, with the specified language
# -b <language>       -  Base language.  English by default.
# -m <language>,...   -  Multipass mode.  Any number of languages
#                        seperated by commas.  Default is
#                        -m French,Spanish
# -t <language>       -  Translates one language to another
#
use Getopt::Std;
use WWW::Babelfish;

#
#=================[ Support Subs ]====================================
#
sub DoTranslation
{
    my($in_text,$language_1,$language_2)=@_;

    my $obj = new WWW::Babelfish( 'agent' => 'GarbleText/1.0' );
      die( "Babelfish server unavailable\n" ) unless defined($obj);

    my $altered = $obj->translate( 'source' => "$language_1",
                                  'destination' => "$language_2",
                                  'text' => "$in_text");
      die("Could not translate: " . $obj->error) unless defined($alter
+ed);
    return $altered;
}


sub PrintBanner
{
    print "\n=========\n";
    print "garble.pl\n";
    print "=========\n\n";
    print "(c)Copyleft #include 2003\n\n";
}

#
#=================[ Main ]============================================
#
my $default_language="English";
my $multipass_mode=1;
my $singlepass_mode=0;
my @mpass=("French","Spanish");
my $singlepass_lang="English";

getopt('iosbmt');

if( ($opt_i) && ($opt_o) ) # Everything we need
{
    PrintBanner();
    if($opt_s)
    {
        $singlepass_mode=1;
        $multipass_mode=0;
        $singlepass_lang=$opt_s;
    }

    if($opt_t)
    {
        $singlepass_mode=0;
        $multipass_mode=0;
    }

    if($opt_m)
    {
        @mpass=split(',',$opt_m);
    }

    if($opt_b)
    {
        $default_language=$opt_b;
    }
} else {
    PrintBanner();
    print "Usage:\n";
    print "perl garble.pl <options>\n\n";
    print "Options:\n";
    print "Options marked with a * are required\n\n";
    print "-i <filename>       -  Loads a file to mangle*\n";
    print "-o <filename>       -  File to write mangled text to*\n";
    print "-s <language>       -  Singlepass mode, with the specified 
+language\n";
    print "-b <language>       -  Base language.  English by default.\
+n";
    print "-m <language>,...   -  Multipass mode.  Any number of langu
+ages\n";
    print "                       seperated by commas.  Default is\n";
    print "                       -m French,Spanish\n";
    print "-t <language>       -  Translates one language to another\n
+\n";
    exit;
}

# Everything's in place, let the mangling begin!

# Load in the text
print "Reading $opt_i...";
open(TEXTFILE,"<$opt_i");
my @tfile=<TEXTFILE>;
my $tt = join('',@tfile);
close TEXTFILE;
print "done!\n";

my $at;
my $mangled_text;

if($opt_t)
{
    # Simple translation mode
    print "Simple Translation Mode\n";
    print "Language: $opt_t\n";
    $at=DoTranslation($tt,$default_language,$opt_t);
    $mangled_text=$at;
}

# Singlepass mode first
if($singlepass_mode==1)
{
    print "Singlepass Mode\n";
    print "Language: $singlepass_lang\n";
    print "Translating $default_language to $singlepass_lang...\n";
    $at=DoTranslation($tt,$default_language,$singlepass_lang);
    print "Translating $singlepass_lang to $default_language...\n";
    $tt=DoTranslation($at,$singlepass_lang,$default_language);
    $mangled_text=$tt;
}

if($multipass_mode==1)
{
    print "Multipass Mode\n";
    print "Passes: ".($#mpass +1)."\n";
    foreach $lang (@mpass)
    {
        print "Translating $default_language to $lang...\n";
        $at=DoTranslation($tt,$default_language,$lang);
        print "Translating $lang to $default_language...\n";
        $tt=DoTranslation($at,$lang,$default_language);
    }
    $mangled_text=$tt;
}

# Now that we've got the mangled text, let's print it
print "Writing $opt_o...";
open(DUMPTEXT,">$opt_o");
print DUMPTEXT "$mangled_text\n";
close DUMPTEXT;
print "done!\n";