# Sentiment Analysis of Movie Reviews using Python

I am currently doing sentiment analysis using Python. Here I am taking all the reviews from movie dataset and using Naive Bayes algorithm to predict whether the review is positive or negative. From the input dataset, I am using a logic to remove stopwords and after that training my dataset to predict the result. As of now, my test results are predicted, but few results are predicted wrongly.

This was a great film. Jack Halley is comical as usual and Bela Lugos is  hilarious even in his tiny role. I love this flick.


Example, the above sentence is positive, but it is predicting as negative. Similar instance occurs to many other scenarios. If it has some sarcasm, then there are chances that it may predict the other way. But for few direct reviews, it is predicting the other way. Could you suggest other ways to better my algorithm, so that it increases the accuracy of prediction. Any other efficient methods apart from removal of stopwords? I have been looking for various suggestions through internet but I am new to this topic and hence couldn't find an effective way to proceed. Any good suggestions would be greatly appreciated.

• Could you post your ROC curve and confusion matrix for clarity? – krthkskmr Apr 16 '16 at 7:39

Given that exact case, I would assume that you are getting negative decision due to names, mentioned in the review (in your training dataset actors were more often met in negative reviews). You should, probably, remove all non-relevant words from reviews, and that includes not only stop words, but all person names (since they are less of a sentiment marker except, maybe, justin bieber, who is a really negative marker to anything :) ).

I'm assuming that you are using bag of words, you can try adding bigrams and/or trigrams (or really any other arbitrary n-grams) to your vocabulary.

I have also had a lot of success using latent dirichlet allocation to predict the distribution of topics for a particular sentence. Feeding that distribution into the naive bayes algorithm as features. It is something of a hack but it seems to work really well for the limited sentiment analysis that I have done. My guess is that the LDA does a decent job of picking up on sarcasm. It isn't full proof, but it might help you get better predictions. These are some of the things that I would try at the very least.

• I have a dataframe where whole reviews are in a particular column and polarity of the reviews in another column which says whether the review is positive or negative. I am feeding this as a input to training model using naive_bayes algorithm. In the test, I am just feeding the reviews as input and the model is predicting me polarity, whether the review is positive or negative – SRS Apr 18 '16 at 15:54
• Can LDA be used using sklearn? I am a beginner in python and it would be great if I get some reference to start with. – SRS Apr 18 '16 at 15:55
• Yes LDA is in sklearn it is a relatively new addition link – Ryan Apr 19 '16 at 17:45

Mainly the accuracy depends upon pre-processing steps, features extracted and the learning model used.

Pre-processing steps normally includes removal of stop words and that is fine. Features extraction is of various methods. Word embeddings is gaining its popularity in NLP, due to its interesting characteristics of vectors generated. Gensim provides a nice python library for word embeddings both word2vec as well as doc2vec models. For the detailed algorithm of how it works, read word2vec, doc2vec

There are lot of learning models from naive bayes, svm to neural network models. The accuracy of it depends upon the dataset used and the features generated and so each models need to be tested under trial and error method. sklearn provides a nice support for ML models.

Two starting places:

1. Do a Google Scholar search for "sentiment analysis" and read the papers from the past few years.

2. Work through Scikit-Learn's text classification tutorial. Copy the 20 Newsgroups classification code there and modify it for your task. This will give you a baseline to work from.