-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibraries.py
212 lines (159 loc) · 5.85 KB
/
Libraries.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
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import confusion_matrix
from wordcloud import WordCloud, STOPWORDS
import nltk
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('omw-1.4')
nltk.download('punkt')
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import pandas as pd
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
class NLPModel:
def __init__(self, dataset, model, model_name, x, y):
self.dataset = dataset
self.model = model
self.name = model_name
self.x = x
self.y = y
def getLabels(self):
xLabel = self.dataset[self.x]
yLabel = self.dataset[self.y]
return (xLabel, yLabel)
def buildPipeline(self):
pipeline = Pipeline(steps = [('cv', CountVectorizer()), (self.name, self.model)])
return pipeline
def fitModel(self, pipeline, xLabel, yLabel):
pipeline.fit(xLabel, yLabel)
return pipeline
def score(self, pipeline, xLabel, yLabel):
prediction = pipeline.predict(xLabel)
predScore = pipeline.score(xLabel, yLabel)
return (prediction, predScore)
def buildCM(self, pred, actual):
CM = confusion_matrix(actual, pred)
return CM
def printCM(self, CM):
plt.figure(figsize = (20, 20))
sb.heatmap(CM, annot = True, fmt=".0f", annot_kws={"size": 18})
def printScore(self, predScore, CM):
print("The classification accuracy is: ", predScore)
print("")
precision = []
recall = []
f1 = []
row = 0
col = 0
total = 0
for i in pd.Categorical(self.dataset[self.y]).categories:
for j in range(len(pd.Categorical(self.dataset[self.y]).categories)):
total += CM[row][col]
row += 1
precision.append(CM[col][col] / total)
col += 1
row = 0
total = 0
row = 0
col = 0
total = 0
for i in pd.Categorical(self.dataset[self.y]).categories:
for j in range(len(pd.Categorical(self.dataset[self.y]).categories)):
total += CM[row][col]
col += 1
recall.append(CM[row][row] / total)
row += 1
col = 0
total = 0
index = 0
for i in pd.Categorical(self.dataset[self.y]).categories:
f1score = (2 * precision[index] * recall[index]) / (precision[index] + recall[index])
f1.append(f1score)
index += 1
score_df = pd.DataFrame()
score_df['Emotion'] = pd.Categorical(self.dataset[self.y]).categories
score_df['Precision'] = precision
score_df['Recall'] = recall
score_df['F1-score'] = f1
return score_df
def show_wordcloud(data, column, bg, max_words, max_font_size, scale, figsize):
stopwords = set(STOPWORDS)
text = " ".join(t for t in data[column])
def display():
wordcloud = WordCloud(
background_color = bg,
stopwords = stopwords,
max_words = max_words,
max_font_size = max_font_size,
scale = scale,
random_state = 1)
wordcloud = wordcloud.generate(str(text))
plt.figure(1, figsize = figsize)
plt.axis('off')
plt.imshow(wordcloud)
plt.show()
display()
def lemmatizeWords(dataset, column, newColumn):
lm = WordNetLemmatizer()
lemmatized_data = []
for sentence in dataset[column]:
sentence = "".join([lm.lemmatize(w, 'v') for w in sentence])
lemmatized_data.append(sentence)
dataset[newColumn] = lemmatized_data
return dataset
def get_html_keys():
url = "https://www.w3schools.com/TAgs/default.asp"
html_keys = pd.read_html(url)
html_tags = []
html_keys[0].head()
for i in html_keys[0]['Tag']:
i = i.strip("<>")
html_tags.append(i)
html_tags.append("www")
html_tags.append("http")
html_tags.append("https")
url = "https://www.w3schools.com/tags/ref_attributes.asp"
html_keys = pd.read_html(url)
html_attr = []
html_keys[0].head()
for i in html_keys[0]['Attribute']:
html_attr.append(i)
return html_tags + html_attr
def remove_html_attr(text, html_keys):
text = text.split()
return " ".join([t for t in text if t.lower() not in set(html_keys)])
def extend_word(word):
custom_words = set(['dont', 'didnt', 'shouldnt', 'cant', 'wont', 'wouldnt', 'musnt'])
temp = []
if word in custom_words:
for char in range(0, len(word) - 1):
temp.append(word[char])
return "".join(temp)
return word
def removeStopWords(dataset, column):
# Custom stopwords which are not in the nltk stopwords set
custom_stopwords = set(['im', 'ive', 'ill', 'feeling', 'feel', 'felt'])
cleaned_data = []
outlier = []
forbidden_words = set(stopwords.words('English'))
loc = 0
for sentence in dataset[column]:
# Extend words that are concatenated without apostrophe such as 'cant' to 'can' and 't'
# Remove the individual words which are present in the custom stopwords set and the nltk stopwords set
split_sentence = sentence.split()
temp = []
for word in split_sentence:
word = extend_word(word)
if word not in custom_stopwords and word not in forbidden_words:
temp.append(word)
# Join back the words which are not removed into a sentence
sentence = " ".join(temp)
# Treat a sentence with only stopwords and html tags and attributes as outliers
if len(sentence) == 0:
outlier.append(loc)
cleaned_data.append(sentence)
loc += 1
return (outlier, cleaned_data)