簡體   English   中英

Python中的VADER情感分析:從字典中刪除單詞

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

我正在使用Python的nltk庫中的VADER情感分析工具。 我想知道是否有人知道如何從詞典中刪除單詞,而不必在文本文件中手動刪除。 使用lexicon.update(new_words)可以添加新單詞。 我們也可以使用類似的方法來刪除單詞嗎?

因此,要添加新單詞,我們可以使用:

sia.lexicon.update(new_words)

我嘗試使用lexicon.remove刪除單詞,但這不起作用。 任何意見,將不勝感激。

我認為, lexiconVADER只是字典。
如果是這種情況,請檢查以下代碼:

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

您可以對字典執行許多操作。 如果確實是您的情況,請檢查文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM