nltk / nltk_book

NLTK Book
http://www.nltk.org/book
403 stars 143 forks source link

Ch. 1.3.3 ValueError: Too many values to unpack (expected 2)w/ collocations() function #224

Closed Selinalkan closed 4 years ago

Selinalkan commented 4 years ago

I've had no issues with running the exercises in the book in IDLE up until this point; however, I keep receiving this ValueError with the collocations() function. Does anyone have any solution to this? Thanks!


>>>text4.collocations()
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    text4.collocations()
  File "/Users/selinalkan/Library/Python/3.7/lib/python/site-packages/nltk/text.py", line 444, in collocations
    w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
  File "/Users/selinalkan/Library/Python/3.7/lib/python/site-packages/nltk/text.py", line 444, in <listcomp>
    w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
ValueError: too many values to unpack (expected 2)```
dshin8 commented 4 years ago

I am encountering the same issue. Could anyone provide some insight?

pjhinton commented 4 years ago

This is a known bug. See NLTK issue #2299. You can work around it by using collocation_list() and then using a string join(). You can eliminate enclosing quotes on output with a print().

print('; '.join(text4.collocation_list()))