-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.py
199 lines (170 loc) Β· 6.9 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
import requests
import dic
from googletrans import Translator
# Filing _ START
f1 = open('user.txt', 'r')
f2 = open('src.txt', 'r')
f3 = open('dest.txt', 'r')
f4 = open('langList.txt', 'r')
user = f1.read().split()
src = f2.read().split()
dest = f3.read().split()
langList = f4.read()
f1.close()
f2.close()
f3.close()
f4.close()
# Filing _ END
# File Modify Function _ START
def filing():
f1 = open('user.txt', 'w')
f2 = open('src.txt', 'w')
f3 = open('dest.txt', 'w')
for item in user:
f1.write("%s\n" % item)
for item in src:
f2.write("%s\n" % item)
for item in dest:
f3.write("%s\n" % item)
f1.close()
f2.close()
f3.close()
# File Modify Function _ END
# Encoding Function _ START
def encode(text):
text = text.title()
if text == 'Bangla':
return 'bn'
for name, code in dic.dic.items():
if name == text:
return code
else:
return 'wrong'
# Encoding Function _ END
# Encoding Function _ START
def decode(text):
if text == 'auto':
return 'Any (Auto Detect)'
for name, code in dic.dic.items():
if code == text:
return name
# Encoding Function _ END
# Translating Function _ START
def translator(text, index):
if src[index] == 'auto':
return Translator().translate(text, dest[index]).text
else:
return Translator().translate(text, dest[index], src[index]).text
# Translating Function _ END
# Commands _ START
def welcome(user_id, name):
user_id = str(user_id)
message = 'Hi, {name}. πππ \n\nWelcome to TranslatorBot.\n\n' \
'Send any sentence to translate.\n\n' \
'Send /admin to chat with admin.\n' \
'Send /help to get help.\n' \
'\n\n\n' \
'A translator bot by Xaadu (V--7.0).\n\n' \
'--------------------\n' \
'Admin --> Xaadu\n' \
'Creator --> Xaadu\n--------------------\n\n' \
'Contact ::->\n' \
'--> FB: http://facebook.com/xayed42/\n' \
'--> Telegram: http://t.me/xaadu\n' \
'' \
'\nBest of luck. Any feedback is much appreciated.'.format(name=name)
if user_id not in user:
user.append(user_id)
src.append('auto')
dest.append('en')
filing()
return message + '\n\n\nCurrent translation: \n\n' + 'From: %s\nTo: %s' \
% (decode(src[user.index(str(user_id))]), decode(dest[user.index(str(user_id))]))
def current_language(user_id):
if str(user_id) not in user:
return 'Sorry. πππ\n\n Please try sending /start once more.'
return 'Current translation: \n\n' + 'From: %s\nTo: %s' \
% (decode(src[user.index(str(user_id))]), decode(dest[user.index(str(user_id))]))
def language_change(user_id, message):
if user_id in user:
try:
message = message.split('-')
print(message)
if encode(message[0]) != 'wrong' and encode(message[1]) != 'auto' and encode(message[1]) != 'wrong':
src[user.index(str(user_id))] = encode(message[0])
dest[user.index(str(user_id))] = encode(message[1])
text = "Success! πππ"
filing()
else:
text = 'Sorry. πππ Check format and supported languages.' \
'(Auto detect can\'t be destination language)'
return text + '\n\nCurrent translation: \n\n' + 'From: %s\nTo: %s' \
% (decode(src[user.index(str(user_id))]), decode(dest[user.index(str(user_id))]))
except Exception as e:
print(e)
return "Sorry. πππ Something wrong happened.\n" \
"Try sending /start once more or " \
"check format and supported languages."
else:
return 'Sorry. πππ\nTry sending /start once more.'
def swap_language(user_id):
if str(user_id) not in user:
return 'Sorry. πππ\n\n Please try sending /start once more.'
try:
if src[user.index(str(user_id))] == 'auto':
return 'Sorry. Language swap not possible. Auto detect can\'t be selected as destination language.'
temp = src[user.index(str(user_id))]
src[user.index(str(user_id))] = dest[user.index(str(user_id))]
dest[user.index(str(user_id))] = temp
filing()
return 'Success! πππ\n\nCurrent translation: \n\n' \
'From: %s\nTo: %s' % (decode(src[user.index(str(user_id))]),
decode(dest[user.index(str(user_id))]))
except:
return 'Sorry. πππ Something isn\'t right.' \
'Try sending /start once more.'
def reset(user_id):
if str(user_id) not in user:
return 'Sorry. πππ\n\n Please try sending /start once more.'
try:
src[user.index(str(user_id))] = 'auto'
dest[user.index(str(user_id))] = 'en'
filing()
return 'Success! πππ\n\nCurrent translation: \n\n\n' \
'From: %s\nTo: %s' % (decode(src[user.index(str(user_id))]),
decode(dest[user.index(str(user_id))]))
except:
return 'Sorry. πππ Something isn\'t right.' \
'Try sending /start once more.'
def supported_languages():
return langList
def get_help(name):
return 'Welcome {name}. πππ\n\n' \
'Send \'/admin\' to chat with admin.\n' \
'Send \'/cl\' to get current translation languages.\n' \
'Send \'/languages\' to see total supported languages.\n' \
'Send \'/swipe\' to swipe current languages.\n' \
'Send \'/reset\' to set auto as source language and ' \
'English as destination Language.\n' \
'Send \'/changed FromLanguage-ToLanguage\'\n' \
'Example: To translate english to bangla, simply send,\n\n' \
'/changed english-bangla\n\n' \
'To auto detect your language, send \'auto\' as source/FromLanguage.\n\n' \
'A translator bot by Xaadu (V--6.0).\n\n' \
'--------------------\n' \
'Admin --> Xaadu\n' \
'Creator --> Xaadu\n--------------------\n\n' \
'For further help, contact me:\n\n' \
'--> FB: http://facebook.com/xayed42/\n' \
'--> Telegram: http://t.me/xaadu\n' \
'' \
'\n\nBest of luck. Any feedback is much appreciated.'.format(name=name)
# Commands _ END
def translate(user_id, message):
if str(user_id) not in user:
return 'Sorry. πππ\n\n Please try sending /start once more.'
try:
return translator(message, user.index(str(user_id)))
except:
return 'Sorry. πππ Something isn\'t right.' \
'Try sending /start once more or change languages or reset languages by sending /reset.'