如果你有一个文本文件,那么以下这段代码可以帮助你实现LDA主题模型。
import jieba
# from nltk.corpus import stopwords
import pyLDAvis.gensim_models
import wordcloud
from gensim.models.coherencemodel import CoherenceModel
from gensim.models.ldamodel import LdaModel
from gensim.corpora.dictionary import Dictionary
text_h = ""
with open("temp.txt", "r", encoding="utf-8") as f:
for ann in f.readlines():
ann = ann.strip("\n") # 去除文本中的换行符
print(ann)
text_h += ann
# text_h = word_tokenize(text_h)
text_h = jieba.cut(text_h, cut_all=True)
"""
for i in range(len(text_h)):
text_h[i] = text_h[i].lower()
text_h = list(filter(lambda x: not str(x).isdigit(), text_h))
print(text_h)
"""
interpunctuations = [
",",
"。",
":",
";",
"?",
"(&#