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

时间:2018-06-21 20:16:58

标签: python sentiment-analysis vader

我正在使用Python的nltk库中的VADER情感分析工具。我想知道是否有人知道如何从词典中删除单词,而不必在文本文件中手动删除。使用lexicon.update(new_words)可以添加新单词。我们也可以使用类似的方法来删除单词吗?

因此,要添加新单词,我们可以使用:

sia.lexicon.update(new_words)

我尝试使用lexicon.remove删除单词,但这不起作用。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为lexicon中的VADER只是字典。
如果是这样,请检查以下代码:

# 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}

您可以对字典执行许多操作。如果确实是您的情况,只需检查documentation