Created
January 29, 2011 00:44
-
-
Save yurenju/801339 to your computer and use it in GitHub Desktop.
gtr.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from urllib2 import urlopen | |
from urllib import urlencode | |
import simplejson | |
import sys | |
def translate(text): | |
sl="zh-tw" | |
tl="en" | |
langpair='%s|%s'%(tl,sl) | |
base_url = 'http://ajax.googleapis.com/ajax/services/language/translate?' | |
data = urlencode({'v':1.0,'ie': 'UTF8', 'q': text, | |
'langpair':langpair}) | |
url = base_url+data | |
urlres = urlopen(url) | |
json = simplejson.loads(urlres.read()) | |
result = json['responseData']['translatedText'] | |
return result | |
if __name__ == "__main__": | |
print translate (sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment