NLTK: Text.collocations() returns ValueError

Book cover from amazon.co.uk

I was trying the collocations examples from Chapter 1, section 3.3 Collocations and Bigrams, of the book NLP with Python and I got the following ValueError


The solution came from this NLTK’s issue on their GitHub :
  1. Open .../nltk/text.py  with your favorite editor
    NOTE: in my case the file was in ~/miniconda3/envs/nlp_py37/lib/python3.7/site-packages/nltk/text.py
  2. Change lines 440 and 441 from .../nltk/text.py   with the following line:
    print(tokenwrap(self.collocation_list(num, window_size), separator="; "))
    

Now the collocations examples are working:
text4.collocations(num=21)
text8.collocations()
Output:
NOTE: you can play with the argument num which refers to the maximum number of collocations to print.

Comments

Popular posts from this blog

Deactivate conda's base environment on startup

Draw arrows with GIMP plugins

Product review: SMONET wireless security camera system