Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -480,6 +480,32 @@ class SentimentAnalyzer:
|
|
480 |
self.has_textblob = False
|
481 |
self.has_vader = False
|
482 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
483 |
def analyze_article(self, article: Dict[str, str]) -> Dict[str, Any]:
|
484 |
"""Analyze sentiment and generate summary for an article."""
|
485 |
try:
|
|
|
480 |
self.has_textblob = False
|
481 |
self.has_vader = False
|
482 |
|
483 |
+
def analyze(self, text: str) -> str:
|
484 |
+
"""Analyze sentiment of text and return sentiment label."""
|
485 |
+
try:
|
486 |
+
# Get ensemble sentiment analysis
|
487 |
+
sentiment_analysis = self._get_ensemble_sentiment(text)
|
488 |
+
return sentiment_analysis['ensemble_sentiment']
|
489 |
+
except Exception as e:
|
490 |
+
print(f"Error in sentiment analysis: {str(e)}")
|
491 |
+
return 'neutral' # Default to neutral on error
|
492 |
+
|
493 |
+
def get_overall_sentiment(self, articles: List[Dict[str, Any]]) -> str:
|
494 |
+
"""Get overall sentiment from a list of articles."""
|
495 |
+
try:
|
496 |
+
# Combine all article texts
|
497 |
+
combined_text = ' '.join([
|
498 |
+
f"{article.get('title', '')} {article.get('content', '')}"
|
499 |
+
for article in articles
|
500 |
+
])
|
501 |
+
|
502 |
+
# Get ensemble sentiment analysis
|
503 |
+
sentiment_analysis = self._get_ensemble_sentiment(combined_text)
|
504 |
+
return sentiment_analysis['ensemble_sentiment']
|
505 |
+
except Exception as e:
|
506 |
+
print(f"Error getting overall sentiment: {str(e)}")
|
507 |
+
return 'neutral' # Default to neutral on error
|
508 |
+
|
509 |
def analyze_article(self, article: Dict[str, str]) -> Dict[str, Any]:
|
510 |
"""Analyze sentiment and generate summary for an article."""
|
511 |
try:
|