Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ML/sentiment_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TextBlob library used for simple sentiment analysis
from textblob import TextBlob

def analyze_sentiment(text):
"""
Analyze sentiment of input text and return standardized output.
"""

blob = TextBlob(text)
polarity = blob.sentiment.polarity

if polarity > 0:
sentiment = "positive"
elif polarity < 0:
sentiment = "negative"
else:
sentiment = "neutral"

return {
"sentiment": sentiment,
"confidence_score": abs(polarity),
"text_summary": f"Detected {sentiment} sentiment from the given text."
}


if __name__ == "__main__":
sample = "I really love this product!"
result = analyze_sentiment(sample)
print(result)