The following example creates an anagram dictionary.
However, it throws a TypeError: 'LazyCorpusLoader' object is not an iterator
:
import nltk
from nltk.corpus import words
anagrams = nltk.defaultdict(list)
for word in words:
key = ''.join(sorted(word))
anagrams[key].append(word)
print(anagrams['aeilnrt'])
You have to use the .words()
method on the words
corpus object.
Specifically: change
for word in words:
to
for word in words.words():
and it should work.
I got the same error for importing stopwords using nltk
Error occurred due to below import
from nltk.corpus import stopwords
Replacing the above with the following worked for me
# from nltk.corpus import stopwords
stopwords = nltk.corpus.stopwords.words('english')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With