简体   繁体   English

Python中的VADER情感分析:从字典中删除单词

[英]VADER sentiment analysis in Python: remove words from dictionary

I am using the VADER sentiment analysis tool in Python's nltk library. 我正在使用Python的nltk库中的VADER情感分析工具。 I am wondering if anyone has a clue as to how to remove words from the lexicon without having to do so manually in the text file. 我想知道是否有人知道如何从词典中删除单词,而不必在文本文件中手动删除。 Using lexicon.update(new_words) allows one to add new words. 使用lexicon.update(new_words)可以添加新单词。 Can we use something similar to remove words as well? 我们也可以使用类似的方法来删除单词吗?

So, to add new words, we can use: 因此,要添加新单词,我们可以使用:

sia.lexicon.update(new_words)

I've tried to use lexicon.remove to delete words, but this does not work. 我尝试使用lexicon.remove删除单词,但这不起作用。 Any advice would be appreciated. 任何意见,将不胜感激。

I Think that lexicon in VADER are simply dictionaries. 我认为, lexiconVADER只是字典。
If that's the case, check the following code: 如果是这种情况,请检查以下代码:

# you can update your lexicon to add key:value pairs
example_dict = {"a":1, "b":2, "c":3, "d":4}
example_dict.update({"e":5})
print(example_dict) # OUTPUT: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# you can pop a key from the dictionary to remove the key:value pair
# .pop returns also the value
example_dict.pop("a")
print(example_dict) # OUTPUT: {'b': 2, 'c': 3, 'd': 4, 'e': 5}

# if you're sure that all the values in another dictionary are present in yours,
# you can map the pop function
dict2 = {"b":2, "d":6}
all(map( example_dict.pop, dict2))
print(example_dict) # {'c': 3, 'e': 5}

# you can also merge two dictionaries
dict3 = {"x":44, "y":42}
new_dict = {**example_dict, **dict3}
print(new_dict) # OUTPUT: {'c': 3, 'e': 5, 'x': 44, 'y': 42}

There are many operations on the dictionaries that you can perform. 您可以对字典执行许多操作。 If it is actually your case, just check the documentation 如果确实是您的情况,请检查文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM