2

I am trying to add these new words and their corresponding polarity scores from a CSV file into Vader Sentiment Lexicon

New words csv here

It also reflects in the vadersentiment object when it is updated:

enter image description here

But as soon as I try to get the polarity scores for the newly added words, it throws an error:

enter image description here

I am confused as to what is happening even though that the word is present in the Vader dictionary:

enter image description here

Does anyone know why is it happening?

Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
Geeta bari
  • 21
  • 2

1 Answers1

1

One workaround is to use pandas for reading CSV and leveraging to_dict() to convert the dataframe to Dictionary directly.

I have placed the sample data provided in the question inside vader.csv

CSV:

enter image description here

This is what the dictionary looks like after reading from CSV using pandas and converting to the dictionary using to_dict.

DICTIONARY:

enter image description here

SOLUTION CODE:

import pandas as pd 
data = pd.read_csv('vader.csv', header=None, index_col=0, squeeze=True, skiprows=1).to_dict()
sa_obj = SentimentIntensityAnalyzer()
sa_obj.lexicon.update(data)
print(sa_obj.lexicon['buffering'])

OUTPUT:

enter image description here

Notes:

  1. skiprows=1 is used to skip the headers while converting the dataframe to dictionary
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56