首页 > 解决方案 > 使用潜在狄利克雷分配器捕获二元组主题而不是一元组

问题描述

我尝试尝试这样问题

LDA 原始输出

Uni-grams

    topic1 -scuba,water,vapor,diving

    topic2 -dioxide,plants,green,carbon

所需输出

Bi-gram topics

    topic1 -scuba diving,water vapor

    topic2 -green plants,carbon dioxide

有这个答案

from nltk.util import ngrams

for doc in docs:
    docs[doc] = docs[doc] + ["_".join(w) for w in ngrams(docs[doc], 2)]

为了只有二元组,我应该进行哪些更新?

标签: pythonnltkgensimn-gram

解决方案


仅使用二元组创建文档:

from nltk.util import ngrams

for doc in docs:
    docs[doc] = ["_".join(w) for w in ngrams(docs[doc], 2)]

或二元组的具体方法:

from nltk.util import bigrams

for doc in docs:
    docs[doc] = ["_".join(w) for w in bigrams(docs[doc])]

然后在texts未来的操作中使用这些二元组的列表。


推荐阅读