Building a Sentiment Analysis using Python

Getting Started.

Install python into your local machine. Install TextBlob >= 8.0.

pip install -U textblob nltk
TextBlob >= 8.0

TextBlob >= 8.0

Step 1
Our first classifier will be a basic sentiment analyzer trained on a sample tweets dataset. To start, we’ll import the textblob.classifiers and make some training and test data.

Step 2
We make another classifier by passing training data into the constructor for a NaiveBayesClassifier.

Step 3
We can now classify self-assertive content utilizing the NaiveBayesClassifier.classify(text) approach.

Step 4
Let’s check the accuracy on the test set.

Step 5
We can also find the most informative features.

from textblob.classifiers import NaiveBayesClassifier

train = [
    ('I love this sandwich.', 'pos'),
    ('This is an amazing place!', 'pos'),
    ('I feel very good about these beers.', 'pos'),
    ('This is my best work.', 'pos'),
    ("What an awesome view", 'pos'),
    ('I do not like this restaurant', 'neg'),
    ('I am tired of this stuff.', 'neg'),
    ("I can't deal with this", 'neg'),
    ('He is my sworn enemy!', 'neg'),
    ('My boss is horrible.', 'neg')
test = [
    ('The beer was good.', 'pos'),
    ('I do not enjoy my job', 'neg'),
    ("I ain't feeling dandy today.", 'neg'),
    ("I feel amazing!", 'pos'),
    ('Gary is a friend of mine.', 'pos'),
    ("I can't believe I'm doing this.", 'neg')

cl = NaiveBayesClassifier(train)

cl.classify("Their burgers are amazing")  # "pos"
cl.classify("I don't like their pizza.")  # "neg"


Sentiment Analysis output

Sentiment Analysis output