-
Notifications
You must be signed in to change notification settings - Fork 8
/
nlp_utils.py
338 lines (225 loc) · 8.66 KB
/
nlp_utils.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
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
"""
__file__
nlp_utils.py
__description__
- Text cleaning utilities
- Default global objects = (stopwords,tokenizer,stemmer)
- Class: Stemmer (wrapper for popular stemmers)
- Class: Tokenizer (wrapper for RegExpTokenizer)
- Class: Normalizer (Stemmer + Tokenizer - stopwords)
- Class: wrappers for sklearn:
[TfidfVectorizer + Stemmer]
[CountVectorizer + Stemmer]
- Defaults for TFIDF and CountVectorizers
- Various tokenizer regexp patterns
- Common set distances used in nlp
__author__
Arman Akbarian
"""
import re
import csv
import nltk
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import RegexpTokenizer
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
####### Wrapper for WordNetLemmatizer #########
class WordNetStemmer(WordNetLemmatizer):
def stem(self,word,pos=u'n'):
return self.lemmatize(word,pos)
######## Wrapper for all of the popular stemmers ###########
class Stemmer(object):
def __init__(self,stemmer_type):
self.stemmer_type = stemmer_type
if (self.stemmer_type == 'porter'):
self.stemmer = nltk.stem.PorterStemmer()
elif (self.stemmer_type == 'snowball'):
self.stemmer = nltk.stem.SnowballStemmer('english')
elif (self.stemmer_type == 'lemmatize'):
self.stemmer = WordNetStemmer()
else:
raise NameError("'"+stemmer_type +"'" + " not supported")
######## Simple wordreplacer object using a dictionary ############
class WordReplacer(object):
def __init__(self, words_map):
self.words_map = words_map
def replace(self, words):
return [self.words_map.get(w, w) for w in words]
####### wordreplacer with csv file for word replacement dictionary ########
class CSVWordReplacer(WordReplacer):
def __init__(self, fname):
words_map = {}
for line in csv.reader(open(fname)):
word, syn = line
if word.startswith('#'):
continue
words_map[word] = syn
super(CSVWordReplacer, self).__init__(words_map)
######### for now just a wrapper to RegexpTokenizer #########
class Tokenizer():
def __init__(self,pattern):
self.pattern = pattern
self.tokenizer = RegexpTokenizer(self.pattern)
######## defining a default stopwords set #############
stopwords = nltk.corpus.stopwords.words('english')
stopwords = set(stopwords)
######## defining a default stemmer ##########
stemmer_type = 'snowball' # optimum of speed and handling
stemmer = Stemmer(stemmer_type).stemmer
###### other options for stemmer: ##########
#stemmer_type = 'porter' # Fastest, doesn' handle as good as snowball
#stemmer_type = 'lemmatize' # Slowest (aka the best!)
######### default token pattern #############
token_pattern = r"(?u)\b\w\w+\b" # good enough and fast enough (from tfidf library)
####### other options for token pattern #########
#token_pattern = r"(?:[A-Za-z]\.)+|\w+(?:[']\w+)*|\$?\d+(?:\.\d+)?%?" # might be slow
#token_pattern= r"\w+(?:[-']\w+)*|'|[-.(]+|\S\w*"
#token_pattern = r'[\w']+'
#token_pattern = r'[ \t\n]+'
#token_pattern = r'\W+'
#token_pattern = r'\w+|\S\w*'
####### defining a default tokenizer ######
tokenizer = Tokenizer(token_pattern).tokenizer
######### Tokenizer + Stemmer - Stopwords ###########
class Normalizer(object):
def __init__(self,stemmer,tokenizer,stop_words):
self.tokenizer = tokenizer
self.stemmer = stemmer
self.stop_words = stop_words
def normalize(self, text):
return [self.stemmer.stem(token)
for token in self.tokenizer.tokenize(text.lower())
if token not in self.stop_words]
######### defining a default normalizer ##########
normalizer = Normalizer(stemmer,tokenizer,stopwords)
########### Normalizer without Stemmer ##############
class NoStemNormalizer(object):
def __init__(self,tokenizer,stop_words):
self.tokenizer = tokenizer
self.stop_words = stop_words
def normalize(self, text):
return [token for token in self.tokenizer.tokenize(text.lower())
if token not in self.stop_words]
nostemnormalizer = NoStemNormalizer(tokenizer,stopwords)
########## Stemmer + Tfidf wrapper ############
class StemmedTfidfVectorizer(TfidfVectorizer):
def build_analyzer(self):
analyzer = super(TfidfVectorizer, self).build_analyzer()
return lambda doc: (stemmer.stem(w) for w in analyzer(doc))
########## Stemmer + CountVectorizer wrapper #############
class StemmedCountVectorizer(CountVectorizer):
def build_analyzer(self):
analyzer = super(CountVectorizer, self).build_analyzer()
return lambda doc: (stemmer.stem(w) for w in analyzer(doc))
########## Defaults TF-IDF & Count Vectorizers ########
#======== TF-IDF Vectorizer =========#
tfidf__norm = "l2"
tfidf__max_df = 0.75
tfidf__min_df = 3
def getTFV(token_pattern = token_pattern,
norm = tfidf__norm,
max_df = tfidf__max_df,
min_df = tfidf__min_df,
ngram_range = (1, 1),
vocabulary = None,
stop_words = 'english'):
tfv =TfidfVectorizer(min_df=min_df, max_df=max_df, max_features=None,
strip_accents='unicode', analyzer='word',
token_pattern=token_pattern,
ngram_range=ngram_range, use_idf=True,
smooth_idf=True, sublinear_tf=True,
stop_words = stop_words, norm=norm, vocabulary=vocabulary)
return tfv
#========= CountVectorizer =========#
bow__max_df = 0.75
bow__min_df = 3
def getBOW(token_pattern = token_pattern,
max_df = bow__max_df,
min_df = bow__min_df,
ngram_range = (1, 1),
vocabulary = None,
stop_words = 'english'):
bow =CountVectorizer(min_df=min_df, max_df=max_df, max_features=None,
strip_accents='unicode', analyzer='word',
token_pattern=token_pattern,
ngram_range=ngram_range,
stop_words = stop_words, vocabulary=vocabulary)
return bow
########################################################
# ------------------------------
# Simple text cleaning using
#
# -replacement dict
#
# or
#
# -WordReplacer object
#--------------------------------
def clean_text(text,replace_dict=None,words_replacer=None):
text = text.lower()
if replace_dict is not None:
for k, v in replace_dict.items():
text = re.sub(k,v,text)
if words_replacer is not None:
text = text.split(' ')
text = words_replacer.replace(text)
text = ' '.join(text)
return text
####### Standard distance metrics ##########
def JaccardCoef(A, B):
A, B = set(A), set(B)
intersect = len(A.intersection(B))
union = len(A.union(B))
coef = safe_divide(intersect, union)
return coef
def DiceDist(A, B):
A, B = set(A), set(B)
intersect = len(A.intersection(B))
union = len(A) + len(B)
d = safe_divide(2*intersect, union)
return d
def compute_dist(A, B, dist="jaccard_coef"):
if dist == "jaccard_coef":
d = JaccardCoef(A, B)
elif dist == "dice_dist":
d = DiceDist(A, B)
return d
def pairwise_jaccard_coef(A, B):
coef = np.zeros((A.shape[0], B.shape[0]), dtype=float)
for i in range(A.shape[0]):
for j in range(B.shape[0]):
coef[i,j] = JaccardCoef(A[i], B[j])
return coef
def pairwise_dice_dist(A, B):
d = np.zeros((A.shape[0], B.shape[0]), dtype=float)
for i in range(A.shape[0]):
for j in range(B.shape[0]):
d[i,j] = DiceDist(A[i], B[j])
return d
def pairwise_dist(A, B, dist="jaccard_coef"):
if dist == "jaccard_coef":
d = pairwise_jaccard_coef(A, B)
elif dist == "dice_dist":
d = pairwise_dice_dist(A, B)
return d
###### other common tools #########
def stem_tokens(tokens, stemmer):
stemmed = []
for token in tokens:
stemmed.append(stemmer.stem(token))
return stemmed
def safe_divide(x,y,res=0.0):
if y != 0.0:
res = float(x)/float(y)
return res
def cosine_sim(x, y):
try:
d = cosine_similarity(x.reshape(1,-1), y.reshape(1,-1))
d = d[0][0]
except:
d = 0.0
return d
if __name__ == '__main__':
print(__doc__)