Examples of online books are not entirely correct.
You can try the following:
from nltk.draw.dispersion import dispersion_plot
words = ['Elinor', 'Marianne', 'Edward', 'Willoughby']
dispersion_plot(gutenberg.words('austen-sense.txt'), words)
You can also call it directly from the text:
from nltk.book import text1
from nltk.draw.dispersion import dispersion_plot
dispersion_plot(text1, ['monstrous'])
this way you import the function directly, rather than calling funcion from a text object. I realized that I was looking directly at the source code.
Hope this work for you
source
share