Popular posts

Pages

Saturday, April 9, 2011

perl script to use google language translate API

I created this script which translated text from one language to another using yahoo's http://babelfish.yahoo.com/ services. But its not the one which i'm going to talk about. That is because in the yahoo version i used perl's LWP module to do a post query on the yahoo server and get back the http response, much like what we see on a browser, and got the translated text using pattern matching operations. That is one crude way of getting things done, but it works!

Now this does the same job but using google services (translate.google.com) and the respective APIs. Its a more sophisticated and efficient way of achieve the same result. Google has exposed the API and the methods for doing this job. You can call them from applications such as - your software, your mobile app, your website or a small script (like tihs one), etc. Mind you, it is illegal to use it for commercial purposes.

For this translate API, you've to generate an activation key (tied to your google account). 
First section is the creation of a LWP::UserAgent object. Then for sanity, convert the text to webformat. Then I've created the url with editable parameters as variables. The text to be converted is $str and needs to be given as command line argument. I like German so its my language of choice for converted text. The response object is created by the get function on UA object. The 'content' method is used on response object for the output, whereas the error state and message are captured in 'is_success' and 'status_line' methods of the response obj. Again, for the desired line in the http response content we need to use pattern matching as shown.

The idea is, how the google (or any) api can be leveraged with perl's lwp module to do some fun programming or something more useful which can solve real problems at your work/school/college. If you've something to share, do comment.
Here's the code:

## google translate API

# https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=de

use Strict;
use LWP::UserAgent;
use URI::Escape;

if ($ARGV[0] eq "") {
print "Usage: perl lwp12 \"text to be converted\""; die; }
$ua = LWP::UserAgent->new();

$str = uri_escape ($ARGV[0]);
$tolang = "de";

$url = "https://www.googleapis.com/language/translat/v2?key=<yourPersonalKeyHere>&q=" 
. $str 
. "&source=en&target="
. $tolang;

$resp = $ua->get ($url);

if ($resp->is_success) {
$newstr = $resp->content;
$newstr =~ /Text":\s"(.*)"\n/;
print "Text in $tolang:\n$1\n"; }

else { print "Program failed with message: ", $resp->status_line;}
# END


Here is how it runs:

>perl lwp12googletranslate.pl "My first program."
Text in de:
Mein erstes Programm.


Output while error handling:

>perl lwp12googletranslate.pl "my first wife"
Program failed with message: 404 Not Found

2 comments:

  1. this doesn't work anymore, unfortunately. Any alternatives?

    Josvanr a t gmail.com

    ReplyDelete
  2. It is a paid service now! unfortunately..

    ReplyDelete