-
Notifications
You must be signed in to change notification settings - Fork 33
/
tokenizer.py
326 lines (271 loc) · 8.26 KB
/
tokenizer.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import collections
import math
import os
import re
import sys
import time
import six
from datetime import datetime
import tqdm
def read_nonempty(filename):
with open(filename, 'r') as file:
return [line.strip() for line in file.readlines()
if line.strip() not in ['', '.']]
def check_mrs(content, i):
is_mr = (i >= 2 and
content[i-2:i].lower() in ['mr', 'ms'] and
(i < 3 or content[i-3] == ' '))
is_mrs = (i >= 3 and
content[i-3:i].lower() == 'mrs' and
(i < 4 or content[i-4] == ' '))
return is_mr or is_mrs
def check_ABB_mid(content, i):
if i <= 0:
return False
if i >= len(content)-1:
return False
l, r = content[i-1], content[i+1]
return l.isupper() and r.isupper()
def check_ABB_end(content, i):
if i <= 0:
return False
l = content[i-1]
return l.isupper()
def fix_file(filename):
if not os.path.exists(filename + '.fixed'):
with open(filename, 'r') as file:
contents = file.read()
contents = fix_contents(contents)
with open(filename+'.fixed', 'w') as file:
file.write(contents)
return filename + '.fixed'
def fix_contents(contents):
# first step: replace special characters
check_list = ['\uFE16', '\uFE15', '\u0027','\u2018', '\u2019',
'“', '”', '\u3164', '\u1160',
'\u0022', '\u201c', '\u201d', '"',
'[', '\ufe47', '(', '\u208d',
']', '\ufe48', ')' , '\u208e',
'—', '_', '–', '&']
alter_chars = ['?', '!', ''', ''', ''',
'"', '"', '"', '"',
'"', '"', '"', '"',
'[', '[', '[', '[',
']', ']', ']', ']',
'-', '-', '-', '&']
replace_dict = dict(zip(check_list, alter_chars))
print('[1/4]')
new_contents = ''
for i, char in tqdm.tqdm(enumerate(contents), total=len(contents)):
if char == '&' and (contents[i:i+5] == '&' or
contents[i:i+6] == '"' or
contents[i:i+6] == ''' or
contents[i:i+5] == ']' or
contents[i:i+5] == '['):
new_contents += char
continue
new_contents += replace_dict.get(char, char)
contents = new_contents
# second: add spaces
check_sp_list = [',', '?', '!', ''', '&', '"', '[',
']', '-', '/', '%', ':', '$', '#', '&', '*', ';', '=', '+', '$', '#', '@', '~', '>', '<']
print('[2/4]')
new_contents = ''
i = 0
l100 = len(contents)//100
while i < len(contents):
if i // l100 > (i-1) // l100:
sys.stdout.write(str(i // l100) + '%')
sys.stdout.flush()
char = contents[i]
found = False
for string in check_sp_list:
if string == contents[i: i+len(string)]:
new_contents += ' ' + string
if string != ''':
new_contents += ' '
i += len(string)
found = True
break
if not found:
new_contents += char
i += 1
contents = new_contents
print('[3/4]')
new_contents = ''
for i, char in tqdm.tqdm(enumerate(contents), total=len(contents)):
if char != '.':
new_contents += char
continue
elif check_mrs(contents, i):
# case 1: Mr. Mrs. Ms.
new_contents += '. '
elif check_ABB_mid(contents, i):
# case 2: U[.]S.A.
new_contents += '.'
elif check_ABB_end(contents, i):
# case 3: U.S.A[.]
new_contents += '. '
else:
new_contents += ' . '
contents = new_contents
# third: remove not necessary spaces.
print('[4/4]')
new_contents = ''
for char in tqdm.tqdm(contents):
if new_contents and new_contents[-1] == ' ' and char == ' ':
continue
new_contents += char
contents = new_contents
return contents.strip()
def is_number(char):
chars = {'0':1, '1':1, '2':1, '3':1, '4':1,
'5':1, '6':1, '7':1, '8':1, '9':1}
if char in chars:
return True
return False
def test_isnumber():
string = ' 39. 8 %, 15. 1% ; 28% ; 38. 7% ; and 26. 9% ' \
+ 'respectively. The prevalences of osteopenia at ' \
+ 'those ROI are 54. 8% ; 46. 3% ; 60. 2% ; 45. 2% ' \
+ 'and 62. 7% respectively .'
for char in string:
if is_number(char):
print(char)
# test_isnumber()
def is_upper(char):
if char == char.upper():
return True
return False
def is_char(char):
chars = {'a':1, 'b':1, 'c':1, 'd':1, 'e':1, 'f':1,
'g':1, 'h':1, 'i':1, 'j':1, 'k':1, 'l':1,
'm':1, 'n':1, 'o':1, 'p':1, 'q':1, 'r':1,
's':1, 't':1, 'u':1, 'v':1, 'w':1, 'x':1,
'y':1, 'z':1}
if char in chars:
return True
return False
def is_end_of_quote(char):
if not is_char(char) or not is_upper(char):
return True
return False
def unfix_contents(contents):
check_list = [''', '"', '[',
']', '<', '>',
' . ', ' , ',
'&'
]
alter_chars = ['\u0027', '"', '(',
')', '<', '>',
'. ', ', ',
'&']
replace_dict = dict(zip(check_list, alter_chars))
new_contents = ''
i = 0
while i < len(contents):
char = contents[i]
found = False
for string in replace_dict:
if string == contents[i: i+len(string)]:
new_contents += replace_dict[string]
i += len(string)
found = True
break
if not found:
new_contents += char
i += 1
contents = new_contents
#fix n 't
check_list1 = ["n 't ", " 've ", " 'd ", " 'm ",
" 're ", " 's ", " 'll ", "s ' ",
' ( ',' ) ', ' ; ', ' % ', ' / ', ' : ',
' + - ', ' - ', ' ? ' ]
alter_chars1 = ["n't ", "'ve ", "'d ", "'m ",
"'re ", "'s ", "'ll ", "s' ",
' (',') ', '; ', '% ', '/', ': ',
' +- ', '-', '? ']
new_contents = ''
replace_dict1 = dict(zip(check_list1, alter_chars1))
i = 0
while i < len(contents):
char = contents[i]
found = False
for string in replace_dict1:
if string == contents[i: i+len(string)]:
new_contents += replace_dict1[string]
i += len(string)
found = True
break
if not found:
new_contents += char
i += 1
contents = new_contents
# rm space in number eg: 4. 5 -> 4.5 or 4, 6 -> 4,6 //how abt 12. 2. 1. ... ?
i = 0
new_contents = ''
while i < len(contents):
char = contents[i]
found = False
if char == '.' or char == ',':
if is_number(contents[i-1]) and \
contents[i+1] == ' ' and \
is_number(contents[i+2]):
new_contents += (char + contents[i+2])
i += 3
found = True
if not found:
new_contents += char
i += 1
contents = new_contents
#rm ' .', ' ?', ' !', ' ;'
i = 0
new_contents = ''
while i < len(contents):
char = contents[i]
found = False
fixchars = ['.', '!', '?', ';']
if char == ' ' and contents[i+1] in fixchars:
found = True
new_contents += contents[i+1]
i += 2
if not found:
new_contents += char
i += 1
contents = new_contents
#rm E. coli
i = 0
new_contents = ''
while i < len(contents):
char = contents[i]
found = False
#end quote
if i < len(contents) - 2 and char == '.' and \
is_upper(contents[i-1]) and \
contents[i+1] == ' ' and \
not is_upper(contents[i+2]):
found = True
new_contents += char + contents[i+2]
i += 3
if not found:
new_contents += char
i += 1
contents = new_contents
return contents.strip()
def unfix_file(fname):
if not fname.startswith(os.getcwd()):
org_place = os.getcwd() + '/'
saving_place = os.getcwd() + '/'
else:
org_place = ''
saving_place = ''
if not os.path.exists(saving_place + fname + '.unfix'):
print('unfixing ', fname)
with open(org_place + fname, 'r') as file:
contents = file.read()
contents = unfix_contents(contents)
with open(saving_place + fname + '.unfix', 'w') as file:
file.write(contents)
return fname + '.unfix'
else:
print('Already done!')