-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranslate.py
46 lines (40 loc) · 1.44 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
from googletrans import Translator
import time
class Translate:
def __init__(self, client, src='en', target='zh-cn', ):
self.translator = Translator(client)
self.src = src
self.trg = target
def translate(self, sentence, max_try=3, count=0):
# text is string
if count >= 3:
return ''
try:
translation = self.translator.translate(sentence, dest=self.trg, src=self.src)
except:
time.sleep(5)
translation = self.translate(sentence, max_try, count=count + 1)
finally:
time.sleep(0.1)
return translation
def translate_batch(self, sentences, max_try=3, count=0):
# sentences is list
results = []
if count >= max_try:
return ['' for _ in range(len(sentences))]
try:
translations = self.translator.translate(sentences, dest=self.trg, src=self.src)
for translation in translations:
try:
translation = translation.text
except:
print('translate error...')
translation = ''
results.append(translation)
except:
time.sleep(2.5)
print('translate retry for %d times...' % (count + 1))
results = self.translate_batch(sentences, max_try, count=count + 1)
finally:
time.sleep(0.5)
return results