#!/usr/bin/perl -w use warnings; use strict; $|++; # # Connection script between the Autshumato ITE and Apertium # #-- IMPORTS -- use CGI; use CGI::Carp qw/fatalsToBrowser/; use Encode; use WWW::Curl; use WWW::Curl::Easy; use JSON; use lib 'lib'; # API Key: dEQF38hrmBbupvSPcjLJwbg0/FI my @APERTIUM_ADDRESS = ("api.apertium.org:80"); # clear the path $ENV{PATH} = ''; # get the String to be translated # The URL from Autshumato looks like this: # # http://129.242.4.42:80/cgi-bin/moses/ite.cgi?tl=nb&sl=se&url=Mun%20lean%20Gogol # #my $sentence = decode('UTF-8', CGI->new->param ('url')); # change the variable in html my $sentence = CGI->new->param ('url'); # change the variable in html my $target_l = CGI->new->param ('tl'); # target language from Autshumato my $source_l = CGI->new->param ('sl'); # source language from Autshumato # This is the string sent to the official Apertium server (for language pairs other than the UiT ones): #curl 'http://api.apertium.org/json/translate?q=hello%20world&q=bye&langpair=en%7Ces&markUnknown=no' # %7C = | (vertical bar) my $string = urlencode($sentence);; my $pair = $source_l."%7C".$target_l; my $unknown = "no"; my $curl = new WWW::Curl::Easy; my $response_body; my $output = "Dummy text!"; $CGITempFile::TMPDIRECTORY = "/tmp"; my %dir = ( "sme-smj", "/home/fran/source/apertium-sme-smj/", "fin-sme", "/home/fran/source/apertium-sme-fin/", "sme-sma", "/home/fran/source/apertium-sme-sma/", "sme-nob", "/home/fran/source/apertium-sme-nob/", "nn-nb", "/home/fran/source/apertium-nn-nb/", "nb-nn", "/home/fran/source/apertium-nn-nb/" ); # Autshumato ITE is sending off only two-letter codes if they are available: if( ($pair eq "fi%7Cse") || ($pair eq "fin%7Csme") || ($pair eq "se%7Cnb") || ($pair eq "sme%7Cnob") || ($pair eq "se%7Csmj") || ($pair eq "se%7Csma") || ($pair eq "sme%7Csmj") || ($pair eq "sme%7Csma") || ($pair eq "nb%7Cnn") || ($pair eq "nn%7Cnb") || ($pair eq "nob%7Cnon") || ($pair eq "nno%7Cnob") ) { ## victorio stuff $output = "Victorio answer"; # map to the appropriate language pairs as used in the apertium dirs: if ($source_l eq "se" && $target_l eq "nb" ) { $pair = "sme-nob"; } elsif ($source_l eq "se" && $target_l eq "smj") { $pair = "sme-smj"; } elsif ($source_l eq "se" && $target_l eq "sma") { $pair = "sme-sma"; } elsif ($source_l eq "fi" && $target_l eq "se" ) { $pair = "fin-sme"; } else { $pair = "$source_l-$target_l"; } # else { $output = "No translation supported for the language pair $source_l-$target_l"; } my $prefix = "PATH=/home/fran/local/bin:/usr/bin:/bin:\$PATH PKG_CONFIG_PATH=/home/fran/local/lib/pkgconfig LD_LIBRARY_PATH=/home/fran/local/lib "; my $cmd = "echo \"$sentence\" | " . $prefix . "LANG=no_NO.UTF-8 /home/fran/local/bin/apertium -d" . $dir{$pair} . " -u " . $pair; #print $cmd; # DEBUG if($sentence ne '') { $output = decode('UTF-8', `$cmd`); } } else { $curl->setopt(CURLOPT_URL, "http://api.apertium.org/json/translate?q=" . $string . "&langpair=" . $pair . "&markUnknown=" . $unknown ); open (my $fileb, ">", \$response_body); $curl->setopt(CURLOPT_WRITEDATA,$fileb); my $retcode = $curl->perform; if ($retcode == 0) { # print("Transfer went ok\n"); my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE); # judge result and next action based on $response_code # print("Received response: $response_body\n"); } else { print("An error happened: ".$curl->strerror($retcode)." ($retcode)\n"); } #translate the string my $output_arr = from_json($response_body, {utf8 => 1}); $output = $output_arr->{"responseData"}{"translatedText"}; } # return the string print "Content-Type: text/html; charset=UTF-8\n\n"; print encode ('UTF-8', "
$output
"); sub urlencode { my $theURL = $_[0]; $theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg; return $theURL; }