【520】利用 TextBlob & Vader 进行情感分析

参考:Tutorial: Quickstart - TextBlob (sentiment analysis)

参考:An overview of sentiment analysis python library: TextBlob

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

参考:Sentiment Analysis: VADER or TextBlob?

1. Installation of TextBlob

  Installation is not a big deal here. If you are already using CMD, you have to run this command to install TextBlob. Go to CMD and enter:

pip install textblob

  You need to download corpus first to train the model of TextBlob. You can achieve it using the following command:

python -m textblob.download_corpora

2. Steps for Sentiment Analysis Python using TextBlob

  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'

  another example:

>>> from textblob import TextBlob
>>> testimonial = TextBlob("My name is Alex")
>>> testimonial.sentiment.polarity
0.0
>>> testimonial = TextBlob("I feel a little headache")
>>> testimonial.sentiment.polarity
-0.1875
>>> testimonial = TextBlob("I can't remember anything")
>>> testimonial.sentiment.polarity
0.0
>>> testimonial = TextBlob("I feel so unhappy")
>>> testimonial.sentiment.polarity
-0.6
>>> testimonial = TextBlob("I really like this toy")
>>> testimonial.sentiment.polarity
0.2
>>> testimonial = TextBlob("I really want this toy")
>>> testimonial.sentiment.polarity
0.2
>>> testimonial = TextBlob("I really don't want this toy")
>>> testimonial.sentiment.polarity
0.2
>>> testimonial = TextBlob("I really don't like this toy")
>>> testimonial.sentiment.polarity
0.2
>>> testimonial = TextBlob("I really hate this toy")
>>> testimonial.sentiment.polarity
-0.8

3. Installation of Vader

  Go to CMD and enter:

pip install vaderSentiment

4. Steps for Sentiment Analysis Python using Vader

>>> from nltk.sentiment.vader import SentimentIntensityAnalyzer
>>> sid = SentimentIntensityAnalyzer()
>>> sid.polarity_scores("I like this movie")
{'neg': 0.0, 'neu': 0.444, 'pos': 0.556, 'compound': 0.3612}
>>> sid.polarity_scores("My name is Alex")
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
>>> sid.polarity_scores("My name is Alex and hate myself")
{'neg': 0.381, 'neu': 0.619, 'pos': 0.0, 'compound': -0.5719}
  • compound > 0, positive
  • compound < 0, negative
  • compound = 0, neutral
>>> def sentiment_vader(x):
	sentiment = SentimentIntensityAnalyzer()
	return sentiment.polarity_scores(x)['compound']

>>> sentiment_vader('I like this movie')
0.3612
>>> sentiment_vader('I donot know what to do now')
0.0
>>> sentiment_vader('I will go to school very early tomorrow and feel a little terrible')
-0.4228

原文地址:https://www.cnblogs.com/alex-bn-lee/p/14257912.html