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)
正面:[]中立:['震级','7.5','地震','命中','秘鲁 - 厄瓜多尔','边界','地区',' - ',''','印度教']否定:[]
分数:{'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,'post':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)
正面:[]中立:['震级','7.5','地震','命中','秘鲁 - 厄瓜多尔','边界','地区',' - ',''','印度教']否定:[]
分数:{'neg':0.0,'neu':1.0,'pos':0.0,'compound':0.0}
您使用的代码绝对没问题。在更新字典时,你使用analyser
instead of 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}
另一个警告(不确定你是否犯了这个错误。)你不应该再次导入库。因为你更新的单词将会消失。步骤应该是: