Untitled
unknown
plain_text
6 months ago
881 B
4
Indexable
Never
import nltk from nltk.sentiment import SentimentIntensityAnalyzer # Create an instance of the SentimentIntensityAnalyzer sia = SentimentIntensityAnalyzer() # Define a function to perform sentiment analysis def analyze_sentiment(text): # Get the sentiment scores for the given text sentiment_scores = sia.polarity_scores(text) # Interpret the sentiment scores if sentiment_scores['compound'] >= 0.05: sentiment = 'Positive' elif sentiment_scores['compound'] <= -0.05: sentiment = 'Negative' else: sentiment = 'Neutral' # Return the sentiment and sentiment scores return sentiment, sentiment_scores # Example usage text = "I really enjoyed the movie. It was fantastic!" sentiment, sentiment_scores = analyze_sentiment(text) print("Text:", text) print("Sentiment:", sentiment) print("Sentiment Scores:", sentiment_scores)