How to do it...

  1. Create a new file and import the chosen packages:
import nltk.classify.utilfrom nltk.classify import NaiveBayesClassifierfrom nltk.corpus import movie_reviews
  1. Describe a function to extract features:
def collect_features(word_list):  word = []  return dict ([(word, True) for word in word_list])
  1. Adopt movie reviews in NLTK as training data:
if __name__=='__main__':  plus_filenum = movie_reviews.fileids('pos')  minus_filenum = movie_reviews.fileids('neg')
  1. Divide the data into positive and negative reviews:
  feature_pluspts = [(collect_features(movie_reviews.words(fileids=[f])),'Positive') for f in plus_filenum]  feature_minuspts = [(collect_features(movie_reviews.words(fileids=[f])),'Negative') for f in minus_filenum]

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.