-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans_.py
30 lines (25 loc) · 948 Bytes
/
trans_.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
from googletrans import Translator
def translate_sentence_code(query, lang):
try:
translator = Translator()
obj = translator.translate(str(query), dest=lang)
response = obj.text
if is_alpha(obj.text):
pronunciation = obj.text
else:
try:
pronunciation = obj.extra_data['translation'][1][-1]
except Exception as e:
pronunciation = obj.text
#pronunciation = obj.pronunciation
reply = {'display': response, 'say': pronunciation}
except Exception as e:
print(f"Error in translate_sentence_code: {e} \n request: {query} \n l: {lang}")
return {'display': "ask me a question",
'say': f"Error in translate_sentence_code: {e} \n request: {query} \n l: {lang}"}
return reply
def is_alpha(word):
try:
return word[:1].encode('ascii').isalpha()
except:
return False