LDA python 用Gensim包处理LDA主题模型

本文演示了如何使用Python中的Gensim库来处理LDA(Latent Dirichlet Allocation)主题模型。通过执行测试代码,展示了生成的主题分布,包括关键词如'dad', 'love', 'nature'等。" 116810498,7534221,C++中的不可变对象与不可变类,"['C++编程', '面向对象', '线程同步', '类设计']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
filepaths = []

#循环将文件夹中的TXT全部导入
import os
rootdir = 'F:/GEV/lda_dir/referenceData'
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
	path = os.path.join(rootdir,list[i])
	if os.path.isfile(path):
		filepaths.append(path)

docs = [open(f, 'r', encoding = 'utf-8').read() for f in filepaths]

docs = [word_tokenize(doc) for doc in docs]

stopWords = set(stopwords.words('english'))

docs = [[w  for w in doc if w.lower() not in stopWords] for doc in docs]


from gensim import corpora
from gensim.models import LdaModel
from gensim import models
from gensim.corpora import Dictionary

dictionary = corpora.Dictionary(docs)

corpus = [ dictionary.doc2bow(text) for text in docs ]
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]

#########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus_tfidf, id2word=dictionary, num_topics=2,passes=2
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值