Analyzing the sentiment of a sentence
Sentiment analysis refers to procedures of finding whether a specified part of text is positive, negative, or neutral. This technique is frequently considered to find out how people think about a particular situation. It evaluates the sentiments of consumers in different forms, such as advertising campaigns, social media, and e-commerce customers.
How to do it...
- Create a new file and import the chosen packages:
import nltk.classify.util from nltk.classify import NaiveBayesClassifier from nltk.corpus import movie_reviews
- Describe a function to extract features:
def collect_features(word_list): word = [] return dict ([(word, True) for word in word_list])
- Adopt movie reviews in NLTK as training data:
if __name__=='__main__': plus_filenum = movie_reviews.fileids('pos') minus_filenum = movie_reviews.fileids('neg')
- Divide the data into positive and negative reviews:
feature_pluspts = [(collect_features(movie_reviews.words(fileids=[f])), 'Positive') for f...