简体   繁体   中英

VADER sentiment analysis in Python: remove words from dictionary

I am using the VADER sentiment analysis tool in Python's nltk library. 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. 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. Any advice would be appreciated.

I Think that lexicon in VADER are simply dictionaries.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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