#!/usr/bin/perl
##############################################################################
#
# This is a quick and dirty test CGI for testing Patolis's efforts at
# providing English-to-Japanese machine translations to their customers,
# code named Nova.
# This CGI is called from translation.html and calls the Nova service to
# translate (via a call to https://transdb1.webpika1.com/pat/ej.cgi) a given
# English text to Japanese and back to English again (via .../je.cgi).
#
# To test from the command line,
# export REQUEST_METHOD=GET
# export QUERY_STRING="First+Arg=One&Second+Arg=Two&Third+Arg=3"
# nova_cgi
#
##############################################################################
$debug=0;
$METHOD = $ENV{REQUEST_METHOD};
open(LOG,">>/tmp/nova.log") if ($debug);
print LOG "==================================================================\n" if ($debug);
print LOG "Method =>$METHOD<\n" if ($debug);
$line_tag="
";
if($METHOD eq 'POST' || $METHOD eq '') {
read(STDIN, $MYINPUT, $ENV{'CONTENT_LENGTH'});
chomp $MYINPUT;
if($METHOD eq '') { $line_tag="\n" }
} elsif($METHOD eq 'GET') {
$MYINPUT = $ENV{'QUERY_STRING'};
} else {
print "Unknown input METHOD ($METHOD)\n";
exit(1);
}
@combo = split(/&/, $MYINPUT);
foreach $combo (@combo) {
$combo =~ s/\+/ /g;
($input_name, $value) = split(/=/,$combo);
$input_name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
if ($input_name eq 'English') {print LOG "The original English is\n$value\n" if ($debug)}
$value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
if ($input_name eq 'English') {print LOG "\nThe un-URL-encoded English is\n$value\n" if ($debug)}
$SARRAY{$input_name} = $value;
}
if ($SARRAY{'submit'} eq "Send to Test Nova Server") {
$Japanese = reverse($SARRAY{'English'});
$Back2English = reverse($Japanese);
} else {
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
# By default, openSSL uses "/usr/local/ssl/certs", but with Carol's specially-built
# Perl in /dfs/prod/perl/bin, it's at /dfs/prod/openssl/ssl/certs.
my $req = HTTP::Request->new(POST => 'https://transdb1.webpika1.com/pat/ej.cgi');
$req->content($SARRAY{'English'});
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
$Japanese = $res->as_string; # Capture Nova's response.
$Japanese =~ s/^(.*?)\n\n//es; # Strip away all headers.
} else {
$Japanese = "Error translating English to Japanese\n";
# $Japanese .= "Here is as_string\n" . $res->as_string;
}
# And now, translate back into English.
my $req = HTTP::Request->new(POST => 'https://transdb1.webpika1.com/pat/je.cgi');
$req->content($Japanese);
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
$Back2English = $res->as_string; # Capture Nova's response.
$Back2English =~ s/^(.*?)\n\n//es; # Strip away all headers.
} else {
$Back2English = "Error translating Japanese Back to English\n";
}
}
# Redisplay the translation.html input form with two added read/only windows,
# one for the translated Japanese (not that we can read it), and the other
# for the Japanese translated back into English.
print STDOUT "Content-type:text/html; charset=SHIFT_JIS\n\n\n