-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsentiWordNet.py
65 lines (50 loc) · 1.38 KB
/
sentiWordNet.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
import nltk
infile = open('sentiWordNet.txt', 'r')
swnLines = infile.readlines()
infile.close()
swnPos = {}
swnNeg = {}
count = {}
i = 0
for item in swnLines:
swnLines[i]=swnLines[i].split('\t')
posScore = swnLines[i][2]
negScore = swnLines[i][3]
words = swnLines[i][4].split(' ')
for word in words:
j = word.find('#')
word = word[:j]
if word in count.keys():
count[word] += 1
swnPos[word] += float(posScore)
swnNeg[word] += float(negScore)
else:
count[word] = 1
swnPos[word] = float(posScore)
swnNeg[word] = float(negScore)
i += 1
for item in count:
swnPos[item] = swnPos[item]*1.0/count[item]
swnNeg[item] = swnNeg[item]*1.0/count[item]
def sentimentFeature(text):
posSentiSum = 0
negSentiSum = 0
tokens = nltk.word_tokenize(text)
noOfWords = len(text.split())
for word in tokens:
try:
posScore = swnPos[word]
negScore = swnNeg[word]
except KeyError:
posScore = 0.0
negScore = 0.0
posSentiSum += posScore
negSentiSum += negScore
posSenti = posSentiSum*1.0/noOfWords
negSenti = negSentiSum*1.0/noOfWords
return(round(posSenti,3), round(negSenti,3))
#print(sentimentFeature("""i hate abc"""))
#try:
# print(swnNeg[':'])
#except KeyError:
# print(0)