以下是我的代码:
import jieba
import os
import warnings
warnings.filterwarnings('ignore')
from gensim.corpora.dictionary import Dictionary
from gensim.models.ldamodel import LdaModel
from wordcloud import WordCloud
import matplotlib.pyplot as plt
textfile=input("输入文本文件名:")
num_topics=int(input("话题数:"))
f=open(textfile, "r",encoding="utf-8")
lines=f.readlines()
f.close()
stoplist=open(r'C:\Users\26552\Desktop\stopword.txt','r',encoding="utf-8").read()
stoplist = set(w.strip() for w in stoplist)
segtexts=[]
for line in lines:
doc=[]
for w in list(jieba.cut(line,cut_all=True)):
if len(w)>1 and w not in stoplist:
doc.append(w)
segtexts.append(doc)
dictionary = Dictionary(segtexts)
dictionary.filter_extremes(2,1.0,keep_n=1000)
corpus = [dictionary.doc2bow(text) for text in segtexts]
lda = LdaModel(corpus,id2word=dictionary, num_topics=num_topics)
topics=lda.print_topics(num_topics=num_topics,num_words=10)
print(topics)
font = r'C:\Users\26552\Desktop\simfang.ttf'
wc=WordCloud(collocations=False, font_path=font, width=2800, height=2800, max_words=20,margin=2)
for topicid in range(0,num_topics):
tlist=lda.get_topic_terms(topicid, topn=20)
wdict={}
for wv in tlist:
wdict[ dictionary[wv[0]]]=wv[1]
print(wdict)
wordcloud = wc.generate_from_frequencies(wdict)
wordcloud.to_file('topic_'+str(topicid)+'.png')
这是停用词文本文档:
路径没有问题,请问老哥们,是我的代码哪里出问题了吗?
如能指正,不胜感激!