-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdialogs_translator.py
305 lines (282 loc) · 14.1 KB
/
dialogs_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
import argparse
import copy
import json
import os
import sys
import time
from googletrans import Translator # pip install googletrans==4.0.0rc1
from print_neatly import print_neatly
def translate(file_path, tr, src='it', dst='en', verbose=False, max_retries=5):
def translate_sentence(text):
target = text
translation = tr.translate(target, src=src, dest=dst).text
if target[0].isalpha() and translation[0].isalpha and not target[0].isupper():
translation = translation[0].lower() + translation[1:]
text = translation
if verbose:
print(target, '->', translation)
return text
def try_translate_sentence(text):
try:
return (translate_sentence(text), True)
except:
for _ in range(max_retries):
try:
time.sleep(1)
return (translate_sentence(text), True)
except:
pass
return (text, False)
translations = 0
with open(file_path, 'r', encoding='utf-8-sig') as datafile:
data = json.load(datafile)
num_events = len([e for e in data["events"] if e is not None])
i = 0
for events in data["events"]:
if events is not None:
print('{}: {}/{}'.format(file_path, i+1, num_events))
i += 1
for pages in events['pages']:
for list in pages['list']:
# Plain text (ex: ["plain text"])
if list['code'] == 401:
# null or empty string check
if not list['parameters'][0]:
continue
# translate
list['parameters'][0], success = try_translate_sentence(list['parameters'][0])
if not success:
print('Anomaly plain text: {}'.format(list['parameters'][0]))
else:
translations += 1
# Choices (ex: [["yes", "no"], 1, 0, 2, 0])
elif list['code'] == 102:
# null or empty list check
if not list['parameters'][0]:
continue
# translate list
for j, choice in enumerate(list['parameters'][0]):
# null or empty string check
if not choice:
continue
# translate
list['parameters'][0][j], success = try_translate_sentence(choice)
if not success:
print('Anomaly choices: {}'.format(choice))
else:
translations += 1
# Choices (answer) (ex: [0, "yes"])
elif list['code'] == 402:
# invalid length null or empty string check
if len(list['parameters']) != 2 or not list['parameters'][1]:
print('Anomaly choices (answer) - Unexpected 402 Code: {}'.format(list['parameters']))
continue
# translate
list['parameters'][1], success = try_translate_sentence(list['parameters'][1])
if not success:
print('Anomaly choices (answer): {}'.format(list['parameters'][1]))
else:
translations += 1
return data, translations
def translate_neatly(file_path, tr, src='it', dst='en', verbose=False, max_len=40, max_retries=5):
def translate_sentence(text):
target = text
translation = tr.translate(target, src=src, dest=dst).text
if target[0].isalpha() and translation[0].isalpha and not target[0].isupper():
translation = translation[0].lower() + translation[1:]
text = translation
return text
def try_translate_sentence(text):
try:
return (translate_sentence(text), True)
except:
for _ in range(max_retries):
try:
time.sleep(1)
return (translate_sentence(text), True)
except:
pass
return (text, False)
translations = 0
with open(file_path, 'r', encoding='utf-8-sig') as datafile:
data = json.load(datafile)
num_events = len([e for e in data["events"] if e is not None])
i = 0
for events in data["events"]:
if events is not None:
print('{}: {}/{}'.format(file_path, i+1, num_events))
i += 1
for pages in events['pages']:
len_list = len(pages['list'])
list_it = 0
while list_it < len_list:
# 102 Choices (dont nestly translate) (ex: [["yes", "no"], 1, 0, 2, 0])
if pages['list'][list_it]['code'] == 102:
# null or empty list check
if not pages['list'][list_it]['parameters'][0]:
list_it += 1
continue
# translate list
for j, choice in enumerate(pages['list'][list_it]['parameters'][0]):
# null or empty string check
if not choice:
print('Anomaly choices - Unexpected 102 code: {}'.format(choice))
continue
# translate
pages['list'][list_it]['parameters'][0][j], success = try_translate_sentence(choice)
if not success:
print('Anomaly choices: {}'.format(choice))
else:
translations += 1
list_it += 1
# 402 Choices (answer) (dont nestly translate) (ex: [0, "yes"])
elif pages['list'][list_it]['code'] == 402:
# invalid length null or empty string check
if len(pages['list'][list_it]['parameters']) != 2 or not pages['list'][list_it]['parameters'][1]:
print('Anomaly choices (answer) - Unexpected 402 Code: {}'.format(pages['list'][list_it]['parameters']))
list_it += 1
continue
# translate
pages['list'][list_it]['parameters'][1], success = try_translate_sentence(pages['list'][list_it]['parameters'][1])
if not success:
print('Anomaly choices (answer): {}'.format(pages['list'][list_it]['parameters'][1]))
else:
translations += 1
list_it += 1
# 401 Plain text (to nestly translate) (ex: ["plain text"])
elif pages['list'][list_it]['code'] == 401:
list_it_2 = list_it + 1
text = copy.deepcopy(pages['list'][list_it]['parameters'])
while pages['list'][list_it_2]['code'] == 401:
text.append(pages['list'][list_it_2]['parameters'][0])
list_it_2 += 1
text = ' '.join(text)
# empty string check
if not text:
list_it = list_it_2
continue
# translate
text_tr, success = try_translate_sentence(text)
if (not success) or (text_tr is None):
print('Anomaly: {}'.format(text))
else:
try:
text_neat = print_neatly(text_tr, max_len)
except:
text_neat = text_tr
for text_it, j in enumerate(range(list_it, list_it_2)):
translations += 1
if text_it >= len(text_neat): # translated text is one row shorter
text_neat.append("")
if verbose:
print(pages['list'][j]['parameters'][0], "->", text_neat[text_it])
pages['list'][j]['parameters'][0] = text_neat[text_it]
list_it = list_it_2
else:
list_it += 1
return data, translations
def translate_neatly_common_events(file_path, tr, src='it', dst='en', verbose=False, max_len=55, max_retries=5):
def translate_sentence(text):
target = text
translation = tr.translate(target, src=src, dest=dst).text
if target[0].isalpha() and translation[0].isalpha and not target[0].isupper():
translation = translation[0].lower() + translation[1:]
text = translation
return text
translations = 0
with open(file_path, 'r', encoding='utf-8-sig') as datafile:
data = json.load(datafile)
num_ids = len([e for e in data if e is not None])
i = 0
for d in data:
if d is not None:
print('{}: {}/{}'.format(file_path, i+1, num_ids))
i += 1
list_it = 0
len_list = len(d['list'])
while list_it < len_list:
if 'code' in d['list'][list_it].keys() and d['list'][list_it]['code'] == 401:
list_it_2 = list_it + 1
text = copy.deepcopy(d['list'][list_it]['parameters'])
while 'code' in d['list'][list_it].keys() and d['list'][list_it_2]['code'] == 401:
text.append(d['list'][list_it_2]['parameters'][0])
list_it_2 += 1
text = ' '.join(text)
# empty string check
if not text:
list_it = list_it_2
continue
text_tr = None
try:
text_tr = translate_sentence(text)
except:
for _ in range(max_retries):
try:
time.sleep(1)
text_tr = translate_sentence(text)
except:
pass
if text_tr is not None:
break
if text_tr is None:
print('Anomaly: {}'.format(text))
else:
try:
text_neat = print_neatly(text_tr, max_len)
except:
text_neat = text_tr
for text_it, j in enumerate(range(list_it, list_it_2)):
translations += 1
if text_it >= len(text_neat):
text_neat.append("")
if verbose:
print(d['list'][j]['parameters'][0], "->", text_neat[text_it])
d['list'][j]['parameters'][0] = text_neat[text_it]
list_it = list_it_2
else:
list_it += 1
return data, translations
# usage: python dialogs_translator.py --print_neatly --source_lang it --dest_lang en
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input_folder", type=str, default="dialogs")
ap.add_argument("-sl", "--source_lang", type=str, default="it")
ap.add_argument("-dl", "--dest_lang", type=str, default="en")
ap.add_argument("-v", "--verbose", action="store_true", default=False)
ap.add_argument("-nf", "--no_format", action="store_true", default=False)
ap.add_argument("-pn", "--print_neatly", action="store_true", default=False)
ap.add_argument("-ml", "--max_len", type=int, default=44)
ap.add_argument("-mr", "--max_retries", type=int, default=10)
args = ap.parse_args()
dest_folder = args.input_folder + '_' + args.dest_lang
translations = 0
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
for file in os.listdir(args.input_folder):
file_path = os.path.join(args.input_folder, file)
if os.path.isfile(os.path.join(dest_folder, file)):
print('skipped file {} because it has already been translated'.format(file_path))
continue
if file.endswith('.json'):
print('translating file: {}'.format(file_path))
if file.startswith('Map'):
if args.print_neatly:
new_data, t = translate_neatly(file_path, tr=Translator(), max_len=args.max_len,
src=args.source_lang, dst=args.dest_lang, verbose=args.verbose,
max_retries=args.max_retries)
else:
new_data, t = translate(file_path, tr=Translator(),
src=args.source_lang, dst=args.dest_lang, verbose=args.verbose,
max_retries=args.max_retries)
elif file.startswith('CommonEvents'):
new_data, t = translate_neatly_common_events(file_path, tr=Translator(), max_len=args.max_len,
src=args.source_lang, dst=args.dest_lang, verbose=args.verbose,
max_retries=args.max_retries)
translations += t
new_file = os.path.join(dest_folder, file)
with open(new_file, 'w', encoding='utf-8') as f:
if not args.no_format:
json.dump(new_data, f, indent=4, ensure_ascii=False)
else:
json.dump(new_data, f, ensure_ascii=False)
print('\ndone! translated in total {} dialog windows'.format(translations))