-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemotion_recognizer_ann.py
251 lines (195 loc) · 7.75 KB
/
emotion_recognizer_ann.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
#important libraries
import pandas as pd
import numpy as np
import nltk
import re
#importing stopwords is optional, in this case it decreased accuracy
#from nltk.corpus import stopwords
import itertools
import json
import time
import datetime
start_time = time.time()
import os
os.chdir('/tmp/guest-pltjjp/Downloads')
data = pd.read_csv('text_emotion.csv')
#data = data.iloc[:100,:]
#stopset = set(stopwords.words('english'))
from nltk.stem.wordnet import WordNetLemmatizer
lem = WordNetLemmatizer()
#comprehensive cleaning
def cleaning(text):
txt = str(text)
txt = re.sub(r"http\S+", "", txt)
if len(txt) == 0:
return 'no text'
else:
txt = txt.split()
index = 0
for j in range(len(txt)):
if txt[j][0] == '@':
index = j
txt = np.delete(txt, index)
if len(txt) == 0:
return 'no text'
else:
words = txt[0]
for k in range(len(txt)-1):
words+= " " + txt[k+1]
txt = words
txt = re.sub(r'[^\w]', ' ', txt)
if len(txt) == 0:
return 'no text'
else:
txt = ''.join(''.join(s)[:2] for _, s in itertools.groupby(txt))
txt = txt.replace("'", "")
txt = nltk.tokenize.word_tokenize(txt)
#data.content[i] = [w for w in data.content[i] if not w in stopset]
for j in range(len(txt)):
txt[j] = lem.lemmatize(txt[j], "v")
if len(txt) == 0:
return 'no text'
else:
return txt
data['content'] = data['content'].map(lambda x: cleaning(x))
data = data.reset_index(drop=True)
for i in range(len(data)):
words = data.content[i][0]
for j in range(len(data.content[i])-1):
words+= ' ' + data.content[i][j+1]
data.content[i] = words
le_train = data.iloc[:int(np.round(len(data)*.75)), :]
val = data.iloc[int(np.round(len(data)*.75)):,:].reset_index(drop = True)
training_data = []
for i in range(len(le_train)):
training_data.append({"class": le_train.sentiment[i], "sentence": le_train.content[i]})
words = []
classes = []
documents = []
# loop through each sentence in our training data
for pattern in training_data:
w = nltk.word_tokenize(pattern['sentence'])
words.extend(w)
documents.append((w, pattern['class']))
if pattern['class'] not in classes:
classes.append(pattern['class'])
classes = list(set(classes))
# create our training data
training = []
output = []
# create an empty array for our output
output_empty = [0] * len(classes)
# training set, bag of words for each sentence
for doc in documents:
bag = []
pattern_words = doc[0]
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
training.append(bag)
# output is a '0' for each tag and '1' for current tag
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
output.append(output_row)
print (len(documents), "documents")
print (len(classes), "classes", classes)
# compute sigmoid nonlinearity
def sigmoid(x):
output = 1/(1+np.exp(-x))
return output
# convert output of sigmoid function to its derivative
def sigmoid_output_to_derivative(output):
return output*(1-output)
def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
return sentence_words
def bow(sentence, words):
sentence_words = clean_up_sentence(sentence)
bag = [0]*len(words)
for s in sentence_words:
for i,w in enumerate(words):
if w == s:
bag[i] = 1
return(np.array(bag))
def model(sentence):
x = bow(sentence.lower(), words)
# input layer is our bag of words
l0 = x
# matrix multiplication of input and hidden layer
l1 = sigmoid(np.dot(l0, synapse_0))
# output layer
l2 = sigmoid(np.dot(l1, synapse_1))
return l2
def train(X, y, hidden_neurons=10, alpha=1, epochs=5000, dropout=False, dropout_percent=0.2):
np.random.seed(1)
last_mean_error = 1
# randomly initialize our weights with mean 0
synapse_0 = 2*np.random.random((len(X[0]), hidden_neurons)) - 1
synapse_1 = 2*np.random.random((hidden_neurons, len(classes))) - 1
prev_synapse_0_weight_update = np.zeros_like(synapse_0)
prev_synapse_1_weight_update = np.zeros_like(synapse_1)
synapse_0_direction_count = np.zeros_like(synapse_0)
synapse_1_direction_count = np.zeros_like(synapse_1)
for j in iter(range(epochs+1)):
# Feed forward through layers 0, 1, and 2
layer_0 = X
layer_1 = sigmoid(np.dot(layer_0, synapse_0))
if(dropout):
layer_1 *= np.random.binomial([np.ones((len(X),hidden_neurons))],1-dropout_percent)[0] * (1.0/(1-dropout_percent))
layer_2 = sigmoid(np.dot(layer_1, synapse_1))
layer_2_error = y - layer_2
if (j% 1000) == 0 and j > 500:
# if this 1k iteration's error is greater than the last iteration, break out
if np.mean(np.abs(layer_2_error)) < last_mean_error:
print ("error after "+str(j)+" iterations:" + str(np.mean(np.abs(layer_2_error))) )
last_mean_error = np.mean(np.abs(layer_2_error))
else:
print ("break:", np.mean(np.abs(layer_2_error)), ">", last_mean_error )
break
layer_2_delta = layer_2_error * sigmoid_output_to_derivative(layer_2)
layer_1_error = layer_2_delta.dot(synapse_1.T)
layer_1_delta = layer_1_error * sigmoid_output_to_derivative(layer_1)
synapse_1_weight_update = (layer_1.T.dot(layer_2_delta))
synapse_0_weight_update = (layer_0.T.dot(layer_1_delta))
if(j > 0):
synapse_0_direction_count += np.abs(((synapse_0_weight_update > 0)+0) - ((prev_synapse_0_weight_update > 0) + 0))
synapse_1_direction_count += np.abs(((synapse_1_weight_update > 0)+0) - ((prev_synapse_1_weight_update > 0) + 0))
synapse_1 += alpha * synapse_1_weight_update
synapse_0 += alpha * synapse_0_weight_update
prev_synapse_0_weight_update = synapse_0_weight_update
prev_synapse_1_weight_update = synapse_1_weight_update
now = datetime.datetime.now()
# persist synapses
synapse = {'synapse0': synapse_0.tolist(), 'synapse1': synapse_1.tolist(),
'datetime': now.strftime("%Y-%m-%d %H:%M"),
'words': words,
'classes': classes
}
synapse_file = "synapses.json"
with open(synapse_file, 'w') as outfile:
json.dump(synapse, outfile, indent=4, sort_keys=True)
X = np.array(training)
y = np.array(output)
train(X, y, hidden_neurons=20, alpha=0.1, epochs=10000, dropout=False, dropout_percent=0.2)
# load our calculated synapse values
synapse_file = 'synapses.json'
with open(synapse_file) as data_file:
synapse = json.load(data_file)
synapse_0 = np.asarray(synapse['synapse0'])
synapse_1 = np.asarray(synapse['synapse1'])
def classify(sentence, show_details=False):
results = model(sentence)
for i in range(len(results)):
if results[i] == np.max(results):
emotion = classes[i]
return emotion
#validation
predictions = []
for i in range(len(val)):
predictions.append(classify(val.content[i]))
from sklearn.metrics import f1_score
from sklearn.metrics import classification_report
print(classification_report(val.sentiment, predictions))
prediction_df = pd.DataFrame({'content':val.content, 'sentiment_predicted':predictions, 'sentimentActual':val.sentiment})
prediction_df.to_csv('emotion_recognizer.csv', index = False)
elapsed_time = time.time() - start_time
print ("processing time:", elapsed_time, "seconds")