无法使用for循环在维德词典中添加新词。它在没有回路的情况下工作得很好。我该怎么解决这个问题?

2024-04-19 00:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我用维德分析情绪。当我在维德词典中添加一个单词时,它就起作用了,也就是说,它根据我给这个单词的值来检测新添加的单词是肯定的还是否定的。代码如下:

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文件通过使用下面的代码添加多个单词来执行相同操作时:我不会将单词forced添加到我的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文件由两列组成。一个是单词,另一个是负数或正数。为什么它仍然被认为是中立的?任何帮助都将不胜感激。非常感谢。你知道吗


Tags: objasrated单词dictsentencelistword
1条回答
网友
1楼 · 发布于 2024-04-19 00:22:01

解决了,谢谢。问题是我把我的文本放在字典里用大写。它总是应该用小写字母存储。词典中的单词必须用小写字母存储。因为维德在比较之前把所有的东西都转换成小写。你知道吗

相关问题 更多 >