From 4efb11dc6a5b282d1062d6e8e0cb44c3fc6d04d9 Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 6 Oct 2025 07:35:13 +0000 Subject: [PATCH] feat: add sentiment analysis module using VADER with classify and score functions Implement a new sentiment.py module that provides VADER-based sentiment analysis. The module includes an `analyze_sentiment_vader` function that classifies text as Positive, Negative, or Neutral based on compound score thresholds (>=0.05, <=-0.05), and a convenience `analyze` function wrapping the analyzer instance. Returns compound score and detailed polarity scores alongside classification. --- sentiment.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sentiment.py diff --git a/sentiment.py b/sentiment.py new file mode 100644 index 0000000..1c1cc1c --- /dev/null +++ b/sentiment.py @@ -0,0 +1,35 @@ +import json +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer + +def analyze_sentiment_vader(text, analyzer): + """ + Analyzes text using VADER and returns a dictionary with the results. + + Args: + text (str): The text content to analyze. + analyzer (SentimentIntensityAnalyzer): An instantiated VADER analyzer. + + Returns: + dict: A dictionary containing the sentiment classification, compound score, + and detailed scores (positive, neutral, negative). + """ + scores = analyzer.polarity_scores(text) + compound_score = scores['compound'] + + if compound_score >= 0.05: + sentiment = 'Positive' + elif compound_score <= -0.05: + sentiment = 'Negative' + else: + sentiment = 'Neutral' + + return { + 'sentiment': sentiment, + 'score': compound_score, + 'details': scores + } + +vader_analyzer = SentimentIntensityAnalyzer() + +def analyze(content): + return analyze_sentiment_vader(content, vader_analyzer)