pycharm在用ldamallet计算一致性时,coherence_model_ldamallet = CoherenceModel(model=ldamallet, texts=texts_out, dictionary=id2word, coherence="c_v")
coherence_ldamallet = coherence_model_ldamallet.get_coherence();
报错:
OSError: [Errno 22] Invalid argument: 'c:\xxx\'!
使用的中文数据
ldamallet一致性报错
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
此错误可能是由于代码中给定的文件路径中包含非法字符,例如空格或特殊字符。解决方法是确保文件路径正确且不包含非法字符。可以使用os.path.realpath()函数来获取有效的文件路径。以下是示例代码: import os import gensim from gensim.models import CoherenceModel假设数据文件路径为 c:\xxx\data.txt
data_file_path = "c:/xxx/data.txt"
通过 os.path.realpath() 获取有效的文件路径
data_file_path = os.path.realpath(data_file_path)
加载中文文本
with open(data_file_path, 'r', encoding='utf-8') as f: texts = [[word.strip() for word in line.split()] for line in f.readlines()]
构建lda模型
id2word = gensim.corpora.Dictionary(texts) corpus = [id2word.doc2bow(document) for document in texts] ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=10, id2word=id2word)
计算一致性得分
coherence_model_ldamallet = CoherenceModel(model=ldamallet, texts=texts, dictionary=id2word, coherence="c_v") coherence_ldamallet = coherence_model_ldamallet.get_coherence() print("一致性得分:", coherence_ldamallet) 注意:上述示例代码中mallet_path需要替换为正确的mallet路径。
解决 无用评论 打赏 举报