cjhutto / vaderSentiment

VADER Sentiment Analysis. VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media, and works well on texts from other domains.
MIT License
4.43k stars 1k forks source link

list index out of range error persists... #146

Open iriumxx1 opened 1 year ago

iriumxx1 commented 1 year ago

Hi I'm having the same problem as in the issue https://github.com/cjhutto/vaderSentiment/issues/91#issuecomment-1550187600, and I'm using the last version:

Cell In[41], line 3 in deteccion_sentimiento_vader probas_vader = analyzer_vader.polarity_scores(texto)

File ~\anaconda3\lib\site-packages\vaderSentiment\vaderSentiment.py:273 in polarity_scores sentiments = self.sentiment_valence(valence, sentitext, item, i, sentiments)

File ~\anaconda3\lib\site-packages\vaderSentiment\vaderSentiment.py:290 in sentiment_valence if item_lowercase == "no" and words_and_emoticons[i + 1].lower() in self.lexicon:

IndexError: list index out of range

This error appears with a sentence that's ends with "no"

gdet commented 1 year ago

Hello! Did you find any solution?

Siddharth-Latthe-07 commented 3 months ago

The above issue might appears to be related to the way it processes the word "no" when it is at the end of a sentence. Specifically, the code tries to access the next word after "no", which does not exist, resulting in anIndexError. Try out these changes and tell if it works or not?

  1. Preprocess the Text:- Ensure that the input text does not end with "no". You can add a space or another character to the end of the text to avoid this issue
    
    def preprocess_text(text):
    if text.strip().endswith("no"):
        text += " "
    return text

texto = preprocess_text(texto) probas_vader = analyzer_vader.polarity_scores(texto)

2. Try upgrading the vader sentiment library
OR the last case you can do is that, modify the library code to handle the above edge case:-
This involves adding a check to ensure that the index does not go out of range.
Find the `vaderSentiment.py` file in your `site-packages` directory and locate the `sentiment_valence` function. Modify the function to include a check for the length of `words_and_emoticons`:

def sentiment_valence(self, valence, sentitext, item, i, sentiments): item_lowercase = item.lower() if item_lowercase == "no" and i + 1 < len(sentitext.words_and_emoticons) and sentitext.words_and_emoticons[i + 1].lower() in self.lexicon:

existing logic here



plz let me know if it works?