Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Babelfish Module

Status
Not open for further replies.

Nova980

Technical User
May 20, 2009
40
US
Help,

Having trouble getting the translation... code follows:

Getting an error -> usage: ./translate.pl autobad.log at babel.pl line 9. Thanks

Code:
#!/usr/bin/perl
 use strict; use warnings;
 use Babelfish;         

 # input file to translate
 # specified as command line argument
 my $input_file = shift;
 if (not defined $input_file) {
     die "usage: ./translate.pl autobad.log";
 }         

 # create the Babelfish service
 my $service = Babelfish->new(
     service => 'Babelfish',
 );         

 # check for errors
 if (not defined $service) {
     die "Babelfish server unavailablen";
 }         

 # open input file to be translated
 open my $ifh, '<', $input_file
     or die "Could not open $input_file for reading: $!n";         

 # translate
 # write translation to output file handle
 my $ret = $service->translate(
     source          => 'Chinese',        # source language
     destination     => 'English',        # destination language
     text            => $ifh,             # file handle to read from
     ofh             => *STDOUT          # file handle to write to
 );
 if (not defined $ret) {
     print "Error while translating to English";
 }
 
if $inputfile has a true value there is no reason that the die function should be evaluated and return a message. Show how you are calling the script from the command line.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
 perl babelfish.pl

I want the script to read the input file "autobad.log" and eventually write a log with the English interpretation of all Chinese characters.

Code:
#!/usr/bin/perl
 use strict; use warnings;
 use Babelfish;         

 # input file to translate
 # specified as command line argument
 my $input_file = shift;
#$input_file = "autobad.log";
$ifh = "autobad.log";
 if (not defined $input_file) {
     die "usage: ./translate.pl autobad.log";
 }         

 # create the Babelfish service
 my $service = [URL unfurl="true"]WWW::Babelfish->new([/URL]
     service => 'Babelfish',
 );         

 # check for errors
 if (not defined $service) {
     die "Babelfish server unavailablen";
 }         

 # open input file to be translated
 open my $ifh, '<', $input_file
     or die "Could not open $input_file for reading: $!n";         

 # translate
 # write translation to output file handle
 my $ret = $service->translate(
     source          => 'Chinese',        # source language
     destination     => 'English',        # destination language
     text            => $ifh,             # file handle to read from
     ofh             => *STDOUT          # file handle to write to
 );
 if (not defined $ret) {
     print "Error while translating to English";
 }
 
'autobad.log' should be a param from the command line.

Your last example should fall over because $ifh is not defined. You then define it later.

Code:
Uncomment - #$input_file = "autobad.log";
Remove - $ifh = "autobad.log";

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top