File size: 1,652 Bytes
c1f8c52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
        }