forked from CogComp/KAIROS-Event-Extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_input.py
155 lines (135 loc) · 6.53 KB
/
preprocess_input.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
import re
from time import time
import regex
from nltk import sent_tokenize, word_tokenize
special_char_list = ["'", ",", ";", ":", "-", "?", "!", "$", "%", "#", "_", "&", "~", "|", "^", "+", "*", "/", "<", "=", ">", "(", ")", "{", "}", "[", "]"]
def replacer(match):
if match.group(1) is not None:
return '{} '.format(match.group(1))
else:
return ' {}'.format(match.group(2))
def preprocess_text(text):
rx = re.compile(r'^(\W+)|(\W+)$')
text = " ".join([rx.sub(replacer, word) for word in raw_text.split()])
text = re.sub("'", " ' ", text)
text = re.sub("\t+", " ", text)
text = re.sub("\s+", " ", text)
text = re.sub("\n+", " ", text)
return text
def preprocess_input_text(input_text="", multi=False, special_char="remove", text_mod=True, char_list=[]):
# start_time = time()
input_text = input_text.encode('utf-8').decode("utf-8")
# special_char_list = ["!", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "]", "^", "_", "`", "{", "|", "}", "~"]
if multi:
if "\n" in input_text:
input_text = re.sub("\n+", " ", input_text)
if "\t" in input_text:
input_text = re.sub("\t+", " ", input_text)
if special_char=="remove":
input_text = regex.sub(r'[^\p{Latin}]', u' ', input_text)
elif special_char=="convert" :
if "’" in input_text:
input_text = re.sub("\’", " ' ", input_text)
# input_text = re.sub("\’", " ", input_text)
if "‘" in input_text:
input_text = re.sub("\‘", " ' ", input_text)
# input_text = re.sub("\‘", " ", input_text)
if "“" in input_text:
input_text = re.sub("“", " \" ", input_text)
# input_text = re.sub("“", " ", input_text)
if "”" in input_text:
input_text = re.sub("”", " \" ", input_text)
# input_text = re.sub("”", " ", input_text)
if "—" in input_text:
input_text = re.sub("—", " - ", input_text)
if "–" in input_text:
input_text = re.sub("–", " - ", input_text)
if "…" in input_text:
input_text = re.sub("…", " . ", input_text)
# if "." in input_text:
# input_text = re.sub(".", " . ", input_text)
if char_list:
if "'" in input_text and "'" in char_list:
input_text = re.sub("'", " ' ", input_text)
# input_text = re.sub("'", " ", input_text)
if "," in input_text and "," in char_list:
input_text = re.sub(",", " , ", input_text)
# input_text = re.sub(",", " ", input_text)
if ";" in input_text and ";" in char_list:
input_text = re.sub(";", " ; ", input_text)
# input_text = re.sub(";", " ", input_text)
if ":" in input_text and ":" in char_list:
input_text = re.sub(":", " : ", input_text)
# input_text = re.sub(":", " ", input_text)
if "-" in input_text and "-" in char_list:
input_text = re.sub("-+", " - ", input_text)
if "?" in input_text and "?" in char_list:
input_text = re.sub("\?+", " ? ", input_text)
if "!" in input_text and "!" in char_list:
input_text = re.sub("!+", " ! ", input_text)
if "$" in input_text and "$" in char_list:
input_text = re.sub("\$", " $ ", input_text)
if "%" in input_text and "%" in char_list:
input_text = re.sub("%", " % ", input_text)
if "#" in input_text and "#" in char_list:
input_text = re.sub("#", " # ", input_text)
if "_" in input_text and "_" in char_list:
input_text = re.sub("_", " _ ", input_text)
if "&" in input_text and "&" in char_list:
input_text = re.sub("&", " & ", input_text)
if "~" in input_text and "~" in char_list:
input_text = re.sub("~", " ~ ", input_text)
if "|" in input_text and "|" in char_list:
input_text = re.sub("|", " | ", input_text)
if "^" in input_text and "^" in char_list:
input_text = re.sub("\^", " ^ ", input_text)
if "+" in input_text and "+" in char_list:
input_text = re.sub("\+", " + ", input_text)
if "*" in input_text and "*" in char_list:
input_text = re.sub("\*", " * ", input_text)
if "/" in input_text and "/" in char_list:
input_text = re.sub("/", " / ", input_text)
if "<" in input_text and "<" in char_list:
input_text = re.sub("<", " < ", input_text)
if "=" in input_text and "=" in char_list:
input_text = re.sub("=", " = ", input_text)
if ">" in input_text and ">" in char_list:
input_text = re.sub(">", " > ", input_text)
if "(" in input_text and "(" in char_list:
input_text = re.sub("\(", " ( ", input_text)
if ")" in input_text and ")" in char_list:
input_text = re.sub("\)", " ) ", input_text)
if "{" in input_text and "{" in char_list:
input_text = re.sub("{", " { ", input_text)
if "}" in input_text and "}" in char_list:
input_text = re.sub("}", " } ", input_text)
if "[" in input_text and "[" in char_list:
input_text = re.sub("\[", " [ ", input_text)
if "]" in input_text and "]" in char_list:
input_text = re.sub("\]", " ] ", input_text)
if text_mod:
if "p.m" in input_text:
input_text = re.sub("p.m", "pm", input_text)
if "p.m." in input_text:
input_text = re.sub("p.m.", "pm", input_text)
if "P.M" in input_text:
input_text = re.sub("P.M", "PM", input_text)
if "P.M." in input_text:
input_text = re.sub("P.M.", "PM", input_text)
if "a.m" in input_text:
input_text = re.sub("a.m", "am", input_text)
if "a.m." in input_text:
input_text = re.sub("a.m.", "am", input_text)
if "A.M" in input_text:
input_text = re.sub("A.M", "AM", input_text)
if "A.M." in input_text:
input_text = re.sub("A.M.", "AM", input_text)
# input_text = regex.sub(r'[^\p{Latin}]', u' ', input_text)
input_text = re.sub("\s+", " ", input_text).strip()
# print("input_text[-1] : ", input_text[-1])
# if not input_text[-1] == '.':
# input_text = input_text + "."
end_time = time()
# print("***Processing Time (preprocessing): ", time() - start_time)
# print("**", input_text)
return input_text