WWW::Babelfish - Perl extension for translation via Babelfish or
Google
use WWW::Babelfish;
$obj = new WWW::Babelfish( service => 'Babelfish', agent => 'Mozilla/8.0', proxy => 'myproxy' );
die( "Babelfish server unavailable\n" ) unless defined($obj);
$french_text = $obj->translate( 'source' => 'English',
'destination' => 'French',
'text' => 'My hovercraft is full of eels',
'delimiter' => "\n\t",
'ofh' => \*STDOUT );
die("Could not translate: " . $obj->error) unless defined($french_text);
@languages = $obj->languages;
Perl interface to the WWW babelfish translation server.
- new
- Creates a new WWW::Babelfish object.
Parameters:
service: Babelfish, Google or Yahoo; default is Babelfish
agent: user agent string
proxy: proxy in the form of host:port
- services
- Returns a plain array of the services available (currently Babelfish,
Google or Yahoo).
- languages
- Returns a plain array of the languages available for translation.
- languagepairs
- Returns a reference to a hash of hashes. The keys of the outer hash
reflect all available languages. The hashes the corresponding values
reference contain one (key) entry for each destination language that the
particular source language can be translated to. The values of these inner
hashes contain the Babelfish option name for the language pair. You should
not modify the returned structure unless you really know what you're
doing.
Here's an example of a possible return value:
{
'Chinese' => {
'English' => 'zh_en'
},
'English' => {
'Chinese' => 'en_zh',
'French' => 'en_fr',
'German' => 'en_de',
'Italian' => 'en_it',
'Japanese' => 'en_ja',
'Korean' => 'en_ko',
'Portuguese' => 'en_pt',
'Spanish' => 'en_es'
},
'French' => {
'English' => 'fr_en',
'German' => 'fr_de'
},
'German' => {
'English' => 'de_en',
'French' => 'de_fr'
},
'Italian' => {
'English' => 'it_en'
},
'Japanese' => {
'English' => 'ja_en'
},
'Korean' => {
'English' => 'ko_en'
},
'Portuguese' => {
'English' => 'pt_en'
},
'Russian' => {
'English' => 'ru_en'
},
'Spanish' => {
'English' => 'es_en'
}
};
- translate
- Translates some text using Babelfish.
Parameters:
source: Source language
destination: Destination language
text: If this is a reference, translate interprets it as an
open filehandle to read from. Otherwise, it is treated
as a string to translate.
delimiter: Paragraph delimiter for the text; the default is "\n\n".
Note that this is a string, not a regexp.
ofh: Output filehandle; if provided, the translation will be
written to this filehandle.
If no ofh parameter is given, translate will return the text;
otherwise it will return 1. On failure it returns undef.
- error
- Returns a (hopefully) meaningful error string.
Babelfish translates 1000 characters at a time. This module tries
to break the source text into reasonable logical chunks of less than 1000
characters, feeds them to Babelfish and then reassembles them. Formatting
may get lost in the process; also it's doubtful this will work for
non-Western languages since it tries to key on punctuation. What would make
this work is if perl had properly localized regexps for sentence/clause
boundaries.
Support for Google is preliminary and hasn't been extensively
tested (by me). Google's translations used to be suspiciously similar to
Babelfish's, but now some people tell me they're superior.
Dan Urist, durist@frii.com