-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextSentimentAnalysis_Sentence_based_input.py
71 lines (51 loc) · 1.5 KB
/
TextSentimentAnalysis_Sentence_based_input.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
# coding: utf-8
# In[25]:
import nltk
import csv
positiveWords=[]
negativeWords=[]
#Loading all negative words to a list
with open("G:\DIGI_Learning\UDEMY\Natural_Language_Processing\words-negative.csv","rb") as file:
reader = csv.reader(file)
for row in reader:
#print row
negativeWords.append(row)
#negativeWords = negativeWords[:10]
#print "Negative ==>",negativeWords
#Loading all positive words to a list
with open("G:\DIGI_Learning\UDEMY\Natural_Language_Processing\words-positive.csv","rb") as file:
reader = csv.reader(file)
for row in reader:
#print row
positiveWords.append(row)
#positiveWords = positiveWords[:10]
#print "Positive ==>",positiveWords
def sentimentOfText(text):
negativeCount = 0
positiveCount = 0
tokens = nltk.word_tokenize(text)
#print tokens
for item in tokens:
for positem in positiveWords:
if item in positem:
positiveCount += 1
for negitem in negativeWords:
if item in negitem:
negativeCount += 1
if positiveCount > 0:
print "positive"
elif negativeCount > 0 and negativeCount %2 == 0:
print "positive"
elif negativeCount > 0 :
print "negative"
else:
print "neutral"
# In[23]:
sentimentOfText("It was terribly bad")
# In[26]:
sentimentOfText("ACtually, it was not bad at all")
# In[27]:
sentimentOfText("This is sentence about nothing")
# In[29]:
sentimentOfText("God bless America")
# In[ ]: