user2238328
user2238328

Reputation: 259

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.

Upvotes: 0

Views: 893

Answers (1)

Gsk
Gsk

Reputation: 2945

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

Upvotes: 1

Related Questions