学习中一起进步 2024-03-20 10:08 采纳率: 0%
浏览 6

ldamallet一致性报错

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\'!
使用的中文数据

  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-20 10:08
    关注
    让阿豪来帮你解答,本回答参考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路径。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月20日