我有以下代码,在推文上运行LDA分析:
import logging, gensim, bz2
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
# load id->word mapping (the dictionary), one of the results of step 2 above
id2word = 'enams4nieuw.dict'
# load corpus iterator
mm = gensim.corpora.MmCorpus('enams4nieuw.mm')
print(mm)
# extract 100 LDA topics, using 1 pass and updating once every 1 chunk (10,000 documents)
lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=100, update_every=1, chunksize=10000, passes=1)
当我尝试运行此脚本时,收到以下日志并显示错误消息:
MmCorpus(40152 documents, 13061 features, 384671 non-zero entries)
2015-03-31 16:52:50,246 : INFO : loaded corpus index from enams4nieuw.mm.index
2015-03-31 16:52:50,246 : INFO : initializing corpus reader from enams4nieuw.mm
2015-03-31 16:52:50,246 : INFO : accepted corpus with 40152 documents, 13061 features, 384671 non-zero entries
Traceback (most recent call last):
File "C:/Users/gerbuiker/PycharmProjects/twitter-streaming.py/lda.py", line 15, in <module>
lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=100, update_every=1, chunksize=10000, passes=1)
File "C:\Users\gerbuiker\AppData\Roaming\Python\Python27\site-packages\gensim\models\ldamodel.py", line 244, in __init__
self.num_terms = 1 + max(self.id2word.keys())
AttributeError: 'str' object has no attribute 'keys'
Process finished with exit code 1
任何人都有解决方案吗?
答案 0 :(得分:1)
将变量id2word设置为字符串。
看来你有一个文件名 - 我假设你腌制了你的字典?
id2word必须是字典。
答案 1 :(得分:0)
我有同样的错误,似乎ldamodel.py试图获取关键字的最大值而不是索引/ ID&#39;所以我的解决方案只是交换字典中的列。
my_dict2 = {y:x for x,y in my_dict.items()}