Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the positive and negative words from a Textblob based on its polarity in Python (Sentimental analysis)

I have a text blob in which i am classifying the text as positive if polarity is > 0, neutral if = 0, and negative if < 0. How can i get the words based on which it is classifying as positive, negative or neutral?

like image 440
Learner Avatar asked Apr 09 '18 11:04

Learner


People also ask

How does TextBlob calculate polarity?

When computing a sentiment for a single word, TextBlob employs the “averaging” technique, which is applied to polarity values to calculate a polarity score for a single word, and thus a similar procedure applies to every single word, resulting in a combined polarity for larger texts.

What is polarity in TextBlob?

Textblob can be used for complex analysis and working with textual data. When a sentence is passed into Textblob it gives two outputs, which are polarity and subjectivity. Polarity is the output that lies between [-1,1], where -1 refers to negative sentiment and +1 refers to positive sentiment.

How do you find the polarity of a sentiment?

For calculating polarity of a text, polarity score of each word of the text, if present in the dictionary, is added to get an 'overall polarity score'. For example, if a lexicon matches a word marked as positive in the dictionary, then the total polarity score of the text is increased.


1 Answers

I hope the following code will help you:

from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
import nltk
nltk.download('movie_reviews')
nltk.download('punkt')

text          = "I feel the product is so good" 

sent          = TextBlob(text)
# The polarity score is a float within the range [-1.0, 1.0]
# where negative value indicates negative text and positive
# value indicates that the given text is positive.
polarity      = sent.sentiment.polarity
# The subjectivity is a float within the range [0.0, 1.0] where
# 0.0 is very objective and 1.0 is very subjective.
subjectivity  = sent.sentiment.subjectivity

sent          = TextBlob(text, analyzer = NaiveBayesAnalyzer())
classification= sent.sentiment.classification
positive      = sent.sentiment.p_pos
negative      = sent.sentiment.p_neg

print(polarity,subjectivity,classification,positive,negative)
like image 102
Sahli Simo Avatar answered Oct 31 '22 11:10

Sahli Simo