是否可以编辑 NLTK 的维达情绪词典? [英] Is it possible to edit NLTK's vader sentiment lexicon?
问题描述
我想将单词添加到 vader_lexicon.txt
以指定单词的极性分数.这样做的正确方法是什么?
I would like to add words to the vader_lexicon.txt
to specify polarity scores to a word. What is the right way to do so?
我在 AppData\Roaming\nltk_data\sentiment\vader_lexicon
中看到了这个文件.该文件由单词、它的极性、强度和由10 个独立的人类评估者"给出的 10 个强度分数的数组组成.[1] 但是,当我编辑它时,以下代码的结果没有任何变化:
I saw this file in AppData\Roaming\nltk_data\sentiment\vader_lexicon
. The file consists of the word, its polarity, intensity, and an array of 10 intensity scores given by "10 independent human raters". [1] However, when I edited it, nothing changed in the results of the following code:
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
s = sia.polarity_scores("my string here")
我认为当我调用 SentimentIntensityAnalyzer 的构造函数时,我的代码会访问这个文本文件.[2] 你对我如何编辑预制词典有什么想法吗?
I think that this text file is accessed by my code when I called SentimentIntensityAnalyzer's constructor. [2] Do you have any ideas on how I can edit a pre-made lexicon?
来源:
[1] https://github.com/cjhutto/vaderSentiment
[2] http://www.nltk.org/api/nltk.sentiment.html
推荐答案
对于任何感兴趣的人,这也可以实现而无需手动编辑 vader lexicon .txt 文件.一旦加载,词典就是一个普通的词典,以单词为键,以分数为值.正如 repoleved 所提供的edit-nltks-vader-sentiment-lexicon-without-modifying-a-txt-file">this 帖子:
For anyone interested, this can also be achieved without having to manually edit the vader lexicon .txt file. Once loaded the lexicon is a normal dictionary with words as keys and scores as values. As provided by repoleved in this post:
from nltk.sentiment.vader import SentimentIntensityAnalyzer
new_words = {
'foo': 2.0,
'bar': -3.4,
}
SIA = SentimentIntensityAnalyzer()
SIA.lexicon.update(new_words)
如果您想删除单词,请使用.pop"功能:
If you wish to remove words, use the '.pop' function:
SIA = SentimentIntensityAnalyzer()
SIA.lexicon.pop('no')
这篇关于是否可以编辑 NLTK 的维达情绪词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!