Skip to content

Create Machine Learning - Sentiment Analysis.py #9059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 30 additions & 0 deletions Machine Learning - Sentiment Analysis
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

# Download VADER lexicon (if not already downloaded)
nltk.download('vader_lexicon')

# Initialize the sentiment analyzer
sid = SentimentIntensityAnalyzer()

def analyze_sentiment(text):
# Calculate sentiment scores
sentiment_scores = sid.polarity_scores(text)

# Determine sentiment label based on the compound score
compound_score = sentiment_scores['compound']
if compound_score >= 0.05:
return "Positive"
elif compound_score <= -0.05:
return "Negative"
else:
return "Neutral"

# Input text for analysis
text = "I love this product! It's amazing."

# Perform sentiment analysis
sentiment = analyze_sentiment(text)

# Display the sentiment result
print(f"Sentiment: {sentiment}")