-
Notifications
You must be signed in to change notification settings - Fork 1
/
KerasSentiment.py
266 lines (233 loc) · 10.2 KB
/
KerasSentiment.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
import numpy
from keras.models import Sequential
from keras.layers import Dense, Dropout
# from gensim.models import word2vec
from gensim.models.keyedvectors import KeyedVectors
import argparse
import numpy as np
import csv
import string
import math
import random
from keras.utils import plot_model
from sklearn.model_selection import StratifiedKFold
np.random.seed(123)
def format_sentence(sent, swords=None):
# convert to lowercase
sent = sent.translate(str.maketrans("", "", string.punctuation)).lower()
# remove stopwords
if swords is not None:
com_list = sent.split()
filtered_words = []
for word in com_list:
if word not in swords and word in w2v_model.wv.vocab and len(w2v_model[word].tolist()) != 0:
filtered_words.append(w2v_model[word].tolist())
else:
com_list = sent.split()
filtered_words = []
for word in com_list:
if word in w2v_model.wv.vocab and len(w2v_model[word].tolist()) != 0:
filtered_words.append(w2v_model[word].tolist())
# Sum all of the word vectors
# comment = [0.0 for _ in range(w2vsize)]
# for word in filtered_words:
# for i in range(w2vsize):
# comment[i] += word[i]
# Average all of the word vectors
comment = [0.0 for _ in range(w2vsize)]
for word in filtered_words:
for i in range(w2vsize):
comment[i] += word[i]
if len(filtered_words) > 0:
for i in range(w2vsize):
comment[i] = comment[i]/len(filtered_words)
return comment
# Import csv file
def import_csv(filename):
my_list = []
with open(filename) as commentfile:
reader = csv.DictReader(commentfile)
for row in reader:
my_list.append({'comment': row['comment'], 'rating': row['rating']})
# Parse and convert positive and negative examples.
pos_words = []
neg_words = []
for c_dict in my_list:
tmp_com = c_dict['comment']
tmp_rating = c_dict['rating']
# remove stop words
with open(args.s) as c_raw:
c_stopwords = c_raw.read().translate(str.maketrans("", "", string.punctuation)).splitlines()
if tmp_rating in args.n:
neg_words.append((format_sentence(tmp_com, c_stopwords), 0))
if tmp_rating in args.p:
pos_words.append((format_sentence(tmp_com, c_stopwords), 1))
# neg_words.extend(neg_words)
# neg_words.extend(neg_words[:100])
print("Total Negative Instances:" + str(len(neg_words)) + "\nTotal Positive Instances:" + str(len(pos_words)))
return neg_words, pos_words
# Parse input arguments
parser = argparse.ArgumentParser(description='Train a DNN Sentiment Classifier')
parser.add_argument('-i', metavar='inputfile', type=str,
help='path to the input csv file for training and testing.', required=True)
parser.add_argument('-c', metavar='toclassify', type=str, help='path to file with entries to classify.',
required=False, default=None)
parser.add_argument('-s', metavar='stopwords', type=str, help='path to stopwords file', required=True)
parser.add_argument('-p', metavar='posratings', type=str, help='a list of positive ratings as strings',
required=False, default=['4', '5'])
parser.add_argument('-n', metavar='negratings', type=str, help='a list of negative ratings as strings',
required=False, default=['1', '2'])
parser.add_argument('-z', metavar='iterations', type=str,
help='the number of times to repeat the classifier training', required=False, default=2)
parser.add_argument('-d', metavar='domain', type=str, help='a file with text from a different domain.',
required=False, default=None)
parser.add_argument('-m', metavar='model', type=str, help='location of word2vec model to be used',
required=False, default='/media/sf_Grad_School/GoogleNews-vectors-negative300.bin')
args = parser.parse_args()
# Build word2vec model
w2vsize = 300
# print("Building word2vec model on {}".format(args.i))
# sentences = word2vec.Text8Corpus(args.i)
# w2v_model = word2vec.Word2Vec(sentences, size=w2vsize, min_count=1, workers=4)
# You can get google's word2vec model here:
# https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing
print("word2vec model {}".format(args.m))
w2v_model = KeyedVectors.load_word2vec_format(args.m, binary=True)
neg_list, pos_list = import_csv(args.i)
# print(neg_list)
seed = 123
numpy.random.seed(seed)
# dataset = neg_list.extend(pos_list)
# print(dataset)
# X = pos_list[:0]
# print(X)
# Y = pos_list[:1]
# print(Y)
negcutoff = math.floor(len(neg_list) * 1)
poscutoff = math.floor(len(pos_list) * 1)
neg_idx_train = sorted(random.sample(range(len(neg_list)), negcutoff))
neg_train = [neg_list[i] for i in neg_idx_train]
pos_idx_train = sorted(random.sample(range(len(pos_list)), poscutoff))
pos_train = [pos_list[i] for i in pos_idx_train]
train = neg_train + pos_train
X = numpy.array([x[0] for x in train])
Y = numpy.array([x[1] for x in train])
kfold = StratifiedKFold(n_splits=int(args.z), shuffle=True, random_state=seed)
cvscores = []
for train, test in kfold.split(X,Y):
model = Sequential()
model.add(Dense(w2vsize, input_shape=(w2vsize, ), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize*2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X[train], Y[train], epochs=50, batch_size=20, verbose=0)
scores = model.evaluate(X[test], Y[test], verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
# plot_model(model, to_file='model.png')
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores), numpy.std(cvscores)))
# create training and test sets
# set the cutoffs
# negcutoff = math.floor(len(neg_list) * 3 / 4)
# poscutoff = math.floor(len(pos_list) * 3 / 4)
#
# avgAccuracy = 0
# print("Training {} times...".format(args.z))
# for z in range(int(args.z)):
# # train = neg_list[:negcutoff] + pos_list[:poscutoff]
# # test = neg_list[negcutoff:] + pos_list[poscutoff:]
# neg_idx_train = sorted(random.sample(range(len(neg_list)), negcutoff))
# neg_train = [neg_list[i] for i in neg_idx_train]
#
# neg_idx_test = set(range(len(neg_list))) - set(neg_idx_train)
# neg_test = [neg_list[i] for i in neg_idx_test]
#
# pos_idx_train = sorted(random.sample(range(len(pos_list)), poscutoff))
# pos_train = [pos_list[i] for i in pos_idx_train]
#
# pos_idx_test = set(range(len(pos_list))) - set(pos_idx_train)
# pos_test = [pos_list[i] for i in pos_idx_test]
#
# train = neg_train + pos_train
# test = neg_test + pos_test
# print('Training on %d instances, testing on %d instances' % (len(train), len(test)))
#
# train_data = [x[0] for x in train]
# train_labels = [x[1] for x in train]
# test_data = [x[0] for x in test]
# test_labels = [x[1] for x in test]
# model.fit(train_data, train_labels, epochs=20, batch_size=10)
# scores = model.evaluate(test_data, test_labels)
# avgAccuracy += scores[1]
# print("Test data accuracy: {}".format(scores[1]*100))
# # print("{}: {}".format(model.metrics_names[1], scores[1]*100))
#
# print("Average Accuracy: " + str(avgAccuracy / int(args.z)))
# Import the file needing classification.
if args.c is not None:
model = Sequential()
model.add(Dense(w2vsize, input_shape=(w2vsize,), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize * 2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=50, batch_size=20, verbose=0)
with open(args.c) as file:
toclass = file.readlines()
print("Predicting...")
predict_this = []
sentences = []
for phrase in toclass:
predict_this.append(format_sentence(phrase))
sentences.append(phrase)
answers = []
predictions = model.predict(predict_this)
for i in range(len(predictions)):
answers.append([predictions[i], sentences[i]])
print("{} :: {}".format(predictions[i], sentences[i]))
if args.d is not None:
domain_list = []
with open(args.d) as domainfile:
d_reader = csv.DictReader(domainfile)
for d_row in d_reader:
domain_list.append({'comment': d_row['comment'], 'rating': d_row['rating']})
print("{} length: {}".format(args.d, len(domain_list)))
d_list = []
for c in range(len(domain_list)):
tmp_c = domain_list[c]['comment']
tmp_r = domain_list[c]['rating']
# remove stop words
with open(args.s) as raw:
stopwords = raw.read().translate(str.maketrans("", "", string.punctuation)).splitlines()
if tmp_r in args.n:
d_list.append((format_sentence(tmp_c, stopwords), 0))
if tmp_r in args.p:
d_list.append((format_sentence(tmp_c, stopwords), 1))
d_data = [word[0] for word in d_list]
d_labels = [word[1] for word in d_list]
model = Sequential()
model.add(Dense(w2vsize, input_shape=(w2vsize,), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize * 2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(w2vsize, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=50, batch_size=20, verbose=0)
domain_accuracy = model.evaluate(d_data, d_labels)
print("Classifier domain shift accuracy: {}".format(domain_accuracy[1]*100))
# print("\n{}: {}".format(model.metrics_names[1], domain_accuracy[1]*100))