安装第三方包:gensim

首先,执行去停词操作(去除与主题无关的词)

 #-*-coding:utf8-*-

 import jieba

 def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r').readlines()]
return stopwords def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
stopwords = stopwordslist('stopWords/stopwords.txt')
outstr = ''
for word in sentence_seged:
word = word.lower()
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
return outstr inputs = open('input/copurs.txt', 'r') outputs = open('input/copurs_out.txt', 'w')
for line in inputs:
line_seg = seg_sentence(line)
outputs.write(line_seg + '\n')
outputs.close()
inputs.close()

然后,执行主题分类操作

 import codecs
from gensim import corpora
from gensim.models import LdaModel
from gensim import models
from gensim.corpora import Dictionary te = []
fp = codecs.open('input/copurs_out.txt','r')
for line in fp:
line = line.split()
te.append([ w for w in line ])
print len(te)
dictionary = corpora.Dictionary(te)
corpus = [ dictionary.doc2bow(text) for text in te ] #tfidf = models.TfidfModel(corpus)
#corpus_tfidf = tfidf[corpus] #########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus]] ####### write the topics in file topics_result.txt ####################
topics_r = lda.print_topics(num_topics = 50, num_words = 10)
topic_name = codecs.open('output/topics_result.txt','w')
for v in topics_r:
topic_name.write(str(v)+'\n') ###################### write the class results to file #########################
###################### each document belongs to which topic ###################### fp2 = codecs.open('output/documents_result.txt','w')
for t in doc_topic:
c = []
c.append([a[1] for a in t])
m = max(c[0]) for i in range(0, len(t)):
if m in t[i]:
#print(t[i])
fp2.write(str(t[i][0]) + ' ' + str(t[i][1]) + '\n')
break
################################ OVER ############################################

注意:上述主题分类,仅使用lda模型(根据频数计算)

也可混合使用tf-idf模型XX-topic下代码改为如下即可:

方式一
#########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus_tfidf, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus_tfidf]]

方式二
#########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus_tfidf]]

常用方式为方式一,作者暂时为弄清楚这两种方式的区别,后期将会继续完善

python应用:主题分类(gensim lda)的更多相关文章

  1. 主题模型(LDA)(一)--通俗理解与简单应用

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_39422642/article/de ...

  2. 文本主题模型之LDA(一) LDA基础

    文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法(TODO) 在前面我们讲到了基于矩阵分解的 ...

  3. 文本主题模型之LDA(二) LDA求解之Gibbs采样算法

    文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法(TODO) 本文是LDA主题模型的第二篇, ...

  4. 文本主题模型之LDA(三) LDA求解之变分推断EM算法

    文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法 本文是LDA主题模型的第三篇,读这一篇之前 ...

  5. python的数据结构分类,以及数字的处理函数,类型判断

    python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...

  6. Gensim LDA主题模型实验

    本文利用gensim进行LDA主题模型实验,第一部分是基于前文的wiki语料,第二部分是基于Sogou新闻语料. 1. 基于wiki语料的LDA实验 上一文得到了wiki纯文本已分词语料 wiki.z ...

  7. gensim LDA模型提取每篇文档所属主题(概率最大主题所在)

    gensim的LDA算法中很容易提取到每篇文章的主题分布矩阵,但是一般地还需要进一步获取每篇文章归属到哪个主题概率最大的数据,这个在检索gensim文档和网络有关文章后,发现竟然没有. 简单写了一下. ...

  8. LDA模型应用实践-希拉里邮件主题分类

    #coding=utf8 import numpy as np import pandas as pd import re from gensim import corpora, models, si ...

  9. 转:Python 文本挖掘:使用gensim进行文本相似度计算

    Python使用gensim进行文本相似度计算 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101895642665/ 在文本处理 ...

随机推荐

  1. postman接口案例

    接口测试 什么是接口(API) API全称Application Programming Interface,这里面我们其实不用去关注AP,只需要I上就可以.一个API就是一个Interface.我们 ...

  2. SpringBoot热部署插件

    1.配置在 maven工程中的pom.xml文件中 2.SpringBoot框架中提供的一个热部署插件,利用该热部署插件,我们可以在修改代码后不用重启应用,大大提高开发效率:

  3. C#中将dll汇入exe z

    用了3层架构,运行目录下有很多dll文件,最终发布时,我打算将dll文件都合并到exe文件中去.微软发布的免费软件ILmerge可以完成这项工作,研究了一下,其用法如下: 1.合并file1.dll. ...

  4. WAKE-WIN10-SOFT-GITHUB

    1,GITHUB 官网:https://github.com/ 2,软件工具 ,,,,,,

  5. [原] Android自动打包之命令行打包

    Android自动打包流程详细图: 总结为以下几个步骤: 1. 生成R文件 2. Java代码编译成class文件 3. class文件生成dex文件 4. 打包资源 5. 生成apk 6. 创建密匙 ...

  6. Ajax系列之三:UpdatePanel

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhanghongjie0302/article/details/35609691           ...

  7. expected expression __bridge

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013020103/article/details/30491117 expected expres ...

  8. CentOS 7.1上安装.Net Core

    官方网站给出了几条命令: sudo yum install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/f ...

  9. 【JeeSite】角色和权限的修改

    @RequiresPermissions("sys:role:edit") @RequestMapping(value = "save") public Str ...

  10. 【luogu T24743 [愚人节题目5]永世隔绝的理想乡】 题解

    题意翻译 我们来说说王的故事吧. 星之内海,瞭望之台.从乐园的角落告知汝等.汝等的故事充满了祝福.只有无罪之人可以进入——『永世隔绝的理想乡(Garden of Avalon)』! 题目背景 zcy入 ...