-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
286 lines (221 loc) · 10.8 KB
/
functions.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
def clean_freq(text):
'''A pre-processing function that cleans text of stopwords, punctuation and capitalization, tokenizes, lemmatizes
then finds the most frequently used 100 words
text - the text to be cleaned in string format'''
# Get all the stop words in the English language
import nltk
from nltk.corpus import stopwords
#importing additional functions to execute
import string
from nltk import FreqDist
#importing and enstantiating lemmatizer
from nltk.stem import WordNetLemmatizer
lemmatizer = nltk.stem.WordNetLemmatizer()
stopwords_list = stopwords.words('english')
#remove punctuation
stopwords_list += list(string.punctuation)
##adding adhoc all strings that don't appear to contribute, added 'article, page and wikipedia' iteratively as
##these are parts of most comment strings
stopwords_list += ("''","``", "'s", "\\n\\n" , '...', 'i\\','\\n',
'•', "i", 'the', "'m", 'i\\', "'ve", "don\\'t", "'ll",
"'re", "\\n\\ni", "it\\", "'ll", 'you\\', "'d", "n't",
'’', 'app', 'wa', 'ha', 'wo', 'u',"'s", "ve","'m","wo","doe")
from nltk import word_tokenize
tokens = word_tokenize(text)
lemma_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stopped_tokens = [w.lower() for w in lemma_tokens if w.lower() not in stopwords_list]
freqdist = FreqDist(stopped_tokens)
most_common_stopped = freqdist.most_common(100)
return most_common_stopped
def clean_tokens(text):
'''A pre-processing function that cleans text of stopwords, punctuation and capitalization, tokenizes, lemmatizes
then finds the most frequently used 100 words
text - the text to be cleaned in string format'''
# Get all the stop words in the English language
import nltk
from nltk.corpus import stopwords
#importing additional function to execute
import string
#importing and enstantiating lemmatizer
from nltk.stem import WordNetLemmatizer
lemmatizer = nltk.stem.WordNetLemmatizer()
stopwords_list = stopwords.words('english')
#remove punctuation
stopwords_list += list(string.punctuation)
##adding adhoc all strings that don't appear to contribute, added 'article, page and wikipedia' iteratively as
##these are parts of most comment strings
stopwords_list += ("''","``", "n't", 'app', "...", "n't",
"wa","ve", "ha","'", 'wa', 'ha', 'ca', "'ll",
'doe' 'wo','u',"'s","'ve", "ve","'m","wo","doe",
"'ve", "'d")
from nltk import word_tokenize
tokens = word_tokenize(text)
lemma_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stopped_tokens = [w.lower() for w in lemma_tokens if w.lower() not in stopwords_list]
return stopped_tokens
def cleaner_tokens(text):
'''A pre-processing function that cleans text of stopwords, punctuation and capitalization, tokenizes, lemmatizes
then finds the most frequently used 100 words
text - the text to be cleaned in string format'''
# Get all the stop words in the English language
import nltk
from nltk.corpus import stopwords
#importing additional function to execute
import string
#importing and enstantiating lemmatizer
from nltk.stem import WordNetLemmatizer
lemmatizer = nltk.stem.WordNetLemmatizer()
stopwords_list = stopwords.words('english')
#remove punctuation
stopwords_list += list(string.punctuation)
##adding adhoc all strings that don't appear to contribute, added 'article, page and wikipedia' iteratively as
##these are parts of most comment strings
stopwords_list += ("''","``", "n't", 'app', "...", "n't", "'d"
"wa","ve", "ha","'", 'wa', 'ha', 'ca', "'re"
'doe' 'wo','u',"'s","'ve", "ve","'m","wo","doe",
'love', 'great', 'excellent', 'awesome', 'really', 'good', 'brilliant',
'fantastic', 'amazing', 'wonderful',
'rubbish', 'terrible', 'worst', 'insane', 'useless', 'horrible',
'awful', 'stupid', 'suck', 'sucks'
)
from nltk import word_tokenize
tokens = word_tokenize(text)
lemma_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stopped_tokens = [w.lower() for w in lemma_tokens if w.lower() not in stopwords_list]
return stopped_tokens
def clean_comment(comment):
'''Lemmatizes, removes capitalization, punctuation and 'stopwords' from the lemmatized tokens,
returns data in the dataframe for modeling in a "clean" state
comment - a text string'''
from nltk.corpus import stopwords
import string
#splitting sentences into tokens
tokens = comment.split()
#instantiating Lemmatizer and lemmatizing words
lemmatizer = nltk.stem.WordNetLemmatizer()
lemma_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stopwords_list = stopwords.words('english')
stopwords_list += ("''","``", ".", 'app', 'apps', 'ca',"--", 'wa', 'ha', 'doe', 'wo', 'u')
#remove punctuation, capitalization, and stopwords
stopwords_list += list(string.punctuation)
stopped_tokens = [w.lower() for w in lemma_tokens if w.lower() not in stopwords_list]
return ' '.join(stopped_tokens)
def good_clean_tokens(text):
'''A pre-processing function that cleans text of stopwords, punctuation and capitalization,
tokenizes, lemmatizes
text - the text to be cleaned in string format'''
import nltk
from nltk.corpus import stopwords
#importing and enstantiating lemmatizer
from nltk.stem import WordNetLemmatizer
lemmatizer = nltk.stem.WordNetLemmatizer()
# Get all the stop words in the English language
stopwords_list = stopwords.words('english')
#importing additional function to execute
import string
#remove punctuation
stopwords_list += list(string.punctuation)
##adding adhoc all strings that don't appear to contribute, added 'love, great, good, really, amazing' iteratively as
##these are parts of most comment strings
stopwords_list += ("''","``", "n't", 'app', 'love', 'apps', 'great', 'good', 'really', 'wa', 'ha',"I", "l",
'...', "--","'s", 'amazing', 've', 'excellent', 'awesome', 'wonderful', 'fantastic', "ve")
from nltk import word_tokenize
tokens = word_tokenize(text)
lemma_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stopped_tokens = [w.lower() for w in lemma_tokens if w.lower() not in stopwords_list]
return stopped_tokens
def five_cluster_wrdcld(xtrain):
'''Takes a dataframe to return wordcloud renderings for each cluster in a KMEANS
model for a 5 cluster model
xtrain - a dataframe '''
from wordcloud import WordCloud
import matplotlib.pyplot as plt
cloud = {}
for cluster in list(xtrain['5cluster'].unique()):
clust_df = xtrain[xtrain['5cluster']==cluster]
wrdcld5 = WordCloud(width=400, height=200, background_color="white",
max_words=5000, contour_width=3, collocations=False,
contour_color='steelblue')
wrdcld5.generate(clust_df['content'].to_string())
clustwrdcld = wrdcld5.to_image()
fig = plt.imshow(clustwrdcld, interpolation='bilinear')
plt.axis('off')
plt.title(f'Cluster {cluster} WordCloud:')
cloud[cluster]=fig
plt.show()
return cloud
def six_cluster_wrdcld(xtrain):
from wordcloud import WordCloud
import matplotlib.pyplot as plt
cloud = {}
for cluster in list(xtrain['6cluster'].unique()):
clust_df = xtrain[xtrain['6cluster']==cluster]
wrdcld6 = WordCloud(width=400, height=200, background_color="white",
max_words=5000, contour_width=3, collocations=False,
contour_color='steelblue')
wrdcld6.generate(clust_df['content'].to_string())
clustwrdcld = wrdcld6.to_image()
fig = plt.imshow(clustwrdcld, interpolation='bilinear')
plt.axis('off')
plt.title(f'Cluster {cluster} WordCloud:')
cloud[cluster]=fig
plt.show()
return cloud
def three_cluster_wrdcld(xtrain):
from wordcloud import WordCloud
import matplotlib.pyplot as plt
cloud = {}
for cluster in list(xtrain['3cluster'].unique()):
clust_df = xtrain[xtrain['3cluster']==cluster]
wrdcld3 = WordCloud(width=400, height=200, background_color="white",
max_words=5000, contour_width=3, collocations=False,
contour_color='steelblue')
wrdcld3.generate(clust_df['content'].to_string())
clustwrdcld = wrdcld3.to_image()
fig = plt.imshow(clustwrdcld, interpolation='bilinear')
plt.axis('off')
plt.title(f'Cluster {cluster} WordCloud:')
cloud[cluster]=fig
plt.show()
return cloud
def plot_coefs(classifier, scaler, col):
'''Plotting function that takes a dataframe and classifier model
and plots the top ten most negative coefficients
df - dataframe that is being analysed
classifier - multinomial classifier
'''
#importing libraries
import pandas as pd
import matplotlib.pyplot as plt
feats = scaler.get_feature_names()
#creating a dictionary for each of the classes and enumerating them in
#order to track the coefficients for each:
class_dict = {}
for i, cat in enumerate(classifier.classes_):
class_dict[cat] = classifier.coef_[i]
#creatging a dataframe of the output
class_coefs = pd.DataFrame(class_dict)
#creating a column that tracks the features for each
class_coefs['feats'] = feats
#setting the index to each of the features:
class_coefs.set_index('feats', inplace=True)
#slicing the most meaningful negative words:
class_coefs[col].sort_values(ascending=False).head(15).plot(kind='barh')
plt.title('Most important terms used to classify a review:', fontsize=14)
plt.ylabel('Term')
plt.xlabel('Mathmetical Coefficient')
def generate_wordcloud(words, mask):
'''This function takes in text and a mask as a .png and generates a wordcloud in the
shape of the mask..
words - a string of text
mask - .png file eg: nmask = np.array(Image.open('mask.png')) you'll need the file
path if the file isn't local'''
import matplotlib.pyplot as plt
from wordcloud import WordCloud
word_cloud = WordCloud(width = 500, height = 300, background_color='white',
contour_color = 'purple', contour_width = 1, mask=mask).generate(words)
plt.figure(figsize=(10,8),facecolor = 'white', edgecolor='blue')
plt.imshow(word_cloud)
plt.axis('off')
plt.tight_layout(pad=0)
plt.show()