Spaces:
Running
Running
Upload sentiment_analysis.py
#40
by
Raiff1982
- opened
- sentiment_analysis.py +37 -0
sentiment_analysis.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textblob import TextBlob
|
| 2 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
|
| 5 |
+
def analyze_sentiment_textblob(text: str) -> TextBlob:
|
| 6 |
+
"""Analyze the sentiment of the given text using TextBlob.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
text (str): The text to analyze.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
TextBlob: The sentiment analysis result from TextBlob.
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
blob = TextBlob(text)
|
| 16 |
+
sentiment = blob.sentiment
|
| 17 |
+
return sentiment
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error analyzing sentiment with TextBlob: {e}")
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
def analyze_sentiment_vader(text: str) -> Dict[str, Any]:
|
| 23 |
+
"""Analyze the sentiment of the given text using VADER.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
text (str): The text to analyze.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
dict: The sentiment analysis result from VADER.
|
| 30 |
+
"""
|
| 31 |
+
try:
|
| 32 |
+
analyzer = SentimentIntensityAnalyzer()
|
| 33 |
+
sentiment = analyzer.polarity_scores(text)
|
| 34 |
+
return sentiment
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error analyzing sentiment with VADER: {e}")
|
| 37 |
+
return {}
|