-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
338 lines (285 loc) · 12.3 KB
/
preprocess.py
File metadata and controls
338 lines (285 loc) · 12.3 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
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
327
328
329
330
331
332
333
334
335
336
337
# coding=utf-8
__author__ = 'Jihyun Park'
__email__ = 'jihyunp@uci.edu'
import nltk
import re
import os
import csv
from collections import defaultdict
from sklearn.feature_extraction.text import CountVectorizer
def remove_punc(rawtext, ignore_case=False, remove_numbers=False, sub_numbers=True,
names_list=set(), locations_list=set(), is_mhddata=False):
"""
Parameters
----------
rawtext : str
ignore_case: bool
remove_numbers: bool
Removes all numbers if True
sub_numbers: bool
Substitutes all numbers to a token -num-.
if 'remove_numbers' is True, it will have no effect.
is_mhddata: bool
True if the data is MHD
False if it's from some other dataset
Returns
-------
str
"""
# from NLTK
#ending quotes
ENDING_QUOTES = [
(re.compile(r'"'), " '' "),
(re.compile(r'(\S)(\'\')'), r'\1 \2 '),
(re.compile(r"([^' ])('[sS]|'[mM]|'[dD]|') "), r"\1 \2 "),
(re.compile(r"([^' ])('ll|'LL|'re|'RE|'ve|'VE|n't|N'T) "), r"\1 \2 "),
]
# List of contractions adapted from Robert MacIntyre's tokenizer.
CONTRACTIONS2 = [re.compile(r"(?i)\b(can)(not)\b"),
re.compile(r"(?i)\b(d)('ye)\b"),
re.compile(r"(?i)\b(gim)(me)\b"),
re.compile(r"(?i)\b(gon)(na)\b"),
re.compile(r"(?i)\b(got)(ta)\b"),
re.compile(r"(?i)\b(lem)(me)\b"),
re.compile(r"(?i)\b(mor)('n)\b"),
re.compile(r"(?i)\b(wan)(na) ")]
CONTRACTIONS3 = [re.compile(r"(?i) ('t)(is)\b"),
re.compile(r"(?i) ('t)(was)\b")]
txt = re.sub(r'[\[\{\(\<]patient name[\]\}\)\>]', ' -name- ', rawtext)
# For proper names and location.
# Couldn't use string match since it might be part of other words therefore using regex
# Takes forever!! we use dict at the end
# for lc in locations_list:
# txt = re.sub(r"\b" + re.escape(lc) + r"\b", "-location-", txt)
# for n in names_list:
# txt = re.sub(r"\b" + re.escape(n) + r"\b", "-name-", txt)
# Laugh
txt = re.sub(r'[\[\{\(\<]laugh[ter]*[\]\}\)\>]', ' -laugh- ', txt)
# Delete brackets (also contents inside the brackets)
txt = re.sub(r'[\[\{\<][^\[\{\<]*[\]\}\>]', ' ', txt)
# Remove parentheses, while leaving the contents inside.
txt = re.sub(r'[\(\)]', ' ', txt)
if remove_numbers:
# remove everything except for alpha characters and ', -, ?, !, .
txt = re.sub(r'[^\.,A-Za-z\'\-\?\! ]', ' ', txt)
else:
# remove everything except for alphanumeric characters and ', -, ?, !, .
txt = re.sub(r'[^\.,A-Za-z0-9\'\-\?\! ]', ' ', txt)
# split numbers
txt = re.sub(r"([0-9]+)", r" \1 ", txt)
if sub_numbers:
txt = re.sub(r'[0-9]+', '-num-', txt)
if is_mhddata:
# Replace non-ascii characters (for now just replace one)
txt = re.sub("\x92", "'", txt)
txt = re.sub("í", "'", txt)
# Remove all NAs
txt = re.sub(r"\bNA\b", "", txt)
if ignore_case:
txt = txt.lower()
# space out the periods and commas
txt = re.sub(r"\.", " . ", txt)
txt = re.sub(r",", " , ", txt)
# split ! and ?
txt = re.sub(r'\!', ' ! ', txt)
txt = re.sub(r'\?', ' ? ', txt)
#add extra space to make things easier
txt = " " + txt + " "
# remove dashes that are used alone -- but this might be useful later
# for now we're just removing them
txt = re.sub(r' \-\-* ', ' ', txt)
# remove -- these (consecutive dashes)
txt = re.sub(r'\-\-+', ' ', txt)
# remove dashes in the words that start with a dash or end with a dash
# (Note: words that start AND end with single dashes are meaningful tokens.)
txt = re.sub(r"\-([A-Za-z\']+)\s", r"\1 ", txt)
txt = re.sub(r"\s([A-Za-z\']+)\-", r" \1", txt)
#add extra space to make things easier
txt = " " + txt + " "
# Added these two to find a match with the pre-trained words
# txt = re.sub(r'[A-Za-z]\-$', '<PARTIALWORD>', txt) # add this? probably not at the moment.
# txt = re.sub(r'\-', ' ', txt)
txt = re.sub(r"''", "'", txt)
txt = re.sub(r"\s+'\s+", " ", txt)
for regexp, substitution in ENDING_QUOTES:
txt = regexp.sub(substitution, txt)
for regexp in CONTRACTIONS2:
txt = regexp.sub(r' \1 \2 ', txt)
for regexp in CONTRACTIONS3:
txt = regexp.sub(r' \1 \2 ', txt)
txt = re.sub(r'\s+', ' ', txt) # make multiple white spaces into 1
tokenized = txt.strip().split() # tokenized temporarily to check proper nouns
for i, w in enumerate(tokenized):
if w in names_list:
tokenized[i] = '-name-'
# considers up to bigrams. First looks up if there is a match in bigrams. if not, checks unigram
if i < len(tokenized)-1 and ' '.join(tokenized[i:i+2]) in locations_list:
tokenized[i] = '-location-'
del(tokenized[i+1])
elif w in locations_list:
tokenized[i] = '-location-'
return ' '.join(tokenized)
def read_words(file_path='./stopwordlists/locations.txt', ignore_case=True):
pns = set()
if os.path.isfile(file_path):
with open(file_path, 'r') as f:
reader = csv.reader(f, delimiter=" ")
for line in reader:
w = " ".join(line)
if ignore_case:
w = w.lower()
pns.add(w)
else:
print("WARNING: file "+ file_path +" does not exist. Skipping..")
return pns
def define_vocabulary(doc, stopwords=None, token_pattern=r"(?u)[A-Za-z\?\!\-\.']+",
ngram_range=(1,2), min_dfreq=0.0001, max_dfreq=0.9,
min_np_len=2, max_np_len=3, ignore_case=False,
parser=None, stemmer_type=None, verbose=2):
vocab, stw_all, tokenizer = define_vocabulary_inner(doc, stopwords, token_pattern,
ngram_range, min_dfreq, max_dfreq)
if min_np_len > 1:
if verbose > 1:
print(" Extracting noun phrases..")
corpus_pos, np2cnt = get_noun_phrase_counts(doc, tokenizer,
min_np_len, max_np_len, stw_all,
ignore_case, parser, stemmer_type)
# vocabulary with noun phrases (min count = 5)
min_dcnt = max(5, int(min_dfreq * len(doc)))
vocab, nps = add_noun_phrases_to_vocab(vocab, np2cnt, min_dcnt)
else:
nps = set()
np2cnt = dict()
corpus_pos = []
return vocab, stw_all, np2cnt, nps, corpus_pos
def define_vocabulary_inner(doc, stopwords=None, token_pattern=r"(?u)[A-Za-z\?\!\-\.']+",
ngram_range=(1,2), min_dfreq=1e-5, max_dfreq=0.9):
"""
Tokenize the document and attach POS tag.
Also return the noun phrases from the document.
Parameters
----------
doc : list[str]
stemmer_type : str
Returns
-------
list[list[tup[str]]], set, defaultdict
"""
# This countvectorizer is used only for creating vocabulary
cntvec = CountVectorizer(ngram_range=ngram_range, stop_words=stopwords,
min_df=min_dfreq, max_df=max_dfreq, token_pattern=token_pattern)
cntvec.fit(doc)
vocabulary = cntvec.vocabulary_
stopwords_all = stopwords.union(cntvec.stop_words_)
tokenizer = cntvec.build_tokenizer()
return vocabulary, stopwords_all, tokenizer
def add_noun_phrases_to_vocab(vocabulary, np2cnt, min_dcnt):
nps = set()
npsinorder = sorted(np2cnt.keys(), key= lambda x: np2cnt[x], reverse=True)
# Save noun phrases that appeared more than min_dcnt docs
# Also add those noun phrases to the vocabulary
i = len(vocabulary)
for np in npsinorder:
# stop saving the ones that appeared less than min_dcnt documents
if np2cnt[np] < min_dcnt:
break
nps.add(np)
vocabulary[np] = i
i += 1
return vocabulary, nps
def get_noun_phrase_counts(doc, tokenizer, min_np_len, max_np_len, stopwords_list,
ignore_case, parser, stemmer_type):
is_stopword = defaultdict(int)
for stw in stopwords_list:
is_stopword[stw] = 1
corpus_postag = []
np2cnt = defaultdict(int) # noun phrase to document count
for text in doc:
# much faster when using regex & lambda (cntvec's tokenizer) than using nltk's word tokenizer
# toktxt = nltk.pos_tag(nltk.word_tokenize(text))
toktxt = nltk.pos_tag(tokenizer(text.encode().decode('utf-8')))
corpus_postag.append(toktxt)
# update the noun phrase list
if len(toktxt) > 0:
np_subset = get_noun_phrases_inner(toktxt, min_np_len, max_np_len, is_stopword,
ignore_case=ignore_case, parser=parser, stemmer_type=stemmer_type)
for np in np_subset:
np2cnt[np] += 1
return corpus_postag, np2cnt
def get_noun_phrases_inner(tagged_text, min_np_len=2, max_np_len=3, stopwords_map=None, ignore_case=True,
parser=None, stemmer_type=None):
"""
Return a set of noun phrases that are extracted from the pos-tagged sentence/text.
Parameters
----------
tagged_text : Tree or list[tuple[str,str]]
POS tagged tokenized text
max_np_len : int
Maximum word length of noun phrases
stopwords_map : defaultdict(int)
A dictionary that indicates whether the key (string) is a stopword (value=1) or not (value=0)
ignore_case : bool
True (default)
parser : nltk.RegexpParser or None
If None, it will use the RegexpParser with "NP: {<VBN|JJ|NN>*<NN|NNS>}")
stemmer_type : str or None ---- currently not supported!
If None, stmmer is not used.
'porter' : porter stemmer
'snowball' : snowball stemmer
Returns
-------
set
A set of noun phrases
"""
if parser is None:
parser = nltk.RegexpParser(r"NP: {<VBN|JJ|NN>*<NN|NNS>}")
# Currently not used!
if stemmer_type is not None:
if stemmer_type == 'snowball':
stemmer = nltk.stem.SnowballStemmer('english')
else:
stemmer = nltk.stem.PorterStemmer()
nps = set()
parsed_txt = parser.parse(tagged_text)
for tok in parsed_txt:
if not isinstance(tok[0], basestring): # tok[0] will be a tree (not str) if they are parsed as NPs.
if len(tok) < min_np_len or len(tok) > max_np_len:
continue
words = map(lambda k: k[0], tok)
if ignore_case:
add = True
for i in xrange(len(words)):
if not words[i].isupper(): # if all-caps words, don't change it to lower case
words[i] = words[i].lower()
if stopwords_map[words[i]] == 1:
add = False
break
if len(words[i]) < 2:
add = False
break
## Stemmer is not supported at the moment
# if stemmer_type is not None:
# words[-1] = stemmer.stem(words[-1])
if add:
nps.add(" ".join(words))
return nps
def get_stopwords(stopwords_dir='./stopwordlists'):
# This list of English stop words is taken from the "Glasgow Information
# Retrieval Group". The original list can be found at
# http://ir.dcs.gla.ac.uk/resources/linguistic_utils/stop_words
# This is the one that scikit learn uses.
# Also some stopwords are from Garren Gaut.
# stopwords_unicode = {s.decode('utf-8') if isinstance(s, basestring) else s for s in ENGLISH_STOP_WORDS}
stopwords = set()
if os.path.isdir(stopwords_dir):
for fname in os.listdir(stopwords_dir):
if fname.split(".")[-1] == "txt" and fname.startswith("stopword"):
fpath = os.path.join(stopwords_dir, fname)
if os.path.exists(fpath):
with open(fpath, 'r') as f:
reader = csv.reader(f, delimiter="\t")
for line in reader:
stopwords.add(' '.join(line).encode().decode('utf-8'))
else:
print("ERROR: directory "+ stopwords_dir+" does not exist!")
return stopwords