To analyze sentiment with NLP in Python, we can use the Natural Language Toolkit (NLTK) library. Here's a simple example:
main.py537 chars18 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