-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
189 lines (156 loc) · 5.44 KB
/
utils.py
File metadata and controls
189 lines (156 loc) · 5.44 KB
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
import yaml
from typing import Dict
import re
def load_config(path: str) -> Dict:
'''
Load config from YAML file
Args:
- path (str): path to YAML file
Returns: (dict) config
'''
with open(path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
return config
def multiple_replace(dict: Dict, text: str) -> str:
'''
Replace multiple characters in a string
Args:
- dict (Dict): dictionary of characters to be replaced
- text (str): string to be replaced
Returns: replaced string
'''
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
return regex.sub(lambda mo: dict[mo.string[mo.start() : mo.end()]], text)
def preprocess_text(input: str) -> str:
'''
Preprocess input sentence to feed into model
Args:
- input (str): input sentence
Returns: (str) input sentence
'''
def check_mrs(input, i):
is_mr = (i >= 2 and
input[i-2:i].lower() in ['mr', 'ms'] and
(i < 3 or input[i-3] == ' '))
is_mrs = (i >= 3 and
input[i-3:i].lower() == 'mrs' and
(i < 4 or input[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()
# if input[-1].isdigit() or input[-1].lower().isalpha():
input += '.'
# 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))
new_input = ''
for i, char in enumerate(input):
if char == '&' and (input[i:i+5] == '&' or
input[i:i+6] == '"' or
input[i:i+6] == ''' or
input[i:i+5] == ']' or
input[i:i+5] == '['):
new_input += char
continue
new_input += replace_dict.get(char, char)
input = new_input
# Second step: add spaces
check_sp_list = [',', '?', '!', ''', '&', '"', '[',
']', '-', '/', '%', ':', '$', '#', '&', '*', ';', '=', '+', '@', '~', '>', '<']
new_input = ''
i = 0
while i < len(input):
char = input[i]
found = False
for string in check_sp_list:
if string == input[i: i+len(string)]:
new_input += ' ' + string
if string != ''':
new_input += ' '
i += len(string)
found = True
break
if not found:
new_input += char
i += 1
input = new_input
new_input = ''
for i, char in enumerate(input):
if char != '.':
new_input += char
continue
elif check_mrs(input, i):
# case 1: Mr. Mrs. Ms.
new_input += '. '
elif check_ABB_mid(input, i):
# case 2: U[.]S.A.
new_input += '.'
elif check_ABB_end(input, i):
# case 3: U.S.A[.]
new_input += '. '
else:
new_input += ' . '
input = new_input
# Thrid step: remove not necessary spaces.
input = re.sub('\s+', ' ', input)
return input
def split_text_by_sens(text: str, max_len: int = 200):
'''
Split the input text by sentences. Each sentence is not longer than max_len words
Args:
- text (str): The text to split
- max_len (int): The maximum length of the sentence (default to 200)
Returns: List[str]: List of sentences
'''
text = text.strip()
text = re.sub(r"\.+", ".", text)
sens = text.split('.')
new_sens = []
for sen in sens:
sen = sen.strip()
words = sen.split(' ')
if len(sen) == 0 or len(words) == 0:
continue
if len(words) <= max_len:
new_sens.append(sen)
else:
raise Exception("The sentence is too long!")
return new_sens
def postprocess_text(output: str) -> str:
'''
Post process output sentence from model to display.
Args:
- output (str): output sentence from model
Returns: (str) output sentence
'''
to_text = output
to_text = re.sub('\s+', ' ', to_text)
to_text = to_text.replace('<EOS>', '').replace('<pad>', '')
to_text = to_text.replace('& quot ;', '"')
to_text = to_text.replace(' : ', ': ')
to_text = to_text.replace('& apos ;', "'")
to_text = to_text.replace('& # 91 ; ', "(")
to_text = to_text.replace(' & # 93 ;', ")")
to_text = to_text.split('\\')[0].strip()
return to_text