-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
307 lines (262 loc) · 13.5 KB
/
preprocess.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
import logging
# https://universaldependencies.org/docsv1/en/overview/tokenization.html
# https://github.com/UniversalDependencies/UD_English-EWT/issues/1
# LRB (Left Round Bracket) and RRB (Right Round Brackets)
# -- https://stackoverflow.com/questions/37536068/removing-special-characters-from-txt-file-give-lrb-lsb-rsb-lrb
# removed -LRB- and -RRB- by assuming, the above definitions are applied here as well.
# singularize the mention
def preprocess(mention, inflect_engine, nlp):
singular_mention = inflect_engine.singular_noun(mention)
# for the cases like "actress" or "a focus", cross-check with stanza's feats of 'Number=Plur'
feats = nlp(mention).sentences[0].words[-1].feats
if (mention[-1] == 's' or mention[-1] == 'S') and feats and 'Number=Plur' in feats.split('|') and \
singular_mention:
return singular_mention
elif mention[-1] != 's' and mention[-1] != 'S' and singular_mention:
return singular_mention
return mention
# nlp = stanza.Pipeline(lang='en', processors='tokenize,pos')
# tried for n=1,2,3
def get_first_ngram_of_mentions(mentions, n, inflect_engine, nlp, apply_preprocess=True):
# ngram search
# based on the info here: https://stackoverflow.com/questions/13423919/computing-n-grams-using-python
# https://stackoverflow.com/questions/32441605/generating-ngrams-unigrams-bigrams-etc-from-a-large-corpus-of-txt-files-and-t
# I used nltk -- https://www.nltk.org/api/nltk.util.html#nltk.util.ngrams
from nltk.util import ngrams
from string import punctuation
processed_mentions = {}
# 1) filter punctuations: for mentions, like "the building , a violation of the Clean Air Act"
# 2) -LRB-, -RRB-: "the settlement Tubabodaga -LRB- '' village of the whites '' -RRB-"
# 3) --: "present Erwin Arnada -- now at large --"
# 4) '': "the percentage of Americans who do `` real exercise to build the heart ''"
filter_list = [item for item in punctuation]
filter_list.extend(["-LRB-", "-RRB-", "--", "''", "``"])
mentions = set(mentions)
count_less, count_assertion_err = 0, 0
for mention in mentions:
if len(mention.split()) == 1:
search_token = mention
# multi-token
else:
mention_ = [token for token in mention.split() if token not in filter_list]
ngram_list = list(ngrams(mention_, n))
if n == 1:
# check if ngram_list is not empty, when n==1
assert ngram_list
assert len(ngram_list[-1]) == n
# remove a-an-the to prevent that the search token is just "a/an/the",
# for the mentions like "the piano"
# Note that [0] index is taken, since when n=1, the ngrams are like
# "golf gear" --> [('golf',), ('gear',)]
if not ngram_list[0][0] in ['a', 'an', 'the', 'A', 'An', 'The']:
search_token = ngram_list[0][0]
else:
search_token = ngram_list[1][0]
else:
if n == 3 and not ngram_list:
# if n=3, and the mentions length is 2, we take the mention as it is.
search_token = ' '.join(mention_)
count_less += 1
else:
# check if ngram_list is not empty, when n==2
assert ngram_list
assert len(ngram_list[0]) == n
search_token = ' '.join(ngram_list[0])
try:
assert len(search_token.split()) == n
except AssertionError:
count_assertion_err += 1
# check if search_token is assigned
assert search_token
if apply_preprocess:
search_token = preprocess(mention=search_token, inflect_engine=inflect_engine, nlp=nlp)
processed_mentions[mention] = search_token
assert len(processed_mentions) == len(mentions)
assert count_assertion_err == count_less
return processed_mentions
# nlp = stanza.Pipeline(lang='en', processors='tokenize,pos')
# tried for n=1,2,3
def get_last_ngram_of_mentions(mentions, n, inflect_engine, nlp, apply_preprocess=True):
# ngram search
# based on the info here: https://stackoverflow.com/questions/13423919/computing-n-grams-using-python
# https://stackoverflow.com/questions/32441605/generating-ngrams-unigrams-bigrams-etc-from-a-large-corpus-of-txt-files-and-t
# I used nltk -- https://www.nltk.org/api/nltk.util.html#nltk.util.ngrams
from nltk.util import ngrams
from string import punctuation
processed_mentions = {}
# 1) filter punctuations for mentions, like
# "a parish church in the Church of England in Willoughby on the Wolds , Nottinghamshire , England"
# 2) -LRB-, -RRB-, "the settlement Tubabodaga -LRB- '' village of the whites '' -RRB-"
# 3) -- "present Erwin Arnada -- now at large --"
# 4) '' "the percentage of Americans who do `` real exercise to build the heart ''"
filter_list = [item for item in punctuation]
filter_list.extend(["-LRB-", "-RRB-", "--", "''", "``"])
mentions = set(mentions)
count_less, count_assertion_err = 0, 0
for mention in mentions:
if len(mention.split()) == 1:
search_token = mention
# multi-token
else:
mention_ = [token for token in mention.split() if token not in filter_list]
ngram_list = list(ngrams(mention_, n))
if n == 1:
# check if ngram_list is not empty, when n==1
assert ngram_list
assert len(ngram_list[-1]) == n
search_token = ngram_list[-1][0]
else:
if n == 3 and not ngram_list:
# if n=3, and the mentions length is 2, we take the mention as it is.
search_token = ' '.join(mention_)
count_less += 1
else:
assert ngram_list
assert len(ngram_list[-1]) == n
search_token = ' '.join(ngram_list[-1])
try:
assert len(search_token.split()) == n
except AssertionError:
count_assertion_err += 1
# check if search_token is assigned
assert search_token
if apply_preprocess:
search_token = preprocess(mention=search_token, inflect_engine=inflect_engine, nlp=nlp)
processed_mentions[mention] = search_token
assert len(processed_mentions) == len(mentions)
assert count_assertion_err == count_less
return processed_mentions
# nlp = stanza.Pipeline(lang='en', processors='tokenize,pos')
# tried for n=1,2,3
def get_first_ngram_of_mentions_sklearn(mentions, n, inflect_engine, nlp, apply_preprocess=True):
# https://stackoverflow.com/questions/13423919/computing-n-grams-using-python
# https://practicaldatascience.co.uk/machine-learning/how-to-use-count-vectorization-for-n-gram-analysis
from sklearn.feature_extraction.text import CountVectorizer
processed_mentions = {}
vector = CountVectorizer(ngram_range=(n, n), lowercase=False)
analyser = vector.build_analyzer()
# since it can filter the punctuation itself, we did not add punctuation
filter_list = ["-LRB-", "-RRB-"]
mentions = set(mentions)
count_less, count_more, count_assertion_err = 0, 0, 0
for mention in mentions:
if len(mention.split()) == 1:
search_token = mention
# multi-token
else:
mention_ = ' '.join([token for token in mention.split() if token not in filter_list])
ngram_list = analyser(mention_)
if n == 1:
# check if ngram_list is not empty, when n==1
assert ngram_list
assert len(ngram_list[-1].split()) == n
# remove an-the to prevent that the search token is just "an/the",
# note: "a" is already removed in sklearn, since its length < 2. 'token_pattern' --
# https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html
# for the mentions like "the piano"
if not ngram_list[0] in ['an', 'the', 'An', 'The']:
search_token = ngram_list[0]
else:
try:
search_token = ngram_list[1]
# for the mentions like 'the U.S.', U.S. removed and only 'the' remained
# for these cases, we take the mention as it is.
except IndexError:
search_token = mention_
count_more += 1
else:
if not ngram_list:
# if n=2,3, and no ngrams with this length is produced, we take the mention as it is.
search_token = mention_
# for cases, like 'a spot', 'the U.S.'; it may not return anything
if len(mention_.split()) == n:
count_less -= 1
count_less += 1
else:
assert len(ngram_list[0].split()) == n
search_token = ngram_list[0]
try:
assert len(search_token.split()) == n
except AssertionError:
count_assertion_err += 1
# check if search_token is assigned
assert search_token
if apply_preprocess:
search_token = preprocess(mention=search_token, inflect_engine=inflect_engine, nlp=nlp)
processed_mentions[mention] = search_token
assert len(processed_mentions) == len(mentions)
# we also need to take care of "count_more" due to IndexError
assert count_assertion_err == count_less + count_more
return processed_mentions
# nlp = stanza.Pipeline(lang='en', processors='tokenize,pos')
# tried for n=1,2,3
def get_last_ngram_of_mentions_sklearn(mentions, n, inflect_engine, nlp, apply_preprocess=True):
# https://stackoverflow.com/questions/13423919/computing-n-grams-using-python
# https://practicaldatascience.co.uk/machine-learning/how-to-use-count-vectorization-for-n-gram-analysis
# https://datascience.stackexchange.com/questions/38167/how-to-use-build-analyzer-in-sklearn-feature-extraction
from sklearn.feature_extraction.text import CountVectorizer
processed_mentions = {}
vector = CountVectorizer(ngram_range=(n, n), lowercase=False)
analyser = vector.build_analyzer()
# since it can filter the punctuation itself, we did not add punctuation
filter_list = ["-LRB-", "-RRB-"]
mentions = set(mentions)
count_less, count_assertion_err = 0, 0
for mention in mentions:
if len(mention.split()) == 1:
search_token = mention
# multi-token
else:
mention_ = ' '.join([token for token in mention.split() if token not in filter_list])
ngram_list = analyser(mention_)
if n == 1:
# check if ngram_list is not empty, when n==1
assert ngram_list
assert len(ngram_list[-1].split()) == n
search_token = ngram_list[-1]
else:
if not ngram_list:
# if n=2,3, and no ngrams with this length is produced, we take the mention as it is.
search_token = mention_
# for cases, like 'a spot', 'the U.S.'; it may not return anything
if len(mention_.split()) == n:
count_less -= 1
count_less += 1
else:
assert len(ngram_list[-1].split()) == n
search_token = ngram_list[-1]
try:
assert len(search_token.split()) == n
except AssertionError:
count_assertion_err += 1
# check if search_token is assigned
assert search_token
if apply_preprocess:
search_token = preprocess(mention=search_token, inflect_engine=inflect_engine, nlp=nlp)
processed_mentions[mention] = search_token
assert len(processed_mentions) == len(mentions)
assert count_assertion_err == count_less
return processed_mentions
# tried head words using stanza library
# nlp should be initialized: nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,lemma,depparse')
# https://stanfordnlp.github.io/stanza/depparse.html
def get_headword_of_mentions(mentions, inflect_engine, nlp, apply_preprocess=True):
processed_mentions = {}
count_multiple_heads = 0
mentions = set(mentions)
for mention in mentions:
doc = nlp(mention)
root_word = [word for sent in doc.sentences for word in sent.words if word.head == 0]
if len(root_word) > 1:
count_multiple_heads += 1
logging.info('More than one head word found, the first one is selected: ' + mention
+ ' ' + root_word[0].text)
assert root_word[0].deprel == "root"
assert root_word[0].head == 0
if apply_preprocess:
# singularize
processed_mentions[mention] = preprocess(mention=root_word[0].text, inflect_engine=inflect_engine, nlp=nlp)
else:
processed_mentions[mention] = root_word[0].text
logging.info('Total number of mentions with more than one head is: ' + str(count_multiple_heads))
return processed_mentions, count_multiple_heads