Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'LazyCorpusLoader' object is not iterable

Tags:

nltk

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'])
like image 382
Vera W Avatar asked Dec 24 '22 01:12

Vera W


2 Answers

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.

like image 192
lenz Avatar answered Feb 20 '23 03:02

lenz


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')
like image 23
Niroshan Ratnayake Avatar answered Feb 20 '23 03:02

Niroshan Ratnayake