Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
881 B
5
Indexable
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)