diff --git a/Machine Learning - Sentiment Analysis b/Machine Learning - Sentiment Analysis new file mode 100644 index 000000000000..7003857427a2 --- /dev/null +++ b/Machine Learning - Sentiment Analysis @@ -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}")