-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
executable file
·62 lines (53 loc) · 1.77 KB
/
translate.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import re
import sys
import urllib
import json
from django.conf import settings
TRANSLATE_BASE_URL = 'https://www.googleapis.com/language/translate/v2'
def getSplits(text,splitLength=4500):
'''
Translate Api has a limit on length of text(4500 characters) that can be translated at once,
'''
return (text[index:index+splitLength] for index in xrange(0,len(text),splitLength))
def translate(text,src='', to='en'):
'''
A Python Wrapper for Google AJAX Language API:
* Uses Google Language Detection, in cases source language is not provided with the source text
* Splits up text if longer then 4500 characters, as a limit put up by the API
'''
params = ({'target': to,
'key': settings.GOOGLE_TRANSLATE_API_KEY
})
if src:
params['source'] = src
retText=''
for text in getSplits(text):
print 'text:', text
url = TRANSLATE_BASE_URL + '?' + urllib.urlencode(params) + '&q=' + text
print 'url:', repr(url)
print 'type(url):', type(url)
urlNet = url.encode('utf-8')
rawResponse = urllib.urlopen(urlNet).read()
print 'response:', rawResponse
resp = json.loads(rawResponse)
try:
retText += resp['data']['translations'][0]['translatedText']
except:
raise
return retText
def test():
msg = " Write something You want to be translated to English,\n"\
" Enter ctrl+c to exit"
print msg
while True:
text = raw_input('#> ')
retText = translate(text)
print retText
if __name__=='__main__':
try:
test()
except KeyboardInterrupt:
print "\n"
sys.exit(0)