【问题标题】:Cannot update VADER lexicon无法更新 VADER 词典
【发布时间】:2019-02-22 16:13:51
【问题描述】:

print(news['title'][5]) 秘鲁-厄瓜多尔边境地区发生 7.5 级地震 - 印度教

print(analyser.polarity_scores(news['title'][5])) {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}

from nltk.tokenize import word_tokenize, RegexpTokenizer

import pandas as pd

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


analyzer = SentimentIntensityAnalyzer()


sentence = news['title'][5]

tokenized_sentence = nltk.word_tokenize(sentence)
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]

for word in tokenized_sentence:
    if (analyzer.polarity_scores(word)['compound']) >= 0.1:
        pos_word_list.append(word)
    elif (analyzer.polarity_scores(word)['compound']) <= -0.1:
        neg_word_list.append(word)
    else:
        neu_word_list.append(word)                

print('Positive:',pos_word_list)
print('Neutral:',neu_word_list)
print('Negative:',neg_word_list) 
score = analyzer.polarity_scores(sentence)
print('\nScores:', score)

正:[] 中性:['Magnitude', '7.5', 'quake', 'hits', 'Peru-Ecuador', 'border', 'region', '-', 'The', 'Hindu'] 否定:[]

分数:{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}

new_words = {
    'Peru-Ecuador': -2.0,
    'quake': -3.4,
}

analyser.lexicon.update(new_words)
print(analyzer.polarity_scores(sentence))

{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}

from nltk.tokenize import word_tokenize, RegexpTokenizer

import pandas as pd

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


analyzer = SentimentIntensityAnalyzer()


sentence = news['title'][5]

tokenized_sentence = nltk.word_tokenize(sentence)
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]

for word in tokenized_sentence:
    if (analyzer.polarity_scores(word)['compound']) >= 0.1:
        pos_word_list.append(word)
    elif (analyzer.polarity_scores(word)['compound']) <= -0.1:
        neg_word_list.append(word)
    else:
        neu_word_list.append(word)                

print('Positive:',pos_word_list)
print('Neutral:',neu_word_list)
print('Negative:',neg_word_list) 
score = analyzer.polarity_scores(sentence)
print('\nScores:', score)

正:[] 中性:['Magnitude', '7.5', 'quake', 'hits', 'Peru-Ecuador', 'border', 'region', '-', 'The', 'Hindu'] 否定:[]

分数:{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}

【问题讨论】:

    标签: nlp nltk sentiment-analysis natural-language-processing vader


    【解决方案1】:

    您使用的代码绝对没问题。更新字典时您使用了analyser而不是analyzer(不知道为什么您没有收到错误消息)。

    new_words = {
        'Peru-Ecuador': -2.0,
        'quake': -3.4,
    }
    ​
    analyzer.lexicon.update(new_words)
    print(analyzer.polarity_scores(sentence))
    

    输出:

    {'neg': 0.355, 'neu': 0.645, 'pos': 0.0, 'compound': -0.6597}
    

    再注意一点(不确定您是否犯了这个错误。) 您不应该再次导入该库。因为您更新的单词将消失。 步骤应该是:

    1. 导入库和字典
    2. 更新字典(此步骤后不应再次导入库)
    3. 计算情绪分数

    【讨论】:

    • 感谢您帮助我!我在那里犯的非常愚蠢的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 1970-01-01
    相关资源
    最近更新 更多