📜  Python|使用 VADER 进行情绪分析

📅  最后修改于: 2022-05-13 01:55:40.334000             🧑  作者: Mango

Python|使用 VADER 进行情绪分析

情绪分析是“计算”确定一篇文章是正面、负面还是中性的过程。它也被称为意见挖掘,得出演讲者的意见或态度。
为什么要进行情绪分析?

  • 商业:在营销领域,公司使用它来制定策略,了解客户对产品或品牌的感受,人们对他们的活动或产品发布的反应以及消费者不购买某些产品的原因。
  • 政治:在政治领域,它用于跟踪政治观点,检测政府层面的声明和行动之间的一致性和不一致。它也可以用来预测选举结果! .
  • 公共行为:情绪分析还用于监控和分析社会现象,以发现潜在的危险情况并确定博客圈的总体情绪。

安装vaderSentiment的命令:

pip install vaderSentiment


VADER 情绪分析:
VADER(Valence Aware Dictionary and sEntiment Reasoner)是一种基于词典和规则的情感分析工具,专门针对社交媒体中表达的情感。 VADER使用组合 情感词典是词汇特征(例如,单词)的列表,这些特征通常根据其语义方向标记为正面或负面。 VADER不仅告诉我们积极性和消极性得分,还告诉我们情绪的积极或消极程度。
下面是代码:

Python3
# import SentimentIntensityAnalyzer class
# from vaderSentiment.vaderSentiment module.
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
 
# function to print sentiments
# of the sentence.
def sentiment_scores(sentence):
 
    # Create a SentimentIntensityAnalyzer object.
    sid_obj = SentimentIntensityAnalyzer()
 
    # polarity_scores method of SentimentIntensityAnalyzer
    # object gives a sentiment dictionary.
    # which contains pos, neg, neu, and compound scores.
    sentiment_dict = sid_obj.polarity_scores(sentence)
     
    print("Overall sentiment dictionary is : ", sentiment_dict)
    print("sentence was rated as ", sentiment_dict['neg']*100, "% Negative")
    print("sentence was rated as ", sentiment_dict['neu']*100, "% Neutral")
    print("sentence was rated as ", sentiment_dict['pos']*100, "% Positive")
 
    print("Sentence Overall Rated As", end = " ")
 
    # decide sentiment as positive, negative and neutral
    if sentiment_dict['compound'] >= 0.05 :
        print("Positive")
 
    elif sentiment_dict['compound'] <= - 0.05 :
        print("Negative")
 
    else :
        print("Neutral")
 
 
   
# Driver code
if __name__ == "__main__" :
 
    print("\n1st statement :")
    sentence = "Geeks For Geeks is the best portal for \
                the computer science engineering students."
 
    # function calling
    sentiment_scores(sentence)
 
    print("\n2nd Statement :")
    sentence = "study is going on as usual"
    sentiment_scores(sentence)
 
    print("\n3rd Statement :")
    sentence = "I am very sad today."
    sentiment_scores(sentence)


输出 :

复合分数是一个度量标准,它计算所有已在 -1(最极端负面)和 +1(最极端正面)之间归一化的词典评级的总和。
积极情绪:(复合得分 >= 0.05)
中性情绪:(复合得分 > -0.05)和(复合得分 < 0.05)
负面情绪:(复合得分 <= -0.05)