理论部分:NLP 笔记:Latent Dirichlet Allocation (介绍篇)-CSDN博客
参考内容:DengYangyong/LDA_gensim: 用gensim训练LDA模型,进行新闻文本主题分析 (github.com)
1 导入库
import jieba,os,re
from gensim import corpora, models, similarities
2 创建停用词列表
stopwords = [line.strip() for line in open('./stopwords.txt',encoding='UTF-8').readlines()]
stopwords
3 对句子进行中文分词(的函数)
def seg_depart(sentence):sentence_depart = jieba.cut(sentence.strip())#使用jieba进行中文分词stopwords = stopwordslist()outstr = ''for word in sentence_depart:if word not in stopwords:#如果不在停用词里面,则将分的词写入输入字符串中outstr += wordoutstr += " " return outstr
4 对文档进行分词
原文档(cnews.train.txt):
"""如果文档还没分词,就进行分词"""
if not os.path.exists('./cnews.train_jieba.txt'):# 给出文档路径filename = "./cnews.train.txt"outfilename = "./cnews.train_jieba.txt"inputs = open(filename, 'r', encoding='UTF-8')outputs = open(outfilename, 'w', encoding='UTF-8')for line in inputs:line = line.split('\t')[1]'''使用制表符分割行,并取第二部分第一部分是新闻的主题'''line = re.sub(r'[^\u4e00-\u9fa5]+','',line)#使用正则表达式删除所有非中文字符,只保留中文line_seg = seg_depart(line.strip())#对剩下的中文行进行分词outputs.write(line_seg.strip() + '\n')#写入文档中outputs.close()inputs.close()
output file是:
5 准备训练语料库
"""准备好训练语料,整理成gensim需要的输入格式"""
fr = open('./cnews.train_jieba.txt', 'r',encoding='utf-8')
train = []
for line in fr.readlines():line = [word.strip() for word in line.split(' ')]train.append(line)
train
dictionary = corpora.Dictionary(train)
'''
使用 train 数据来创建一个 Dictionary 对象这个词典是一个从单词到单词ID的映射,每个单词都会被赋予一个唯一的ID
'''corpus = [dictionary.doc2bow(text) for text in train]
'''
遍历 train 数据集中的每个文档使用 doc2bow 方法将每条新闻转换为词袋模型(Bag-of-Words)每个元素是新闻中的每个词语,在字典中的ID和频率'''
corpus
6 创建LDA
lda = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10)
'''
创建了一个 LdaModel 对象
使用前面生成的语料库 corpus 和词典 dictionary 进行训练
num_topics=10 表示要从数据中提取的主题数量
'''
7 获取topic list
topic_list = lda.print_topics(10)
topic_list
'''
[(0,'0.073*"基金" + 0.015*"公司" + 0.013*"投资" + 0.011*"市场" + 0.010*"中" + 0.009*"股票" + 0.006*"行业" + 0.006*"经理" + 0.006*"经济" + 0.006*"中国"'),(1,'0.008*"基金" + 0.007*"数码相机" + 0.006*"市场" + 0.006*"中" + 0.005*"产品" + 0.005*"元" + 0.004*"万" + 0.004*"信息" + 0.004*"账户" + 0.004*"性能"'),(2,'0.005*"活动" + 0.005*"设计" + 0.005*"中" + 0.004*"拍摄" + 0.003*"市场" + 0.003*"中国" + 0.003*"公司" + 0.003*"数码" + 0.003*"商家" + 0.003*"视频"'),(3,'0.016*"分红" + 0.015*"机身" + 0.012*"考试" + 0.006*"市场" + 0.006*"中" + 0.006*"英寸" + 0.004*"元" + 0.004*"采用" + 0.004*"公司" + 0.004*"基金"'),(4,'0.004*"中" + 0.003*"搭配" + 0.003*"设计" + 0.002*"比赛" + 0.002*"小巧" + 0.002*"时尚" + 0.002*"元" + 0.002*"房地产" + 0.002*"黑色" + 0.002*"市场"'),(5,'0.056*"基金" + 0.012*"赎回" + 0.007*"分红" + 0.005*"市场" + 0.005*"中" + 0.005*"元" + 0.004*"收益" + 0.004*"影像" + 0.004*"投资者" + 0.004*"公司"'),(6,'0.007*"中" + 0.006*"拍摄" + 0.004*"功能" + 0.004*"中国" + 0.004*"支持" + 0.003*"能力" + 0.003*"照片" + 0.003*"发展" + 0.003*"快门" + 0.003*"四级"'),(7,'0.005*"搭配" + 0.004*"中" + 0.004*"纽曼" + 0.004*"时尚" + 0.003*"穿" + 0.003*"中国" + 0.002*"市场" + 0.002*"性感" + 0.002*"黑色" + 0.002*"拍摄"'),(8,'0.011*"功能" + 0.009*"中" + 0.008*"采用" + 0.008*"玩家" + 0.008*"拍摄" + 0.007*"相机" + 0.006*"万" + 0.006*"支持" + 0.005*"镜头" + 0.005*"新"'),(9,'0.007*"说" + 0.006*"英语" + 0.006*"中" + 0.006*"时间" + 0.005*"做" + 0.004*"四级" + 0.003*"句子" + 0.003*"设计" + 0.002*"题" + 0.002*"信息"')]
'''
8 每个新闻的主题分布和主要主题
for document in corpus:#print(document)doc_topics = lda.get_document_topics(document)print(doc_topics)most_probable_topic = max(doc_topics, key=lambda x: x[1])print("Most Probable Topic: Topic ID:", most_probable_topic[0], "with probability", most_probable_topic[1])
'''
[(4, 0.9862377)]
Most Probable Topic: Topic ID: 4 with probability 0.9862377
[(2, 0.17439426), (3, 0.11908359), (4, 0.1178159), (6, 0.243781), (9, 0.342713)]
Most Probable Topic: Topic ID: 9 with probability 0.342713
[(3, 0.043999113), (4, 0.80878687), (6, 0.023567822), (9, 0.12172426)]
Most Probable Topic: Topic ID: 4 with probability 0.80878687
[(1, 0.40913466), (7, 0.33063287), (8, 0.25087485)]
Most Probable Topic: Topic ID: 1 with probability 0.40913466
[(3, 0.5576278), (7, 0.06341313), (9, 0.3749383)]
'''