19 lines
710 B
Python
19 lines
710 B
Python
# 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'] |