-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtrans.py
executable file
·33 lines (27 loc) · 1006 Bytes
/
gtrans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,json,urllib
url_string = 'http://ajax.googleapis.com/ajax/services/language/'
def detect(text):
query = dict(v='1.0', q=text)
file = urllib.urlopen(url_string+'detect?%s' % urllib.urlencode(query))
result = json.loads(file.read())
return result['responseData']['language'] if result['responseData'] else None
def translate(text):
source = detect(text)
target = ['en','zh-CN'][source == 'en']
query = dict(v='1.0', q=text,langpair = '%s|%s' % (source, target))
file = urllib.urlopen(url_string+'translate?%s' % urllib.urlencode(query))
result = json.loads(file.read())
return result['responseData']['translatedText'] if result['responseData'] else None
def main():
if len(sys.argv) == 1:
info = '''
usage: python gtrans.py word
'''
print info
else:
text = ' '.join(sys.argv[1:])
print translate(text)
if __name__ == '__main__':
main()