Reputation: 67
My code is below. Can anyone help me understand why I'm getting this error:
"TypeError: 'int' object is not iterable" regarding my "for i, crp" line?
from collections import defaultdict
import numpy
from gensim import corpora, models
dictionary = corpora.Dictionary(sws_removed)
corpus = [dictionary.doc2bow(text) for text in sws_removed]
document_topic = defaultdict(list)
for i, crp in enumerate(corpus):
probs = ldamodel.get_document_topics(crp)
max_topic = probs.argsort()[-1]
document_topic[max_topic].append(i)
Upvotes: 0
Views: 3182
Reputation: 589
I don't think the error is in the for statement, but in the line
corpus = [dictionary.doc2bow(text) for text in sws_removed]
Sws_removed must be a list of unicode characters. If it is not a list, but an int, it might give you that error. In fact, this code gives that error
from collections import defaultdict
import gensim
from gensim import corpora
lofs = 13
dictionary = corpora.Dictionary(lofs)
corpus = [dictionary.doc2bow(text) for text in lofs]
ldamodel = gensim.models.LdaModel(corpus=corpus)
document_topic = defaultdict(list)
for i, crp in enumerate(corpus):
probs = ldamodel.get_document_topics(crp)
And this is the stacktrace (which is always useful to post)
Traceback (most recent call last): File "D:/python/stackoverflow/numpcorpora/numcorpora.py", line 9, in dictionary = corpora.Dictionary(lofs) File "C:\Users\Admin\Anaconda3\lib\site-packages\gensim\corpora\dictionary.py", line 58, in init self.add_documents(documents, prune_at=prune_at) File "C:\Users\Admin\Anaconda3\lib\site-packages\gensim\corpora\dictionary.py", line 111, in add_documents for docno, document in enumerate(documents): TypeError: 'int' object is not iterable
Now replace lofs = 13
with lofs = ['list of words'.split()]
and it will run fine, giving the following output:
[[(0, 1), (1, 1), (2, 1)]]
[(50, 0.75250000000000183)]
So check your sws_removed. It is an int, and not a list of unicode characters, as it should be.
Upvotes: 1
Reputation: 7600
is type(corpus)
an int?
it needs to be an iterable: https://docs.python.org/3/library/functions.html#enumerate
Upvotes: 0