use nlp to analyze sentiment in python

To analyze sentiment with NLP in Python, we can use the Natural Language Toolkit (NLTK) library. Here's a simple example:

main.py
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

nltk.download('vader_lexicon')

# initialize analyzer
analyzer = SentimentIntensityAnalyzer()

# analyze sentiment
sentences = ["I love programming!", 
             "I hate Mondays.", 
             "The movie was okay.", 
             "I'm feeling really happy today.", 
             "I feel very sad about the news."]
for sentence in sentences:
    scores = analyzer.polarity_scores(sentence)
    print(f"{sentence}\nSentiment score: {scores['compound']:.2f}\n")
537 chars
18 lines

In this example, we initialize a SentimentIntensityAnalyzer from the NLTK library and then use it to analyze sentiment for a set of sample sentences. The polarity_scores() method returns a dictionary with positive, negative, neutral, and compound scores for the given sentence. The compound score is a metric that ranges from -1 (very negative) to +1 (very positive), with 0 indicating a neutral sentiment.

This is just a simple example, and there are more advanced techniques and libraries available for sentiment analysis in NLP. However, this code should provide a good starting point for your sentiment analysis needs.

gistlibby LogSnag