TypeError: object of type ‘module‘ has no len()

本文介绍了一个关于使用gensim库训练LDA主题模型时出现的TypeError异常,并提供了具体的解决办法,通过调整代码逻辑避免将模块对象传递给求长度的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误代码:

if self.id2word is None:
   logger.warning("no word id mapping provided; initializing from corpus, assuming identity")
   self.id2word = utils.dict_from_corpus(corpus)
   self.num_terms = len(self.id2word)
elif len(self.id2word) >0:
   self.num_terms = 1 + max(self.id2word.keys())
else:
   self.num_terms = 0

错误提示:

Traceback (most recent call last):
  File "/Users/dl/PycharmProjects/pythonProject/困惑度/困惑度+一致性检验.py", line 59, in <module>
    lda = models.LdaModel( corpus=corpus, id2word=dictionary, num_topics=10, passes=1)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gensim/models/ldamodel.py", line 438, in __init__
    elif len(self.id2word) >0:
TypeError: object of type 'module' has no len()

Process finished with exit code 1

正确代码:在"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gensim/models/ldamodel.py"文件中更改len为list.

if self.id2word is None:
   logger.warning("no word id mapping provided; initializing from corpus, assuming identity")
   self.id2word = utils.dict_from_corpus(corpus)
   self.num_terms = len(self.id2word)
elif list(self.id2word) >0:
   self.num_terms = 1 + max(self.id2word.keys())
else:
   self.num_terms = 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值