-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
322 lines (278 loc) · 8.74 KB
/
translator.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
__module_name__ = "Translator"
__module_version__='0.1'
__module_description__='Translate the messages of a given user to a specified language'
__module_author__='Fivesheep'
import xchat
import urllib
import urllib2
import re
import threading
from threading import Thread
import Queue
import traceback
try:
import json
except:
import simplejson as json
AUTOUSER={}
LAST_ERROR=''
class TranslateException(Exception):
pass
class GoogleTranslator:
LANGUAGES={
'AFRIKAANS' : 'af',
'ALBANIAN' : 'sq',
'AMHARIC' : 'am',
'ARABIC' : 'ar',
'ARMENIAN' : 'hy',
'AZERBAIJANI' : 'az',
'BASQUE' : 'eu',
'BELARUSIAN' : 'be',
'BENGALI' : 'bn',
'BIHARI' : 'bh',
'BULGARIAN' : 'bg',
'BURMESE' : 'my',
'CATALAN' : 'ca',
'CHEROKEE' : 'chr',
'CHINESE' : 'zh',
'CHINESE_SIMPLIFIED' : 'zh-CN',
'CHINESE_TRADITIONAL' : 'zh-TW',
'CROATIAN' : 'hr',
'CZECH' : 'cs',
'DANISH' : 'da',
'DHIVEHI' : 'dv',
'DUTCH': 'nl',
'ENGLISH' : 'en',
'ESPERANTO' : 'eo',
'ESTONIAN' : 'et',
'FILIPINO' : 'tl',
'FINNISH' : 'fi',
'FRENCH' : 'fr',
'GALICIAN' : 'gl',
'GEORGIAN' : 'ka',
'GERMAN' : 'de',
'GREEK' : 'el',
'GUARANI' : 'gn',
'GUJARATI' : 'gu',
'HEBREW' : 'iw',
'HINDI' : 'hi',
'HUNGARIAN' : 'hu',
'ICELANDIC' : 'is',
'INDONESIAN' : 'id',
'INUKTITUT' : 'iu',
'IRISH' : 'ga',
'ITALIAN' : 'it',
'JAPANESE' : 'ja',
'KANNADA' : 'kn',
'KAZAKH' : 'kk',
'KHMER' : 'km',
'KOREAN' : 'ko',
'KURDISH': 'ku',
'KYRGYZ': 'ky',
'LAOTHIAN': 'lo',
'LATVIAN' : 'lv',
'LITHUANIAN' : 'lt',
'MACEDONIAN' : 'mk',
'MALAY' : 'ms',
'MALAYALAM' : 'ml',
'MALTESE' : 'mt',
'MARATHI' : 'mr',
'MONGOLIAN' : 'mn',
'NEPALI' : 'ne',
'NORWEGIAN' : 'no',
'ORIYA' : 'or',
'PASHTO' : 'ps',
'PERSIAN' : 'fa',
'POLISH' : 'pl',
'PORTUGUESE' : 'pt-PT',
'PUNJABI' : 'pa',
'ROMANIAN' : 'ro',
'RUSSIAN' : 'ru',
'SANSKRIT' : 'sa',
'SERBIAN' : 'sr',
'SINDHI' : 'sd',
'SINHALESE' : 'si',
'SLOVAK' : 'sk',
'SLOVENIAN' : 'sl',
'SPANISH' : 'es',
'SWAHILI' : 'sw',
'SWEDISH' : 'sv',
'TAJIK' : 'tg',
'TAMIL' : 'ta',
'TAGALOG' : 'tl',
'TELUGU' : 'te',
'THAI' : 'th',
'TIBETAN' : 'bo',
'TURKISH' : 'tr',
'UKRAINIAN' : 'uk',
'URDU' : 'ur',
'UZBEK' : 'uz',
'UIGHUR' : 'ug',
'VIETNAMESE' : 'vi',
'WELSH' : 'cy',
'YIDDISH' : 'yi'}
CODES_SET=set(LANGUAGES.values())
BASEURL=r'http://ajax.googleapis.com/ajax/services/language/translate?'
def codeLookup(cls,lang):
lang=lang.upper()
if cls.LANGUAGES.has_key(lang):
return cls.LANGUAGES[lang]
else:
return None
codeLookup=classmethod(codeLookup)
def translate(cls,text,dest='en',src=None):
"""Translate the given text into the targetLang.
Arguments:
- `text`: the text to be translated
- `dest`: the code of the destination language
- `src`: the code of the source language, if not given, auto-detect feature will be used.
"""
langpair=''
if dest not in cls.CODES_SET:
raise TranslateException('Destination language not supported.')
if src == None:
langpair='|'+dest
elif src in cls.CODES_SET:
langpair=src+'|'+dest
else:
raise TranslateException('Source language not supported.')
params=urllib.urlencode({'v':1.0, 'q':text, 'langpair':langpair,'format':'text'})
url=cls.BASEURL+params
data=json.loads(urllib2.urlopen(url).read())
if data['responseStatus'] != 200:
raise TranslateException(data['responseDetails'])
translatedText=data['responseData']['translatedText'].encode('utf-8')
dectedLang=''
if data['responseData'].has_key('detectedSourceLanguage'):
dectedLang=data['responseData']['detectedSourceLanguage']
return (dectedLang, translatedText)
translate=classmethod(translate)
class TranslateThread(Thread):
def __init__(self,queue):
Thread.__init__(self,target=self.doTranslate)
self.queue=queue
self.keepalive=True
def doTranslate(self):
global LAST_ERROR
while True:
task=self.queue.get()
if not self.keepalive or task==None:
break
try:
context,user,src,dest,text=task
lang,translation=GoogleTranslator.translate(text,dest,src)
if translation.strip().lower() != text.strip().lower():
nick="[%s|%s]"%(user,lang)
context.emit_print("Channel Message", nick, translation)
except TranslateException,e:
LAST_ERROR="[TE: %s] <%s> %s"%(e,user,text)
except urllib2.URLError, e:
LAST_ERROR="[URL] %s"%e
except UnicodeError, e:
LAST_ERROR="[Encode: %s] <%s> %s"%(e,user,text)
class TranlsateMachine:
tasks=Queue.Queue()
workThread=TranslateThread(tasks)
workThread.setDaemon(True)
workThread.start()
def addTask(cls,task):
cls.tasks.put(task)
addTask=classmethod(addTask)
def translate(word,word_eol,userdata):
"""Translate the given sentence."""
text=None
lang=None
if re.match(r'[a-z]+\|[a-z]+',word[1]):
src,dest=word[1].split('|')
lang,text=GoogleTranslator.translate(word_eol[2],dest,src)
else:
lang,text=GoogleTranslator.translate(word_eol[1])
xchat.prnt("|%s| %s"%(lang,text))
return xchat.EAT_NONE
xchat.hook_command("tr",translate)
def auto_translate(word,word_eol,userdata):
"""Translate a given user's msgs automaticially.
Nick [dest] [src]
"""
global AUTOUSER
#print word
channel=xchat.get_info('channel')
user=word[1]
dest='en'
src=None
if len(word)>2:
dest=word[2]
if len(word)>3:
src=word[3]
AUTOUSER[channel+' '+user.lower()]=(dest,src)
xchat.prnt("user '%s' has been added to auto-translate list"%user)
return xchat.EAT_ALL
xchat.hook_command("a2tr",auto_translate)
def remove_auto_translate(word,word_eol,userdata):
"""Remove a user from the a2tr list
"""
global AUTOUSER
channel=xchat.get_info('channel')
user=word[1]
cu=channel+' '+user
if AUTOUSER.pop(cu,None) != None:
xchat.prnt("User %s has been removed from the list"%user)
return xchat.EAT_ALL
xchat.hook_command("rma2tr",remove_auto_translate)
def read_error(word,word_eol,userdata):
"""Read last error
"""
global LAST_ERROR
xchat.prnt(LAST_ERROR)
xchat.hook_command("last_err",read_error)
def add_translate_task(word,word_eol,userdata):
"""add tasks
"""
global AUTOUSER
user=word[0]
text=word[1]
channel=xchat.get_info('channel')
cu=channel+' '+user.lower()
if AUTOUSER.has_key(cu):
#xchat.prnt('Add a TasK: %s'%text)
dest,src=AUTOUSER[cu]
TranlsateMachine.addTask((xchat.get_context(),user,src,dest,text))
return xchat.EAT_NONE
xchat.hook_print("Channel Message",add_translate_task)
def print_watching_users(word,word_eol,userdata):
"""print the users in the a2tr list of the channel.
"""
global AUTOUSER
channel=xchat.get_info('channel')
users=[ cu.split(' ')[1] for cu in AUTOUSER.keys() ]
xchat.prnt('A2TR: %s'%(' '.join(users)))
xchat.EAT_ALL
xchat.hook_command("lsa2tr",print_watching_users)
def c2l(word,word_eol,userdata):
"""langcode to language
"""
code=word[1].lower()
for lang,c in GoogleTranslator.LANGUAGES.items():
if code==c:
xchat.prnt("%s <=> %s"%(lang,c))
return xchat.EAT_ALL
xchat.prnt("No Language for %s"%code)
xchat.EAT_ALL
xchat.hook_command("code2lang",c2l)
def l2c(word,word_eol,userdata):
"""language to langcode
"""
lang=word[1].upper()
if GoogleTranslator.LANGUAGES.has_key(lang):
code=GoogleTranslator.LANGUAGES[lang]
xchat.prnt("%s <=> %s"%(code,word[1]))
else:
xchat.prnt("No langcode for %s"%word[1])
xchat.EAT_ALL
xchat.hook_command("lang2code",l2c)
def unload_translator(userdata):
TranlsateMachine.workThread.keepalive=False
TranlsateMachine.addTask(None)
print "Translator is unloaded."
xchat.hook_unload(unload_translator)
print 'tranlsator plugin loaded.'