How does TextBlob calculate sentiment polarity? How can I calculate a value for sentiment with machine learning classifier?

Talal Najam picture Talal Najam · Jul 6, 2018 · Viewed 8.7k times · Source

how does TextBlob calculate an empirical value for the sentiment polarity. I have used naive bayes but it just predicts whether it is positive or negative. How could I calculate a value for the sentiment like TextBlob does?

Answer

Sangeetha James picture Sangeetha James · Sep 20, 2018

Here is an example from the site: https://textblob.readthedocs.io/en/dev/quickstart.html#sentiment-analysis

text1 = TextBlob("Today is a great day, but it is boring")
text1.sentiment.polarity
# You can derive the sentiment based on the polarity.

Here is a sample code of how I used TextBlob in tweets sentiments:

from textblob import TextBlob
### My input text is a column from a dataframe that contains tweets. 

def sentiment(x):
    sentiment = TextBlob(x)
    return sentiment.sentiment.polarity

tweetsdf['sentiment'] = tweetsdf['processed_tweets'].apply(sentiment)
tweetsdf['senti'][tweetsdf['sentiment']>0] = 'positive'
tweetsdf['senti'][tweetsdf['sentiment']<0] = 'negative'
tweetsdf['senti'][tweetsdf['sentiment']==0] = 'neutral'

Based on the polarity and how the sentences were really sounding, I ended up with the logic above. Note that this might not be the case for some tweets.

I personally found vader sentiments compound score to be making more sense so that I can figure out a range for positive, negative and neutral sentiments based on the compound score & the tweet text instead of just assigning postivie sentiment for all texts with polarity >0