-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyzer.py
More file actions
53 lines (46 loc) · 1.56 KB
/
analyzer.py
File metadata and controls
53 lines (46 loc) · 1.56 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
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
class SentimentAnalyzer:
def __init__(self):
self.vader = SentimentIntensityAnalyzer()
def analyze_textblob(self, text):
"""Analyze text sentiment using TextBlob"""
analysis = TextBlob(text)
polarity = analysis.sentiment.polarity
# sentiment category
if polarity > 0.05:
sentiment = "Positive"
emoji = "😃"
elif polarity < -0.05:
sentiment = "Negative"
emoji = "😞"
else:
sentiment = "Neutral"
emoji = "😐"
return {
"sentiment": sentiment,
"polarity": polarity,
"emoji": emoji,
"subjectivity": analysis.sentiment.subjectivity
}
def analyze_vader(self, text):
"""Analyze text sentiment using VADER"""
scores = self.vader.polarity_scores(text)
# determine sentiment category based on compound score
if scores["compound"] >= 0.05:
sentiment = "Positive"
emoji = "😃"
elif scores["compound"] <= -0.05:
sentiment = "Negative"
emoji = "😞"
else:
sentiment = "Neutral"
emoji = "😐"
return {
"sentiment": sentiment,
"compound": scores["compound"],
"pos": scores["pos"],
"neu": scores["neu"],
"neg": scores["neg"],
"emoji": emoji
}