-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
292 lines (262 loc) · 9.67 KB
/
main.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
import os
import sys
import re
import getopt
from time import time
# from googletrans import Translator # !!googletrans-py, instead of googletrans!!
from crawl import translate
def process_trans(translated: str) -> str:
translated = translated.replace('#', '#')
if translated.startswith('#'):
i = 0
while i < len(translated):
if translated[i] == '#':
i += 1
else:
if translated[i] != ' ':
translated = translated[:i] + ' ' + translated[i:]
break
return translated
def trans_write(ot_buf: str, tr_buf: str) -> None:
if tr_buf[-1] == ' ':
tr_buf = tr_buf[:-1]
# translation = translator.translate(tr_buf, src='en', dest='zh-cn')
# translated = translation.text
translated = translate(tr_buf, text_language='en', to_language='zh-CN')
if translated == '' and tr_buf != '':
print('Translation Exception: Empty Result (Currently at Passage {})'.format(trans_cnt))
print('Original text:')
print(tr_buf)
translated = process_trans(translated)
ot_buf = '<!--\n' + ot_buf + ' -->\n' # add comment mark
ot_buf += translated # append translated text
if ot_buf[-1] != '\n':
ot_buf += '\n'
fo.write(ot_buf + '\n') # write to fo
print('Time elapsed: {:.2f} secs'.format(time() - t1))
def is_note(line: str, pos: int, check_path=False) -> bool:
if line[pos] == '[':
while pos < len(line) and line[pos] != ']':
pos += 1
if pos + 2 < len(line) and line[pos:pos + 3] == ']: ':
if check_path:
if pos + 3 < len(line) and line[pos + 3] in ('/', '.', 'h'):
return True
else:
return False
return True
return False
def is_new(line: str) -> bool:
'''
Check if a new line needs wrapping
'''
line_mark = '+-*>'
line_break = False
i = 0
while i < len(line) and line[i] == ' ':
i += 1
# i -> non-space: if line[i] is a md-line-mark, omit; or restore the linebreak
if i < len(line) and tr_buf != '' and tr_buf[-1] == ' ':
# "#...# " needs a linebreak
if line[i] == '#':
while i < len(line) and line[i] == '#':
i += 1
if i < len(line) and line[i] == ' ':
line_break = True
# "1. ", "2. ", etc. need a linebreak too
elif line[i].isdigit():
while i < len(line) and line[i].isdigit():
i += 1
if i + 1 < len(line) and line[i:i + 2] == '. ':
line_break = True
# "[...]: " needs a linebreak too
elif is_note(line, i):
line_break = True
# "- ", "* ", etc. need a linebreak too
elif len(line) - i > 1:
for ch in line_mark:
if line[i:i + 2] == ch + ' ':
line_break = True
break
return line_break
def buf_add(tr_buf: str, line: str) -> str:
'''
Add new line from file to buf, unwrapping the linebreaks if needed
'''
if is_new(line):
tr_buf = tr_buf[:-1] + '\n'
tr_buf += line
# For every new line, change the ending from linebreak to space
if tr_buf[-1] == '\n':
tr_buf = tr_buf[:-1] + ' '
return tr_buf
if __name__ == '__main__':
t0 = time()
# Deal with arguments
try:
opts, args = getopt.getopt(sys.argv[1:], '-i:-o:-p:', ['input=', 'output=', 'proxy='])
except getopt.GetoptError:
print('Arguments Error (Unknown arguments)')
exit(0)
input_path = ''
output_path = ''
proxies = None
for opt, arg in opts:
if opt in ('-i', '--input'):
input_path = os.path.expanduser(arg)
elif opt in ('-o', '--output'):
output_path = os.path.expanduser(arg)
elif opt in ('-p', '--proxy'):
proxies = {'http': arg, 'https': arg}
if input_path == '':
print('Argument -i Error')
exit(0)
if output_path == '':
output_path = input_path + '.tmp'
if proxies is None:
print('Proxy unset. If there\'s empty translation, please check\n'
'your connection to https://translate.google.com/')
# translator = Translator(service_urls=['translate.google.com', ], proxies=proxies)
# Deal with files
fi = open(input_path, 'r')
fo = open(output_path, 'w')
ot_buf = '' # output text
tr_buf = '' # text to translate
code_area = False
ref_area = False
comment_area = False
trans_cnt = 1
code_cnt = 1
ref_cnt = 1
comment_cnt = 1
t1 = time()
for line in fi:
# Close ref area if needed before latent wrong recognitions
if comment_area:
comment_end = line.find('-->')
if comment_end != -1:
comment_end += 3
ot_buf += line[:comment_end]
fo.write(ot_buf)
comment_area = False
print('Time elapsed: {:.2f} secs'.format(time() - t1))
ot_buf = line[comment_end:]
if ot_buf.isspace() or ot_buf == '':
fo.write('\n')
ot_buf = ''
else: # Replacing 'normal start'
tr_buf = buf_add(tr_buf, ot_buf)
print('Translating Passage {}'.format(trans_cnt), end=', ')
t1 = time()
continue
ot_buf += line
continue
# Close ref area if needed to avoid a code block RIGHT after a ref area
if ref_area:
if is_note(line, pos=0, check_path=True):
fo.write(line)
continue
else:
print('Time elapsed: {:.2f} secs'.format(time() - t1))
ref_area = False
# Deal with html comments
if re.match(r'^\s*<!--', line) is not None:
if not comment_area:
comment_area = True
print('Skipping Comment {}'.format(comment_cnt), end=', ')
comment_cnt += 1
t1 = time()
# try finding inline closing mark
comment_end = line.find('-->')
if comment_end != -1:
comment_end += 3
ot_buf += line[:comment_end]
fo.write(ot_buf)
comment_area = False
print('Time elapsed: {:.2f} secs'.format(time() - t1))
ot_buf = line[comment_end:]
if ot_buf.isspace() or ot_buf == '':
fo.write('\n')
ot_buf = ''
else: # Replacing 'normal start'
tr_buf = buf_add(tr_buf, ot_buf)
print('Translating Passage {}'.format(trans_cnt), end=', ')
t1 = time()
continue
else: # Should not be triggered
print('Comment Area Exception')
# Deal with code blocks wrapped in "```"
if re.match(r'^\s*```', line) is not None:
if not code_area:
code_area = True
print('Skipping Code Block {}'.format(code_cnt), end=', ')
code_cnt += 1
t1 = time()
else:
ot_buf += line
fo.write(ot_buf)
ot_buf = ''
code_area = False
print('Time elapsed: {:.2f} secs'.format(time() - t1))
continue
if code_area:
ot_buf += line
continue
# Deal with ref area:
if is_note(line, pos=0, check_path=True):
if tr_buf != '':
trans_write(ot_buf, tr_buf)
trans_cnt += 1
ot_buf = ''
tr_buf = ''
if not ref_area:
ref_area = True
print('Skipping Ref Block {}'.format(ref_cnt), end=', ')
ref_cnt += 1
t1 = time()
fo.write(line)
continue
# Deal with passage that doesn't need translating
if re.match(r'\s*\*\s+\{.+}\s*', line) is not None:
if tr_buf != '':
trans_write(ot_buf, tr_buf)
trans_cnt += 1
ot_buf = ''
tr_buf = ''
print('Skipping Passage {}'.format(trans_cnt), end=', ')
t1 = time()
fo.write(line)
print('Time elapsed: {:.2f} secs'.format(time() - t1))
continue
# Normal lines
# Deal with a single linebreak
if line == '\n':
if ot_buf == '':
fo.write('\n')
continue
trans_write(ot_buf, tr_buf)
trans_cnt += 1
ot_buf = ''
tr_buf = ''
continue
# Start of normal lines
if ot_buf == '':
print('Translating Passage {}'.format(trans_cnt), end=', ')
t1 = time()
ot_buf += line
# for beginning with special char, restore line break
tr_buf = buf_add(tr_buf, line)
if ot_buf != '': # Deal with remaining text when exiting
trans_write(ot_buf, tr_buf)
trans_cnt += 1
ot_buf = ''
tr_buf = ''
fi.close()
fo.close()
if output_path == input_path + '.tmp':
os.remove(input_path)
os.rename(output_path, input_path)
print('Translation completed ({} passages, skipping {} code blocks)'.format(trans_cnt - 1, code_cnt - 1))
print('Total time elapsed: {:.5f} secs'.format(time() - t0))
print('CAUTIONS: Please check the format of the translated file (especially punctuations),')
print(' and correct them manually if needed.')