我使用 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 词典中。
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 文件由两列组成。一个带有单词,另一个带有负数或正数。为什么它仍然被确定为中立?任何帮助将不胜感激。谢谢你。