Sentiment analyzis.

This commit is contained in:
2025-07-21 12:59:32 +02:00
parent e80978681a
commit f95ab82a4a
7 changed files with 123 additions and 76 deletions

19
sentiment_analyzer.py Normal file
View File

@@ -0,0 +1,19 @@
# sentiment_analyzer.py
from nltk.sentiment.vader import SentimentIntensityAnalyzer
# Initialize the VADER sentiment intensity analyzer
# We only need to create one instance of this.
_analyzer = SentimentIntensityAnalyzer()
def get_sentiment_score(text):
"""
Analyzes a piece of text and returns its sentiment score.
The 'compound' score is a single metric that summarizes the sentiment.
It ranges from -1 (most negative) to +1 (most positive).
"""
# The polarity_scores() method returns a dictionary with 'neg', 'neu', 'pos', and 'compound' scores.
# We are most interested in the 'compound' score.
scores = _analyzer.polarity_scores(text)
return scores['compound']