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

标签 python nlp nltk gensim lda

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

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

大约有 3000 多个标题,我想在 Python 中使用 LDA 为每个标题生成主题。假设我已经使用 nltk 包清理和标记了这些文本并删除了停用词,我最终会得到:

texts = [["places","eat"],["places","visit"]]...

然后我继续将这些文本转换为词袋:

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)

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

最佳答案

您可以使用 gensim LDA 检索文档主题列表

Ldamodel.show_topics()

然后对新文档进行分类

Ldamodel.get_document_topics(doc)

其中 doc 是文档词袋向量。

https://radimrehurek.com/gensim/models/ldamodel.html

关于python - 如何使用 LDA (Python) 从标题列表生成主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606331/

相关文章:

python - 如果对象是私有(private)的,则排除对象

python - 使用自定义函数后如何存储新的数据框?

nlp - 判断邮件是否为垃圾邮件的有效方法?

python - 在 Tensorflow 2.0/Keras 中使用其他数据功能创建文本分类器

algorithm - 识别一个人的名字与字典中的单词

python - NLTK精度: "ValueError: too many values to unpack"

python - 在 Python 3 中已存在 NLTK 时在 Python 2.7 中安装它

python - sklearn 中的流水线问题

python - python 中有没有不用字典就可以计算音节的方法?

python - 使用 Python 自动创建/操作 Excel 电子表格