我正在对报纸文章进行一些主题建模,并使用 Python3 中的 gensim 实现了 LDA。现在我想为每个主题创建一个词云,使用每个主题的前 20 个单词。我知道我可以打印单词,并保存 LDA 模型,但是有没有办法只保存每个主题的顶部单词,以便我可以进一步使用它来生成单词云?
我尝试用谷歌搜索,但找不到任何相关内容。任何帮助表示赞赏。
您可以使用 Gensim 的内置方法 show_topic 从 LDA 模型中获取 topn 单词。
lda = models.LdaModel.load('lda.model')
for i in range(0, lda.num_topics):
with open('output_file.txt', 'w') as outfile:
outfile.write('{}\n'.format('Topic #' + str(i + 1) + ': '))
for word, prob in lda.show_topic(i, topn=20):
outfile.write('{}\n'.format(word.encode('utf-8')))
outfile.write('\n')
这将写入一个格式类似于以下内容的文件:
Topic #69:
pet
dental
tooth
adopt
animal
puppy
rescue
dentist
adoption
animal
shelter
pet
dentistry
vet
paw
pup
patient
mix
foster
owner
Topic #70:
periscope
disneyland
disney
snapchat
brandon
britney
periscope
periscope
replay
britneyspear
buffaloexchange
britneyspear
https
meerkat
blab
periscope
kxci
toni
disneyland
location
您可能需要也可能不需要根据您的需要进行调整,即生成前 20 个单词的列表,而不是将其输出到文本文件。
这篇文章中的答案很好地解释了如何使用原始文本创建词云。如何打印lda主题模型和每个主题的词云
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)