Spaces:
Build error
Build error
Download analyzer.py from Jivan01/Sentiment_Analysis: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/Jivan01/Sentiment_Analysis/resolve/main/analyzer.py
- Command line
-
hf download hf://spaces/Jivan01/Sentiment_Analysis/analyzer.py
-
curl -L -o analyzer.py https://huggingface.co/spaces/Jivan01/Sentiment_Analysis/resolve/main/analyzer.py
1.65 kB
| 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 | |
| } |