如何使用LDA(Python)从标题列表中生成主题?

2024-05-16 22:28:15 发布

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

我不熟悉自然语言处理。 我有一个博客标题列表,例如(不是真实数据,但你明白了):

docs = ["Places to Eat", "Places to Visit", "Top 10 Things to Do in Singapore"]...

大约有3000多个标题,我想用Python中的LDA为每个标题生成主题。假设我已经使用nltk包清理并标记了这些文本,并删除了停止字,那么我将得到:

^{pr2}$

然后我继续把这些文字转换成一袋字:

from gensim import corpora, models
dictionary = corpora.Dictionary(texts)

corpus = [dictionary.doc2bow(text) for text in texts]

语料库数据如下:

[(0, 1), (1, 1)]...

模型创建:

import gensim
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=30, id2word = dictionary, passes=20)

我如何利用这个模型为每个标题生成一个主题列表-例如“吃”、“访问”等?我知道输出可能包含概率,但我只想将它们与文本串在一起。在


Tags: to数据in文本import标题主题列表