一、说明
二、亮相工具库
2.1. 工具库TextBlob介绍:
图像。TextBlob: Simplified Text Processing — TextBlob 0.16.0 documentation
TextBlob 是一个 python 库,可用于多个自然语言处理 (NLP) 任务,例如:
- 名词短语提取
- 词性标记
- 情绪分析
- 分类
- 标记化
- 单词和短语频率
- 解析
- n 元语法
- 词形变化(复数和单数化)和词形还原
- 拼写更正
#VADER 图像
2.2. 工具库VADER介绍:
VADER(Valence Aware Dictionary and sEntiment Reasoner)是一种基于词典和规则的情感分析工具,(Lexicon 意味着 NLP 系统的组件,其中包含有关每个单词或单词字符串的语义或语法等信息。例如。“无击球手”,“前进跑”和“巴尔的摩排骨”等是棒球词典的一部分,“边界”,“死亡结束”,“杜斯拉”等是板球词典的一部分。这是根据社交媒体的表达进行训练的。
词典情绪分析输出从 -1 到 1 的极性分数,其中 -1 表示真正的负面情绪,1 表示真正积极的情绪。接近 0 的值表示中性情绪。
三、TextBlob对比VADER:
TextBlob和VADER之间的关键区别在于VADER专注于社交媒体。因此,VADER 投入了大量精力来识别通常出现在社交媒体上的内容的情绪,例如表情符号、重复单词和标点符号。
现在,我们将在情感分析中比较 VADER 和 TextBlob,因为 VADER 是仅用于情感分析的库。
3.1 步骤:
让我们首先安装这两个库。以下代码片段将帮助我们进行安装。
!pip install textblob
!pip install vaderSentiment
现在,让我们导入库。
#Importing libraries
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
现在,让我们创建函数:
#Define funcitons for VADER and textblob
def vader_score(text):
#After using VADER we will get 4 values: pos, compound, neu and neg.
#pos:positive, neu:neutral, neg:negative
#Here we are only collecting the compound. Why?
#Because compound score is computed by summing the valence scores of each word in the lexicon,
#adjusted according to the rules, and then normalized to be between -1 (most extreme negative) and +1 (most extreme positive).
vader_sentiment = SentimentIntensityAnalyzer()
score = vader_sentiment.polarity_scores(text)
return score['compound']
def textblob_score(text):
#textblob_sentiment.sentiment will give us 2 values: polarity and subjectivity
#The polarity score is a float within the range [-1.0, 1.0].
#The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.
# Here we are interested in polarity, so we are using polaroty
textblob_sentiment = TextBlob(text)
score = textblob_sentiment.sentiment.polarity
return score
让我们看一下每种情绪(积极、消极和中性)的示例。
让我们创建一个包含 3 个句子的列表,每个句子有一个肯定的、一个否定的和一个中性的句子。所以我们可以看到,VADER和TextBlob在它们上的表现。
#List of sentences with different emotions.
#First sentence is neutral.
#second is positive and
#third is negative.
text_list = ["This is my first ever post on the internet.",
"I am very excited to write this post.",
"It's not good to work late hours."]
现在让我们创建一个 for 循环,它将遍历每个句子,并为我们提供之前创建的 “vader_score” 和 “textblob_score” 函数的输出。
#Here it will iterate through every sentence from the text_list list
#and will output the sentence first and on the next line it will
#print the vader score and in the next line it will print the
#textblob score.
for text in text_list:
print(f'sentence: {text} \n VADER sentiment score: {vader_score(text)} \n TextBlob score: {textblob_score(text)}')
print("=" * 30)
我们将得到这样的输出。
sentence: This is my first ever post on the internet.
VADER sentiment score: 0.0
TextBlob score: 0.25
==============================
sentence: I am very excited to write this post.
VADER sentiment score: 0.4005
TextBlob score: 0.48750000000000004
==============================
sentence: It's not good to work late hours.
VADER sentiment score: -0.3412
TextBlob score: -0.32499999999999996
==============================
从上面的单元格中,我们可以得出结论,VADER完美地将第一句话识别为中性句子,而TextBlob离它并不远。然后对于第二句话,VADER给出了正分数,但TextBlob给了我们一个更积极的分数。对于最后一句话,VADER给出的负分比TextBlob更高。
现在,我们可以说VADER和TextBlob都给出了相似的分数。
现在,由于它看起来很棒,我们需要对句子进行一些更改以进一步深化我们的研究。例如。我们可以添加表情符号,标点符号,大写,重复等。然后我们将看到哪个表现更好。
检查标点符号的影响:
#Adding punctuations
text_list = ["This is my first ever post on the internet!",
"I am very excited to write this post!",
"It's not good to work late hours!"]
sentence: This is my first ever post on the internet!
VADER sentiment score: 0.0
TextBlob score: 0.3125
==============================
sentence: I am very excited to write this post!
VADER sentiment score: 0.4561
TextBlob score: 0.609375
==============================
sentence: It's not good to work late hours!
VADER sentiment score: -0.4015
TextBlob score: -0.3625
==============================
现在,从上面的单元格中,我们可以说,感叹号确实提高了我们在所有句子中的分数。但是对于我们的中性句子(句子 1),TextBlob 走得更远了。
检查大写的影响:
#Capitalizing words
text_list = ["This is my FIRST EVER post on the internet!",
"I am very EXCITED to write this post!",
"It's NOT GOOD to work late hours!"]
sentence: This is my FIRST EVER post on the internet!
VADER sentiment score: 0.0
TextBlob score: 0.3125
==============================
sentence: I am very EXCITED to write this post!
VADER sentiment score: 0.5744
TextBlob score: 0.609375
==============================
sentence: It's NOT GOOD to work late hours!
VADER sentiment score: -0.5007
TextBlob score: -0.3625
==============================
现在,我们可以说,我们的VADER分数提高了,但TextBlob分数保持不变。为什么会这样?好吧,原因是VADER认为大写版本具有更强的情绪并增加了情绪得分。同时,TextBlob 没有区分单词的大写和小写版本之间的情绪。
检查重复单词的影响:
text_list = ["This is my VERY VERY FIRST EVER post on the internet!",
"I am very very EXCITED to write this post!",
"It's NOT NOT NOT GOOD to work late hours!"]
sentence: This is my VERY VERY FIRST EVER post on the internet!
VADER sentiment score: 0.0
TextBlob score: 0.40625
==============================
sentence: I am very very EXCITED to write this post!
VADER sentiment score: 0.6119
TextBlob score: 0.609375
==============================
sentence: It's NOT NOT NOT GOOD to work late hours!
VADER sentiment score: -0.3311
TextBlob score: -0.3625
==============================
通过重复的单词,我们可以看到VADER分数的明显变化,但这不会影响TextBlob分数。这是为什么呢?简单地说,这是因为VADER认为重复的单词具有更强的情感,而TextBlob没有解释重复的单词。
检查表情符号的影响:
text_list = ["This is my VERY VERY FIRST EVER post on the internet🫡🤔!",
"I am very very EXCITED to write this post😍😇!",
"It's NOT NOT NOT GOOD to work late hours☹️😳!"]
sentence: This is my VERY VERY FIRST EVER post on the internet🫡🤔!
VADER sentiment score: 0.0
TextBlob score: 0.40625
==============================
sentence: I am very very EXCITED to write this post😍😇!
VADER sentiment score: 0.8749
TextBlob score: 0.609375
==============================
sentence: It's NOT NOT NOT GOOD to work late hours☹️😳!
VADER sentiment score: -0.5802
TextBlob score: -0.3625
==============================
我们可以清楚地看到,维德分数正在提高。但是 TextBlob 分数根本没有变化。
四、结论:
根据我们所做的实验,这是否意味着VADER库比TextBlob更好?答案是否定的。但是为什么?因为:
TextBlob 可以做的不仅仅是情绪分类。
VADER-情感分析主要针对基于社交媒体数据的情感分析进行训练。TextBlob 的情况并非如此。
因此,我们不能得出VADER更好或TextBlob更好的结论。每个都有自己的用例。您可以根据问题陈述选择要使用的一个。
哈沙德·帕蒂尔