28
- April
2020
Posted By : Dikai Tang
NLP Sentiment Assessment using Python

0. Prerequisite

install basic nlp library nltk and textblob

pip install nltk
pip install textblob

download necessary packages for nltk

python -c "import nltk;nltk.download('brown');nltk.download('punkt')"

1. A simple example

Considering a simple senerio when others are expressing their altitude using most common phrases.

import nltk
from textblob import TextBlob
complaints = TextBlob("zoom bombers are so annoying, and the random glitches that wasted my time")
thanks = TextBlob("Thanks so much for the help last night, appreciate your patience")
print(complaints.sentiment)
#Sentiment(polarity=-0.5, subjectivity=0.4666666666666666)
print(thanks.sentiment)
#Sentiment(polarity=0.13333333333333333, subjectivity=0.15555555555555556)

2. Of course, it usually gets complicated

Well you know, people are more creative and sentimental than machines.

unknown = TextBlob("I told you so, you should never have done that. ")
print(unknown.sentiment_assessments)
# Sentiment(polarity=0.0, subjectivity=0.0, assessments=[])

But still, there are ways to understand a little better.

2.1 Train your own model using Naive Bayes

from textblob.classifiers import NaiveBayesClassifier
train = {
    ("how could you","neg"),
    ("never ever","neg"),
    ("I'm tired of ","neg"),
    ("I don't have time for","neg"),
    ("Why haven't you called","neg"),
    ("Alright, see you then","pos"),
    ("I'll be waiting for that","pos"),
    ("Fair enough","pos"),
    ("That will be fine","pos"),
}
cl = NaiveBayesClassifier(train)

2.2 Now try that again

cl.classify("I told you so, you should never have done that. ")
#'neg'
cl.prob_classify("I told you so, you should never have done that. ").__dict__
#{'_prob_dict': {'neg': -0.07376406916411682, 'pos': -4.326429210867413}, '_log': True}
cl.classify("cant wait for that")
#'pos'
cl.prob_classify("cant wait for that").__dict__
#{'_prob_dict': {'neg': -2.2057832597226756, 'pos': -0.3524864587156138}, '_log': True}

Reference

textblob