Python 无法使用for循环在维德词典中添加新词。它在没有回路的情况下工作得很好。我如何解决这个问题?
我使用维德进行情绪分析。当我在维德词典中添加一个单词时,它会起作用,也就是说,它会根据我对单词给出的值来检测新添加的单词是肯定的还是否定的。代码如下:Python 无法使用for循环在维德词典中添加新词。它在没有回路的情况下工作得很好。我如何解决这个问题?,python,nltk,vader,Python,Nltk,Vader,我使用维德进行情绪分析。当我在维德词典中添加一个单词时,它会起作用,也就是说,它会根据我对单词给出的值来检测新添加的单词是肯定的还是否定的。代码如下: from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer sid_obj = SentimentIntensityAnalyzer() new_word = {'counterfeit':-2,'Good':2,} sid_obj.lexicon.update(n
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")
它适用于词典中添加的一个单词。当我尝试使用CSV文件通过使用下面的代码添加多个单词来执行相同操作时:我不会将单词伪冒添加到我的维德词典中
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', '.']
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
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文件由两列组成。一个带单词,另一个带负数或正数。为什么它仍然被认定为中立?任何帮助都将不胜感激。谢谢。解决了,谢谢。问题是我在字典里用大写字母写了我的文章。它总是应该用小写字母存储。词典中的单词必须以小写形式存储。因为维德在比较之前会将所有内容都转换为小写。即使您在字典中提到了假冒是一个单词,但如果您将其包含在示例
新单词
打印输出中,可能会减少混淆。另外,你能检查一下它在csv文件中的值是否为负值而不是中性值吗?@TheGamer007我已经检查过了,csv文件中的单词“假冒”是以-2作为值的。以防万一,我将CSV条目减少到10个条目,并尝试使用Cyberattack,这是第一个使用-2的条目,但仍然得到了相同的结果,即。Neutral@TheGamer007解决了,谢谢。问题是我在字典里用大写字母写了我的文章。它总是应该用小写字母存储。