-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecisionList.py
executable file
·290 lines (247 loc) · 10.1 KB
/
decisionList.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
#!/usr/bin/python
from math import log
from dataParse import parseA, parseB
from collections import defaultdict
from operator import itemgetter
from checkTags import checkTagsB, checkTagsA
# ----------------------------------------------------------------- #
def getScores(scores,mfp):
alpha = 1
for word in mfp.keys():
wordScores = []
total = mfp[word]['count']
for polar in mfp[word].keys():
if polar != 'count':
count = mfp[word][polar]
score = log((count+alpha)/(float(total-count)+1*alpha))
wordScores.append((word,polar,score))
topScore = sorted(wordScores, key=itemgetter(2), reverse=True)[0]
scores[word]['polar'] = topScore[1]
scores[word]['score'] = topScore[2]
# ----------------------------------------------------------------- #
def getTags(rules,test,mfs):
tags = defaultdict(lambda: defaultdict(str))
pos = neg = obj = neut = 0
for ID in test.keys():
for index in test[ID].keys():
currentTweet = test[ID][index]['tweet'].split()
editedTweet = []
for word in currentTweet:
editedTweet.append(word.lower().strip('\'\"!@#$%^&*(),.?<>;:-'))
' '.join(editedTweet)
taggedFlag = False
for ruleSet in rules:
rule, polar, score = ruleSet[0], ruleSet[1], ruleSet[2]
if rule in editedTweet and score > 0:
tags[ID][index] = polar
taggedFlag = True
break
if not taggedFlag:
tags[ID][index] = 'neutral'
return tags
# ----------------------------------------------------------------- #
def getUnigrams(train):
mfp = defaultdict(lambda: defaultdict(int))
pos = neg = obj = neut = 0
for ID in train.keys():
for subj in train[ID].keys():
if train[ID][subj]['polar'] == 'positive': pos += 1
if train[ID][subj]['polar'] == 'negative': neg += 1
if train[ID][subj]['polar'] == 'neutral': neut += 1
if train[ID][subj]['polar'] == 'objective': obj += 1
tweet = train[ID][subj]['tweet'].split()
for word in tweet:
word1 = word.lower().strip('\'\"!@#$%^&*(),.?<>;:-')
polar = train[ID][subj]['polar']
mfp[word1][polar] += 1
mfp[word1]['count'] += 1
if pos > neg and pos > neut and pos > obj: mfs = 'positive'
elif neg > pos and neg > neut and neg > obj: mfs = 'negative'
elif neut > neg and neut > pos and neut > obj: mfs = 'neutral'
elif obj > neg and obj > neut and obj > pos: mfs = 'objective'
return mfp, mfs
# ----------------------------------------------------------------- #
def getBigrams(train):
mfp = defaultdict(lambda: defaultdict(int))
pos = neg = obj = neut = 0
for ID in train.keys():
for subj in train[ID].keys():
if train[ID][subj]['polar'] == 'positive': pos += 1
if train[ID][subj]['polar'] == 'negative': neg += 1
if train[ID][subj]['polar'] == 'neutral': neut += 1
if train[ID][subj]['polar'] == 'objective': obj += 1
tweet = train[ID][subj]['tweet'].split()
if len(tweet) >= 2:
for i in range(1,len(tweet)):
w1 = tweet[i-1].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w2 = tweet[i].lower().strip('\'\"!@#$%^&*(),.?<>;:')
bigram = w1 + ' ' + w2
polar = train[ID][subj]['polar']
mfp[bigram][polar] += 1
mfp[bigram]['count'] += 1
if pos > neg and pos > neut and pos > obj: mfs = 'positive'
elif neg > pos and neg > neut and neg > obj: mfs = 'negative'
elif neut > neg and neut > pos and neut > obj: mfs = 'neutral'
elif obj > neg and obj > neut and obj > pos: mfs = 'objective'
return mfp, mfs
def getTrigrams(train):
mfp = defaultdict(lambda: defaultdict(int))
pos = neg = obj = neut = 0
for ID in train.keys():
for subj in train[ID].keys():
if train[ID][subj]['polar'] == 'positive': pos += 1
if train[ID][subj]['polar'] == 'negative': neg += 1
if train[ID][subj]['polar'] == 'neutral': neut += 1
if train[ID][subj]['polar'] == 'objective': obj += 1
tweet = train[ID][subj]['tweet'].split()
if len(tweet) >= 3:
for i in range(2,len(tweet)):
w1 = tweet[i-1].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w2 = tweet[i].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w3 = tweet[i-2].lower().strip('\'\"!@#$%^&*(),.?<>;:')
trigram = w3 + ' ' + w1 + ' ' + w2
polar = train[ID][subj]['polar']
mfp[trigram][polar] += 1
mfp[trigram]['count'] += 1
if pos > neg and pos > neut and pos > obj: mfs = 'positive'
elif neg > pos and neg > neut and neg > obj: mfs = 'negative'
elif neut > neg and neut > pos and neut > obj: mfs = 'neutral'
elif obj > neg and obj > neut and obj > pos: mfs = 'objective'
return mfp, mfs
# ----------------------------------------------------------------- #
def unigramsBigrams(train):
mfp = defaultdict(lambda: defaultdict(int))
pos = neg = obj = neut = 0
for ID in train.keys():
for subj in train[ID].keys():
if train[ID][subj]['polar'] == 'positive': pos += 1
if train[ID][subj]['polar'] == 'negative': neg += 1
if train[ID][subj]['polar'] == 'neutral': neut += 1
if train[ID][subj]['polar'] == 'objective': obj += 1
tweet = train[ID][subj]['tweet'].split()
if len(tweet) >= 2:
for i in range(1,len(tweet)):
w1 = tweet[i-1].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w2 = tweet[i].lower().strip('\'\"!@#$%^&*(),.?<>;:')
bigram = w1 + ' ' + w2
polar = train[ID][subj]['polar']
mfp[bigram][polar] += 1
mfp[bigram]['count'] += 1
for word in tweet:
word1 = word.lower().strip('\'\"!@#$%^&*(),.?<>;:-')
polar = train[ID][subj]['polar']
mfp[word1][polar] += 1
mfp[word1]['count'] += 1
if pos > neg and pos > neut and pos > obj: mfs = 'positive'
elif neg > pos and neg > neut and neg > obj: mfs = 'negative'
elif neut > neg and neut > pos and neut > obj: mfs = 'neutral'
elif obj > neg and obj > neut and obj > pos: mfs = 'objective'
return mfp, mfs
def allGrams(train):
mfp = defaultdict(lambda: defaultdict(int))
pos = neg = obj = neut = 0
for ID in train.keys():
for subj in train[ID].keys():
if train[ID][subj]['polar'] == 'positive': pos += 1
if train[ID][subj]['polar'] == 'negative': neg += 1
if train[ID][subj]['polar'] == 'neutral': neut += 1
if train[ID][subj]['polar'] == 'objective': obj += 1
tweet = train[ID][subj]['tweet'].split()
if len(tweet) >= 3:
for i in range(2,len(tweet)):
w1 = tweet[i-1].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w2 = tweet[i].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w3 = tweet[i-2].lower().strip('\'\"!@#$%^&*(),.?<>;:')
trigram = w3 + ' ' + w1 + ' ' + w2
polar = train[ID][subj]['polar']
mfp[trigram][polar] += 1
mfp[trigram]['count'] += 1
if len(tweet) >= 2:
for i in range(1,len(tweet)):
w1 = tweet[i-1].lower().strip('\'\"!@#$%^&*(),.?<>;:')
w2 = tweet[i].lower().strip('\'\"!@#$%^&*(),.?<>;:')
bigram = w1 + ' ' + w2
polar = train[ID][subj]['polar']
mfp[bigram][polar] += 1
mfp[bigram]['count'] += 1
for word in tweet:
word1 = word.lower().strip('\'\"!@#$%^&*(),.?<>;:-')
polar = train[ID][subj]['polar']
mfp[word1][polar] += 1
mfp[word1]['count'] += 1
if pos > neg and pos > neut and pos > obj: mfs = 'positive'
elif neg > pos and neg > neut and neg > obj: mfs = 'negative'
elif neut > neg and neut > pos and neut > obj: mfs = 'neutral'
elif obj > neg and obj > neut and obj > pos: mfs = 'objective'
return mfp, mfs
# ----------------------------------------------------------------- #
def decisionList(train,test):
scores = defaultdict(lambda: defaultdict(int))
# get counts of the mfp (most frequent polarity)
mfp, mfs = getUnigrams(train)
#mfp, mfs = getBigrams(train)
#mfp, mfs = getTrigrams(train)
#mfp, mfs = unigramsBigrams(train)
#mfp, mfs = allGrams(train)
# get the highest score for each unigram and its associated polarity
getScores(scores,mfp)
scoresSort = []
for word in scores.keys():
scoresSort.append((word, scores[word]['polar'], scores[word]['score']))
scoresSort = sorted(scoresSort, key=itemgetter(2), reverse = True)
#for item in scoresSort[:80]:
#print "Word: %20s Polar: %10s Score:%0.2f" %(item[0],item[1],item[2])
#print ''
tags = getTags(scoresSort,test,mfs)
return tags
# ----------------------------------------------------------------- #
if __name__ == '__main__':
# file names
trainingFileA = 'trainA.txt'
trainingFileB = 'trainB.txt'
# parse training and testing data
trainA, testA = parseA(trainingFileA,5)
trainB, testB = parseB(trainingFileB,5)
# decision list
print '\nDECISION LIST Part 5'
tagsA = decisionList(trainA,testA)
tagsB = decisionList(trainB,testB)
checkTagsA(tagsA,testA)
checkTagsB(tagsB,testB)
# parse training and testing data
trainA, testA = parseA(trainingFileA,4)
trainB, testB = parseB(trainingFileB,4)
# decision list
print '\nDECISION LIST Part 4'
tagsA = decisionList(trainA,testA)
tagsB = decisionList(trainB,testB)
checkTagsA(tagsA,testA)
checkTagsB(tagsB,testB)
# parse training and testing data
trainA, testA = parseA(trainingFileA,3)
trainB, testB = parseB(trainingFileB,3)
# decision list
print '\nDECISION LIST Part 3'
tagsA = decisionList(trainA,testA)
tagsB = decisionList(trainB,testB)
checkTagsA(tagsA,testA)
checkTagsB(tagsB,testB)
# parse training and testing data
trainA, testA = parseA(trainingFileA,2)
trainB, testB = parseB(trainingFileB,2)
# decision list
print '\nDECISION LIST Part 2'
tagsA = decisionList(trainA,testA)
tagsB = decisionList(trainB,testB)
checkTagsA(tagsA,testA)
checkTagsB(tagsB,testB)
# parse training and testing data
trainA, testA = parseA(trainingFileA,1)
trainB, testB = parseB(trainingFileB,1)
# decision list
print '\nDECISION LIST Part 1'
tagsA = decisionList(trainA,testA)
tagsB = decisionList(trainB,testB)
checkTagsA(tagsA,testA)
checkTagsB(tagsB,testB)
# ----------------------------------------------------------------- #