-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotionClass.py
177 lines (151 loc) · 4.66 KB
/
emotionClass.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
#Patrick Wilson
#EECS 498 HW 3
#!/usr/bin/env python
import re
import sys
import os
import operator
import math
import cProfile
import numpy
from sentstotweets import sentstotweets
def spacePeriod(input):
final_string = ''
for tweet in input:
pattern = re.sub("\. "," . ",tweet)
final_string = final_string + pattern
return final_string
def spaceCommaApos(input):
pattern = re.sub("\,"," , ",input)
pattern = re.sub("\'"," ' ",pattern)
return pattern
def removePunc(input):
pattern = re.sub("\@","",input)
pattern = re.sub("\!","",pattern)
pattern = re.sub("\&","",pattern)
pattern = re.sub("\+","",pattern)
pattern = re.sub("\?","",pattern)
pattern = re.sub("',","",pattern)
pattern = re.sub("\'","",pattern)
pattern = re.sub("u'","",pattern)
pattern = re.sub("\$","",pattern)
pattern = re.sub("\(","",pattern)
pattern = re.sub("\)","",pattern)
pattern = re.sub("\#","",pattern)
pattern = re.sub("\*","",pattern)
pattern = re.sub("\/","",pattern)
pattern = re.sub("\.","",pattern)
pattern = re.sub("\,","",pattern)
pattern = re.sub("\=","",pattern)
pattern = re.sub("\-","",pattern)
pattern = re.sub("\_","",pattern)
pattern = re.sub("\:","",pattern)
pattern = re.sub("\;","",pattern)
pattern = re.sub("\"","",pattern)
pattern = re.sub("\[","",pattern)
pattern = re.sub("\]","",pattern)
pattern = re.sub(r"http","",pattern)
#pattern = re.sub("\\","",pattern)
return pattern
stopwordlist = ["a","all","an","and","any","are","as","be","been",
"but","by","few","for","have","he","her","here","him","his","how",
"i","in","is","it","its","many","me","my","none","of","on","or",
"our","she","some","the","their","them","there","they","that",
"this","us","was","what","when","where","which","who","why","will",
"with","you","your","to","at","from"]
def isStopword(word):
for stopword in stopwordlist:
if (word==stopword):
return True
return False
def removeStopWords(input):
remove = '|'.join(stopwordlist)
regex = re.compile(r'\b('+remove+r')\b', flags=re.IGNORECASE)
out = regex.sub("", input)
return out
def stem(readfile):
output = ''
word = ''
line = readfile
if line == '':
return
for c in line:
if c.isalpha():
word += c.lower()
else:
if word:
output += p.stem(word, 0,len(word)-1)
word = ''
output += c.lower()
return output
def populate(dictionary,words,num):
for word in words:
num+=1
if word in dictionary:
dictionary[word] += 1
else:
dictionary[word] = 1
return num
def calcProb(dic,words,okno,numwords,x):
num = math.log(okno)
for word in words:
if word in dic:
num += math.log(dic[word])
else:
num += math.log(float(1)/(x+numwords))
return num
def prepare(dic,totalnum,numwords,x):
for key in dic:
first = float((dic[key]+1))/(x+numwords)
dic[key]=first
def getEmotion(query):
queryList = query.split()
emotionlist = ['#love OR #attached OR #devotion', '#happy OR #elated',
'#amused OR #excited OR #firedup',
'#blessed OR #grateful', '#sad OR #depressed OR #heartbroken',
'#angry OR #mad OR #infuriated', '#afraid OR #scared OR #terrified',
'#humiliating OR #embarrassing OR #ashamed']
complete = {}
total = {}
totalwords = 0
wordsperclass = {}
count = 0
for words in emotionlist:
tempdic = {}
numwords=0
training = emotionlist[count]
count +=1
test_set = []
wordsofclass = ''
with open('tweetlists/' + training+".txt") as f:
for line in f:
test_set = line.split()
wordsofclass = wordsofclass + ' ' + line
wordsperclass[words] = wordsofclass
totalwords = populate(total,test_set,totalwords)
count = 0
for words in emotionlist:
tempdic = {}
numwords=0
training = emotionlist[count]
count+=1
readfile = spacePeriod(wordsperclass[training])
readfile = spaceCommaApos(readfile)
readfile = removePunc(readfile)
#readfile = removeStopWords(readfile)
test_set = readfile.split()
numwords = populate(tempdic,test_set,numwords)
prepare(tempdic,totalwords,len(total),numwords)
tempdic['numberofwordsinthisclass']=numwords
complete[words] = tempdic
problist = []
for word in emotionlist:
prob = calcProb(complete[word],queryList,float(1)/len(emotionlist),len(total),complete[word]['numberofwordsinthisclass'])
problist.append(prob)
num = 0
finalemotion = ''
for thing in emotionlist:
if (max(problist)==problist[num]):
finalemotion = thing
num+=1
return finalemotion