使用潜在的dirichlet分配捕获二元图主题而不是单元图

2024-09-28 17:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个类似于this问题的尝试

原始输出

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)]

任何帮助我应该做什么更新才能只有Bigram


Tags: indocsfordocgreencarbonwaterngrams
1条回答
网友
1楼 · 发布于 2024-09-28 17:16:26

仅使用Bigram创建文档:

from nltk.util import ngrams

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

或bigrams的具体方法:

from nltk.util import bigrams

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

然后在texts中使用这些bigram的列表用于将来的操作

相关问题 更多 >