Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed tuple unpacking error when adding new entries to lexicon.txt #30

Closed
wants to merge 1 commit into from

Conversation

f0lie
Copy link

@f0lie f0lie commented Jun 16, 2017

When adding new entries to vader_lexicon.txt, Unix text editors will add a newline to the end of files to comply with standards. The problem is that when nltk reads the entire file then vader splits the lines, the code reads an empty entry. When python attempts to unpack the line into a tuple, it crashes.

The resulting error looks something like the text I attached below.

I fixed the issue by striping a newline within the body of SentimentIntensityAnalzyer.make_lex_dict(). Nltk gives a single line of text when it reads the file, so rstrip just removes the very last newline.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-d54d1cf92423> in <module>()
      2 # NOTE VADER FAILS WITH LONGER MESSAGES ESPECIALLY MULTIPLE SENTENCES
      3 from nltk.sentiment.vader import SentimentIntensityAnalyzer
----> 4 vader = SentimentIntensityAnalyzer('./vader_lexicon.txt')
      5 scored = feedback['FEEDBACK_TX'].apply(vader.polarity_scores)
      6 

~/feedback-sentiment/.env/lib/python3.6/site-packages/nltk/sentiment/vader.py in __init__(self, lexicon_file)
    203     def __init__(self, lexicon_file="sentiment/vader_lexicon.zip/vader_lexicon/vader_lexicon.txt"):
    204         self.lexicon_file = nltk.data.load(lexicon_file)
--> 205         self.lexicon = self.make_lex_dict()
    206 
    207     def make_lex_dict(self):

~/feedback-sentiment/.env/lib/python3.6/site-packages/nltk/sentiment/vader.py in make_lex_dict(self)
    211         lex_dict = {}
    212         for line in self.lexicon_file.split('\n'):
--> 213             (word, measure) = line.strip('\n').split('\t')[0:2]
    214             lex_dict[word] = float(measure)
    215         return lex_dict

ValueError: not enough values to unpack (expected 2, got 1)

@@ -204,7 +204,7 @@ def make_lex_dict(self):
Convert lexicon file to a dictionary
"""
lex_dict = {}
for line in self.lexicon_full_filepath.split('\n'):
for line in self.lexicon_full_filepath.rstrip('\n').split('\n'):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line that actually changed. The rest is my editor removing extra whitespaces. Sorry about that.

@dariencupit
Copy link

dariencupit commented Jul 22, 2019

I think I have ran into this exact same error when attempting to use pyinstaller on our program that uses vaderSentiment. I am puzzled as to why this pull request is still up, even though it is 2 years old.

@cjhutto
Copy link
Owner

cjhutto commented Aug 6, 2019

Thanks so much for finding this, and making a change for the better. I've incorporated the fix :)

@cjhutto cjhutto closed this Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants