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?
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
Need more clarity in your question. Are you talking about creating your own code base for calculating the sentiment?
TextBlob does NLP tasks like tokenization, sentiment analysis, POS tagging etc. Refer to the source code as to how the sentiment polarity & subjectivity is calculated.
You calculate the sentiment using TextBlob or Vader. Based on the polarity and subjectivity, you determine whether it is a positive text or negative or neutral. For TextBlog, if the polarity is >0, it is considered positive, <0 -is considered negative and ==0 is considered neutral. For vader sentiments, this is based on the compound score.
Then you train the classifier based on your sentiments (positive, negative, neutral) and proceed with prediction.
© 2022 - 2024 — McMap. All rights reserved.