• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

babelfish subtitle translation script - perl

Status
Not open for further replies.
My friend and I have been playing around with babelfish to do random string translations. We were going to make a firefox plugin but got lazy and dropped the idea for a while. Today we looked at the subtitle format for a couple of Japanese movies he downloaded and inspiration struck. Here's the fruit of our labor (well, his, my idea, his code). It's a little slow (the http requests take forever - we need to pipe together as many subtitles as possible at one time to speed it up) but it works.

Options may come later (user set translation paths?) but probably not (unless we get a Tivo and our idea works). Have fun.

edit: The subtitles get translated from English to a random foreign language and back again twice. The original subtitle is shown in parens above the translation

ex.

(fortune cookie)
possibility bisquit

(don't bother to call)
llama fact that bitter and astringent hymn dries oneself



#!/usr/bin/perl -w
use Lingua::Translate;
use Getopt::Std;

$| = 1;

sub usage() {
print "Moron. -i input, -o output \n";
exit;
}

Lingua::Translate::config
(
back_end => "Babelfish",
babelfish_uri => 'http://babelfish.altavista.com/tr?',
ua => LWP::UserAgent->new(),
);

getopt("io");

if (!$opt_i || !$opt_o) {
usage();
}

open(SOURCE, "< $opt_i")
or die "Couldn't open $opt_i for reading: $!\n";
open(DEST, "> $opt_o")
or die "Couldn't open $opt_o for writing: $!\n";

my $trans = Lingua::Translate::Babelfish->new(src => "en", dest=> "ko");
my @lang = $trans->available();

my @english = grep { /en_/ } @lang;
my @other = grep { /_en/ } @lang;

select(DEST);
$in_string = "";
while (defined ($line = <SOURCE>)) {
$in_string .= $line;
chomp($line);
if ($line eq "\r" || $line eq "") {
my @block;
if ($line eq "\r") {
@block = split(/\r\n/, $in_string);
} else {
@block = split(/\n/, $in_string);
}
my $o = 2;
while( $o < $#block + 1 ) {
$original = $block[$o];
my $i = 0;
while ($i < 3) {
my @choice = split(/_/, $english[ rand @english ]);
$trans = Lingua::Translate::Babelfish->new(src => $choice[0], dest => $choice[1]);
$block[$o] = $trans->translate($block[$o]);
$trans = Lingua::Translate::Babelfish->new(src => $choice[1], dest => $choice[0]);
$block[$o] = $trans->translate($block[$o]);
$i++;
}
$block[$o] = "(" . $original . ")\n" . $block[$o];
$o++;
}
$in_string = "";

print STDOUT $block[0] . "\n";
foreach $item (@block) {
print $item . "\n";
}
print "\n";
}
}

select(STDOUT);
 

Boomer

Member
What do you think is going to happen when you post something <1% of posters will understand?


(@$@bubblegum!!=)
!----if still flavored, chew===+
!----if not, spit out===+

{chim jiggedy9908':
:LA LA LA BLIP}

select(STDOUT);
 
Status
Not open for further replies.
Top Bottom