#!/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\n"; # Displays Japanese OK # print STDOUT "Content-type:text/html; charset=ISO-2022-JP\n\n\n\n"; # Apparently same as SHIFT_JIS # print STDOUT "Content-type:text/html; charset=ISO-2022\n\n\n\n"; # Apparently same as SHIFT_JIS # print STDOUT "Content-type:text/html; charset=EUC-JP\n\n\n\n"; # Apparently same as SHIFT_JIS # print STDOUT ""; # Displays Japanese OK # print STDOUT ""; # Does not display Japanese # print STDOUT "Content-type:\"text/html; charset=SHIFT_JIS\"\n\n\n\n"; Doesn't work. Unknown MIME type. # print STDOUT "Content-type:text/html charset=SHIFT_JIS\n\n\n\n"; Doesn't work. Blank screen. print STDOUT "Test Page for Patolis Translation Project\n"; print STDOUT "\n"; print STDOUT "

Test Page for Patolis Translation Project

\n"; print STDOUT "
\n"; print STDOUT "Type English Text Below$line_tag\n"; $text2display = $SARRAY{'English'}; $text2display =~ s/&/&/g; # Only needed 'cause we're displaying in a textarea # $text2display =~ s/\\/\/g; print STDOUT "$line_tag\n"; # print STDOUT "$line_tag\n"; # print STDOUT "$line_tag\n"; print STDOUT "\n"; print STDOUT "\n"; print STDOUT "\n"; # print STDOUT "
\n"; print STDOUT "$line_tag$line_tag\n"; print STDOUT "Translated Japanese Text Below$line_tag\n"; # Escape any html entities (e.g. < or >) by translating the & to & $text2display = $Japanese; $text2display =~ s/&/&/g; # Only needed 'cause we're displaying in a textarea print LOG "The translated Japanese is\n$Japanese\n" if ($debug); print LOG "and the displayable Japanese is\n$text2display\n" if ($debug); # print STDOUT "
"; print STDOUT "$line_tag\n"; # print STDOUT "$line_tag\n"; # print STDOUT "$line_tag\n"; # print STDOUT "
"; print STDOUT "\n"; # print STDOUT "\n"; # Escape any html entities (e.g. < or >) by translating the & to & $text2display = $Back2English; $text2display =~ s/&/&/g; # Only needed 'cause we're displaying in a textarea print LOG "The Back2English is\n$Back2English\n" if ($debug); print LOG "and the displayable Back2English is\n$text2display\n" if ($debug); print STDOUT "Translated Japanese Text, Translated Back To English$line_tag\n"; print STDOUT "$line_tag\n"; print STDOUT "\n";