我使用vader进行情感分析。当我在Vader词典中添加单个单词时,它可以正常工作,即它会根据我给该单词赋予的值将新添加的单词检测为正数或负数。代码如下:
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sid_obj = SentimentIntensityAnalyzer()
new_word = {'counterfeit':-2,'Good':2,}
sid_obj.lexicon.update(new_word)
sentence = "Company Caught Counterfeit."
sentiment_dict = sid_obj.polarity_scores(sentence)
tokenized_sentence = nltk.word_tokenize(sentence)
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]
for word in tokenized_sentence:
if (sid_obj.polarity_scores(word)['compound']) >= 0.1:
pos_word_list.append(word)
elif (sid_obj.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)
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")
输出如下:
Positive: []
Neutral: ['Company', 'Caught', '.']
Negative: ['Counterfeit']
Overall sentiment dictionary is : {'neg': 0.6, 'neu': 0.4, 'pos': 0.0, 'compound': -0.4588}
sentence was rated as 60.0 % Negative
sentence was rated as 40.0 % Neutral
sentence was rated as 0.0 % Positive
Sentence Overall Rated As Negative
它非常适合词典中添加的一个单词。当我尝试通过使用以下代码添加多个单词来使用CSV文件执行相同操作时:我没有在Vader Lexicon中添加单词Counterfeit。
new_word={}
import csv
with open('Dictionary.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
new_word[row['Word']] = int(row['Value'])
print(new_word)
sid_obj.lexicon.update(new_word)
以上代码的输出是一个字典,该字典已更新为词典。字典看起来像这样(它有大约2000个单词,但我只打印了几个单词)。它还包含伪造品作为一个单词:
{'CYBERATTACK': -2, 'CYBERATTACKS': -2, 'CYBERBULLYING': -2, 'CYBERCRIME':
-2, 'CYBERCRIMES': -2, 'CYBERCRIMINAL': -2, 'CYBERCRIMINALS': -2,
'MISCHARACTERIZATION': -2, 'MISCLASSIFICATIONS': -2, 'MISCLASSIFY': -2,
'MISCOMMUNICATION': -2, 'MISPRICE': -2, 'MISPRICING': -2, 'STRICTLY': -2}
输出如下:
Positive: []
Neutral: ['Company', 'Caught', 'Counterfeit', '.']
Negative: []
Overall sentiment dictionary is : {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
sentence was rated as 0.0 % Negative
sentence was rated as 100.0 % Neutral
sentence was rated as 0.0 % Positive
Sentence Overall Rated As Neutral
向词典中添加多个单词时,我哪里出错了? CSV文件由两列组成。一个带有单词,另一个带有负或正数的值。为什么仍然被识别为中立?任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
解决了,谢谢。问题是我将文本放在大写的字典中。通常应该将其存储为小写。字典词必须以小写形式存储。因为Vader在比较之前会将所有内容都转换为小写。