forked from azk0019/CourseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_experiment.py
More file actions
114 lines (86 loc) · 3.81 KB
/
analysis_experiment.py
File metadata and controls
114 lines (86 loc) · 3.81 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
from typing import Text
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# textblob gives polarity(sentiment ranging from -1 to 1) and subjectivity (ranging 0 to 1) values.
# text1 = "The food at Bankok Thai is good"
# text2 = "Thanksgiving is comming soon"
# text3 = "The ball is blue"
# complex_text = "This does not taste good" # inaccurate when not is used
# blob1 = TextBlob(text1)
# blob2 = TextBlob(text2)
# blob3 = TextBlob(text3)
# blob_complex_text = TextBlob(complex_text)
# print(blob1.sentiment)
# print(blob2.sentiment)
# print(blob3.sentiment)
# print(blob_complex_text.sentiment)
pos_count = 0
pos_correct = 0
file_positive = open("positive.txt","r")
file_positive_lines = file_positive.readlines()
for line in file_positive_lines:
analysis = TextBlob(line)
# if analysis.sentiment.subjectivity >= 0.0001: # find optimal threshold
if analysis.sentiment.polarity > 0: # find optimal threshold
pos_correct += 1
pos_count +=1
neg_count = 0
neg_correct = 0
file_negative = open("negative.txt","r")
file_negative_lines = file_negative.readlines()
for line in file_negative_lines:
analysis = TextBlob(line)
#if analysis.sentiment.subjectivity > 0.0001: # find optimal threshold
if analysis.sentiment.polarity <= 0: # find optimal threshold
neg_correct += 1
neg_count +=1
print("Positive accuracy using textBlob= {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count))
print("Negative accuracy using textBlob= {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count))
print("Using vaderSentiment")
analyzer = SentimentIntensityAnalyzer()
# The Compound score is a metric that calculates the sum of all the lexicon ratings which have been normalized between -1(most extreme negative) and +1 (most extreme positive).
# positive sentiment : (compound score >= 0.05)
# neutral sentiment : (compound score > -0.05) and (compound score < 0.05)
# negative sentiment : (compound score <= -0.05)
pos_count = 0
pos_correct = 0
file_positive = open("positive.txt","r")
file_positive_lines = file_positive.readlines()
for line in file_positive_lines:
v = analyzer.polarity_scores(line)
if v['neg'] < 0.1:
if v['pos']-v['neg'] > 0 or v['compound'] >= 0.5:
pos_correct += 1
pos_count += 1
neg_count = 0
neg_correct = 0
file_negative = open("negative.txt","r")
file_negative_lines = file_negative.readlines()
for line in file_negative_lines:
v = analyzer.polarity_scores(line)
if v['pos'] < 0.1:
if v['pos']-v['neg'] <= 0 or v['compound'] <= -0.5: # The "=" makes it better
neg_correct += 1
neg_count += 1
print("Positive accuracy using vader= {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count))
print("Negative accuracy using vader= {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count))
print(" ")
def overall_sentiment(text):
v = SentimentIntensityAnalyzer()
percent_sentiment = v.polarity_scores(text)
print("Text is:", text)
print("Breakdown: ", percent_sentiment)
print("Text was rated as ", percent_sentiment['neg']*100, "% Negative")
print("Text was rated as ", percent_sentiment['neu']*100, "% Neutral")
print("Text was rated as ", percent_sentiment['pos']*100, "% Positive")
print("Sentence overall rated As", end = " ")
if percent_sentiment['neg'] < 0.1:
if percent_sentiment['pos']-percent_sentiment['neg'] > 0 or percent_sentiment['compound'] >= 0.5:
print("Positive")
elif percent_sentiment['pos'] < 0.1:
if percent_sentiment['pos']-percent_sentiment['neg'] <= 0 or percent_sentiment['compound'] <= -0.5: # The "=" makes it better
print("Negative")
else :
print("Neutral")
print(" ")
overall_sentiment("Yesterday was the worst day of my life.")