-
Notifications
You must be signed in to change notification settings - Fork 3
/
bagOfWords.py
302 lines (247 loc) · 9.69 KB
/
bagOfWords.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
# import nltk
import csv
import nltk
import perceptron
import random
import time
class Main():
# Open a file
file1 = csv.reader(open('DataCSV.csv', 'rb'), delimiter=',', quotechar='"')
# Initialize dictionaries
sentence = {}
sentiment = {}
corpus = {}
probWord = {}
probSent = {}
wordVectors = {}
# Perceptron used for machine learning
p = perceptron.Perceptron()
# Initialize lists
trainSet = []
testSet = []
validationSet = []
bagOfWords = []
# The distribution of all the data over train-, test- and validationset
distribution = (0.7, 0.1, 0.2)
def __init__(self, iterations = 10):
# Load the sentences and sentiments from file
self.initializeCorpus()
# Use machine learning method
# self.singleInputPerceptron()
self.multiInputPerceptron()
def singleInputPerceptron(self):
# Reset totals
acc = 0
pre = 0
# Get current time
t = time.time()
for i in range( iterations ):
print "--- iteration", i + 1, "of", iterations, "---"
# Reset dictionaries
self.corpus = {}
self.probWord = {}
self.probSent = {}
self.p.reset()
self.trainSet = []
self.testSet = []
self.validationSet = []
# Go through the steps
self.makeCorpus()
self.calcProbability()
self.trainSingleInputPerceptron()
# Retrieve results
result = self.printResults()
# Add to the totals
acc += result[0]
pre += result[1]
# Average results and print
print 'Accuracy , averaged: ', acc / float(iterations)
print 'Precision, averaged: ', pre / float(iterations)
print 'Time taken for', iterations, 'iterations: ', time.time()- t
def multiInputPerceptron(self):
# Get current time
t = time.time()
# Go through the steps
self.makeCorpus()
self.createWordVectors()
self.calcProbability()
self.trainMultiInputPerceptron()
self.testMultiInputPerceptron()
print 'Time taken: ', time.time() - t
def initializeCorpus(self):
self.sentence = {}
self.sentiment = {}
# Initialize counter
i = 0
# Collect sentences and sentiments
for entry in self.file1:
# Do not include header
if i == 0:
i+=1
continue
# The actual message is the 9th attribute, sentiment is the 4th
self.sentence[i - 1] = entry[9]
self.sentiment[i - 1] = float(entry[4])
# Stop at 10000
i += 1
if ( i == 2000 ): break
def makeCorpus(self):
print 'Creating corpus...'
for i in range(1,2000):
# Assign at random to train, test or validation set
r = random.random()
if ( r < self.distribution[0] ):
self.trainSet.append(i-1)
else:
self.testSet.append(i-1)
# Create corpus and count word frequencies
self.corpus = {}
for j in self.trainSet:
# Tokenize the sentence
tk_sentence = nltk.tokenize.word_tokenize( self.sentence[j] )
# Check for sentiment
sent = self.sentiment[j]
# Iterate over every token
for token in tk_sentence:
# Add token to the bag of words
if not token in self.bagOfWords:
self.bagOfWords.append(token)
# Increment frequencies
if token in self.corpus:
if sent != 0:
self.corpus[token] = self.corpus[token][0] + 1, self.corpus[token][1] + 1
else:
self.corpus[token] = self.corpus[token][0] + 1, self.corpus[token][1]
else:
if sent != 0:
self.corpus[token] = 1, 1
else:
self.corpus[token] = 1, 0
print 'Calculating unigram probability.'
# Corpus created, calculate words probability of sentiment based on frequency
self.probWord = {}
for token in self.corpus.keys():
self.probWord[token] = float(self.corpus[token][1]) / self.corpus[token][0]
# print token, ' || ',corpus[token][1],' / ',corpus[token][0],' = ', probWord[token], '\n'
def calcProbability(self):
# Probability of sentiment per word calculated, estimate sentence probability of sentiment
self.probSent = {}
for i in self.trainSet:
p = 1
tk_sent = nltk.tokenize.word_tokenize( self.sentence[i] )
for token in tk_sent:
p = p + self.probWord[token]
self.probSent[i] = p / float(len(tk_sent)) # to be extra certain intdiv does not occur
# print i, 'PROB', self.probSent[i], 'SENT', self.sentiment[i]
def trainSingleInputPerceptron(self):
print 'Training perceptron.'
ssv = [x != 0 for x in self.sentiment.values()]
trainingSet = {}
for i in self.trainSet:
trainingSet[(self.probSent[i],)] = ssv[i]
self.p.train(trainingSet)
print 'Found threshold: ', self.p.threshold / self.p.weights[0]
print 'Validating perceptron.'
# Calculate probability for test sentences
for i in self.testSet:
p = 1
tk_sent = nltk.tokenize.word_tokenize( self.sentence[i] )
for token in tk_sent:
try:
p = p + self.probWord[token]
except:
# If word does not occur in corpus, ignore for now
# (can try katz backoff later?)
pass
# Store the probability in dictionary
self.probSent[i] = p / float(len(tk_sent)) # to be extra certain intdiv does not occur
# print i, 'PROB', self.probSent[i], 'SENT', self.sentiment[i]
def trainMultiInputPerceptron(self):
ssv = [x != 0 for x in self.sentiment.values()]
print "Number of inputs = ", len(self.bagOfWords)
# Create trainingset for perceptron
trainingSet = {}
for i in self.trainSet:
trainingSet[tuple(self.wordVectors[i])] = ssv[i]
# Initialise weights on word probability
print "Setting weights"
self.p.set_weights([self.probWord[token] for token in self.bagOfWords])
# Train the perceptron
print 'Training perceptron'
self.p.train(trainingSet,0.1,100)
def testMultiInputPerceptron(self):
# Initialize confusion matrix
confusion = {}
confusion["tp"] = 0
confusion["tn"] = 0
confusion["fp"] = 0
confusion["fn"] = 0
print "Testing perceptron"
for i in self.testSet:
# Tokenize sentence
tk_sentence = nltk.tokenize.word_tokenize( self.sentence[i] )
# Create word vector
vec = [ (x in tk_sentence) for x in self.bagOfWords]
# Let the perceptron do its work
out = self.p.output(vec)
if out:
if self.sentiment[i] == 0:
confusion["fp"] += 1
else:
confusion["tp"] += 1
else:
if self.sentiment[i] == 0:
confusion["tn"] += 1
else:
confusion["fn"] += 1
# Print results
print 'Results for test set: '
print confusion
try:
acc = float(confusion["tp"] + confusion["tn"]) / (confusion["tp"] + confusion["tn"] + confusion["fp"] + confusion["fn"])
except:
acc = 0
print 'accuracy = ', acc
try:
pre = float(confusion["tp"]) / (confusion["tp"] + confusion["fp"] )
except:
pre = 0
print 'precision = ', pre
def printResults(self):
t = self.p.threshold / self.p.weights[0]
confusion = {}
confusion["tp"] = 0
confusion["tn"] = 0
confusion["fp"] = 0
confusion["fn"] = 0
for i in self.testSet:
if self.probSent[i] > t:
if self.sentiment[i] == 0:
confusion["fp"] += 1
else:
confusion["tp"] += 1
if self.probSent[i] < t:
if self.sentiment[i] == 0:
confusion["tn"] += 1
else:
confusion["fn"] += 1
# print 'Results for test set: '
# print confusion
acc = float(confusion["tp"] + confusion["tn"]) / (confusion["tp"] + confusion["tn"] + confusion["fp"] + confusion["fn"])
# print 'accuracy = ', acc
pre = float(confusion["tp"]) / (confusion["tp"] + confusion["fp"] )
# print 'precision = ', pre
return (acc, pre)
def createWordVectors(self):
for i in self.trainSet:
# Tokenize sentence
tk_sentence = nltk.tokenize.word_tokenize( self.sentence[i] )
# Create word vector
vec = [ (x in tk_sentence) for x in self.bagOfWords]
# for word in self.bagOfWords:
# if word in tk_sentence:
# vec.append(1)
# else:
# vec.append(0)
self.wordVectors[i] = vec
m = Main(10)