Classifying movies based on their reviews
Having gone through all the preparations, we are now ready to embark on a more advanced task: classifying movies based on their reviews. In this recipe, we will use a sentiment analyzer and Naïve Bayes classifier to classify the movies.
Getting ready
To execute this recipe, you will need NLTK
and JSON
. No other prerequisites are required.
How to do it…
It takes a bit of wrangling but ultimately the code is fairly easy to understand and follow (the nlp_classify.py
file):
# this is needed to load helper from the parent folder import sys sys.path.append('..') # the rest of the imports import helper as hlp import nltk import nltk.sentiment as sent import json @hlp.timeit def classify_movies(train, sentim_analyzer): ''' Method to estimate a Naive Bayes classifier to classify movies based on their reviews ''' nb_classifier = nltk.classify.NaiveBayesClassifier.train classifier = sentim_analyzer.train(nb_classifier, train) ...