-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempSentAnalysis.py
More file actions
223 lines (181 loc) · 7.09 KB
/
Copy pathTempSentAnalysis.py
File metadata and controls
223 lines (181 loc) · 7.09 KB
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
from operator import attrgetter
import re
from math import log10
import time
import copy
punct = [".", ",", ":", ";", "!", "$", "^", "*", "(", ")", "[", "]", "{", "}", "-", "", "&", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "0", "%", "+", "/", "\\"]
# sets up the feature class the represent a word with a list of whether it is in a sentence - index corresponding to
# the sentence number - a boolean of inVocab is ensure there are no duplicate words, and attributes for the probability
# distribution table.
class Feature:
def __init__(self, word, inSentence, inVocab):
self.word = word
self.inSentence = inSentence
self.inVocab = inVocab
self.falsefalse = 0
self.falsetrue = 0
self.truefalse = 0
self.truetrue = 0
# sets up the class label class with a list of whether the sentence is positive or negative - index correspdoning to
# the sentence number - and two other attributes to keep track of the probability that sentence is positive or negative.
class Label:
def __init__(self):
self.word = "classlabel"
self.reviewType = []
self.probNegative = 0
self.probPositive = 0
# This strips all the punctuation out of the sentences and only grabs with words with "'"s and words without any other
# punctuation specified in the punct list. All words with "'"s are then stripped of the "'" and put back into the set.
# It is then sorted alphabetically. The class label is also then generated along side the punctuation stripping.
def preprocess(name, label, label2):
print("Processing", name)
f = open(name, "r")
lineList = f.readlines()
f.close()
vocab = []
for line in lineList:
a = line.split("\t")
for i in punct:
a[0] = a[0].replace(i, "")
a[1] = a[1].strip()
if name == "trainingSet.txt":
label.reviewType.append(a[1])
elif name == "testSet.txt":
label2.reviewType.append(a[1])
for b in a[0].split(" "):
b = b.replace("'", "")
if b == "":
continue
newFeat = Feature(b.lower(), [], False)
if len(vocab) == 0:
vocab.append(newFeat)
for j in vocab:
if j.word == newFeat.word:
newFeat.inVocab = True
break
if not newFeat.inVocab:
vocab.append(newFeat)
vocab = sorted(vocab, key=attrgetter('word'))
for line in lineList:
for i in punct:
line = line.replace(i, "")
line = line.lower()
for j in vocab:
if re.search(r"\b" + j.word + r"\b", line) is not None:
j.inSentence.append(1)
else:
j.inSentence.append(0)
return vocab
# generates the output files based on the preprocessed data and formatted according to the assignment
def writeout(name):
f = open(name, "w")
if name == "preprocessed_train.txt":
for i in trainVocab:
f.write(i.word)
f.write(", ")
f.write("classlabel\n")
sentenceCount = len(i.inSentence)
for i in range(sentenceCount):
for j in trainVocab:
f.write(str(j.inSentence[i]))
f.write("\t")
f.write(trainCL.reviewType[i])
f.write("\n")
elif name == "preprocessed_test.txt":
for i in testVocab:
f.write(i.word)
f.write(", ")
f.write("classlabel\n")
sentenceCount = len(i.inSentence)
for i in range(sentenceCount):
for j in testVocab:
f.write(str(j.inSentence[i]))
f.write("\t")
f.write(testCL.reviewType[i])
f.write("\n")
f.close()
# generates prediction based on the training data using the inference Naive bayes' formula as well as UDP
def predict(name):
print("Predicting", name)
f = open(name, "r")
lineList = f.readlines()
f.close()
predictions = []
counter = 1
for line in lineList:
a = line.split("\t")
for i in punct:
a[0] = a[0].replace(i, "")
a[0] = a[0].lower()
a[0] = a[0].replace("'", "")
clTrue = []
clFalse = []
for trainWord in trainVocab:
if re.search(r"\b" + trainWord.word + r"\b", a[0]) is not None:
clTrue.append(log10(trainWord.truetrue))
clFalse.append(log10(trainWord.truefalse))
else:
clTrue.append(log10(trainWord.falsetrue))
clFalse.append(log10(trainWord.falsefalse))
resultTrue = log10(trainCL.probPositive) + sum(clTrue)
resultFalse = log10(trainCL.probNegative) + sum(clFalse)
if resultTrue > resultFalse:
#print("prediction for sentence:", counter),
# print("positive")
# print(resultTrue, resultFalse)
predictions.append('1')
else:
#print("prediction for sentence:", counter),
# print("negative")
# print(resultTrue,resultFalse)
predictions.append('0')
counter += 1
f = open("results.txt", "a")
if name == "testSet.txt":
print("Accuracy on testSet:")
right = 0
for i in range(len(predictions)):
if predictions[i] == testCL.reviewType[i]:
right += 1
acc = right / len(predictions)
f.writelines("Accuracy on testSet: ")
f.writelines(str(acc))
f.writelines("\n")
print(acc)
elif name == "trainingSet.txt":
right = 0
for i in range(len(predictions)):
if predictions[i] == trainCL.reviewType[i]:
right += 1
acc = right / len(predictions)
f.writelines("Accuracy on trainingSet: ")
f.writelines(str(acc))
f.writelines("\n")
print("Accuracy on trainingSet:")
print(acc)
trainCL = Label()
testCL = Label()
trainVocab = preprocess("trainingSet.txt", trainCL, testCL)
testVocab = preprocess("testSet.txt", trainCL, testCL)
trainCL.probNegative = trainCL.reviewType.count("0") / len(trainCL.reviewType) # P(CD = false)
trainCL.probPositive = trainCL.reviewType.count("1") / len(trainCL.reviewType) # P(CD = true)
writeout("preprocessed_train.txt")
writeout("preprocessed_test.txt")
for i in trainVocab:
for j in range(len(i.inSentence)):
if i.inSentence[j] == 0 and trainCL.reviewType[j] == '0':
i.falsefalse += 1
elif i.inSentence[j] == 1 and trainCL.reviewType[j] == '0':
i.truefalse += 1
elif i.inSentence[j] == 0 and trainCL.reviewType[j] == '1':
i.falsetrue += 1
elif i.inSentence[j] == 1 and trainCL.reviewType[j] == '1':
i.truetrue += 1
for i in trainVocab:
i.falsefalse = ((i.falsefalse + 1) / (trainCL.reviewType.count('0') + 2))
i.falsetrue = ((i.falsetrue + 1) / (trainCL.reviewType.count('1') + 2))
i.truefalse = ((i.truefalse + 1) / (trainCL.reviewType.count('0') + 2))
i.truetrue = ((i.truetrue + 1) / (trainCL.reviewType.count('1') + 2))
predict("testSet.txt")
predict("trainingSet.txt")