alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【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:

1
pip install textblob

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

1
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:

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
>>> 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:

1
pip install vaderSentiment

 

4. Steps for Sentiment Analysis Python using Vader

1
2
3
4
5
6
7
8
>>> 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
1
2
3
4
5
6
7
8
9
10
>>> 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

posted on   McDelfino  阅读(700)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-01-10 【287】◀▶ arcpy 常用类说明
2012-01-10 为小7文件夹右键添加“复制到”“移动到”
点击右上角即可分享
微信分享提示