Install python into your local machine. Install TextBlob >= 8.0.
pip install -U textblob nltk
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.
We make another classifier by passing training data into the constructor for a NaiveBayesClassifier.
We can now classify self-assertive content utilizing the NaiveBayesClassifier.classify(text) approach.
Let’s check the accuracy on the test set.
We can also find the most informative features.
#STEP 1 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') ] #STEP 2 cl = NaiveBayesClassifier(train) #STEP 3 cl.classify("Their burgers are amazing") # "pos" cl.classify("I don't like their pizza.") # "neg" #STEP 4 cl.accuracy(test) #STEP 5 cl.show_informative_features(5)