原文链接:http://www.one2know.cn/nlp10/

  • 主题识别

    是发现输入文本集合中存在的主题的过程

    LDA算法,即狄利克雷分布算法
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
from gensim import corpora,models
import feedparser class IdentifyingTopicExample:
def getDocuments(self): # 获取文档 放到documents中
url = 'https://sports.yahoo.com/mlb/rss.xml'
feed = feedparser.parse(url)
self.documents = []
for entry in feed['entries'][:5]:
text = entry['summary']
if 'ex' in text:
continue
self.documents.append(text)
print('-- {}'.format(text))
print('INFO: Fetching documents from {} completed'.format(url)) def cleanDocuments(self):
tokenizer = RegexpTokenizer(r'[a-zA-Z]+') # 想要只处理字母9
en_stop = set(stopwords.words('english')) # 英文停用词放到en_stop中
self.cleaned = [] # 用于存储所有被清洗且分词后的文档
for doc in self.documents:
lowercase_doc = doc.lower() # 字母都变小写
words = tokenizer.tokenize(lowercase_doc) # 分词
non_stopped_words = [i for i in words if not i in en_stop] # 过滤掉停用词
self.cleaned.append(non_stopped_words) # cleaned 二维列表
print('INFO: Clearning {} documents completed'.format(len(self.documents))) def doLDA(self):
dictionary = corpora.Dictionary(self.cleaned) # 创建字典
corpus = [dictionary.doc2bow(cleandoc) for cleandoc in self.cleaned]
# 由每个清洗后的句子,以词袋形式定义corpus变量
ldamodel = models.ldamodel.LdaModel(corpus,num_topics=2,id2word=dictionary)
# 在corpus上创建一个模型,主题数量设为2,id2word设置词典的大小/映射情况
print(ldamodel.print_topics(num_topics=2,num_words=4)) # 打印主题 每个主题含4个单词 def run(self):
self.getDocuments()
self.cleanDocuments()
self.doLDA() if __name__ == "__main__":
topicExample = IdentifyingTopicExample()
topicExample.run()

输出:

-- MLB Network documentary shines spotlight on 1995 Mariners team that saved baseball in Seattle.
-- Marcus Semien's second big swing of the day finally gave the Oakland Athletics some breathing room in an oh-so-tight series with the AL Central-leading Twins. Semien hit a grand slam in the eighth inning after his tying homer leading off the fifth, Chris Herrmann had a career-high four hits, and
-- It wasn't long until Cleveland took advantage of it. Francisco Lindor drove in the go-ahead runs during a six-run seventh inning, Jose Ramirez homered twice and Carlos Santana pushed his on-base streak to 27 games as the Indians rallied to beat bumbling Kansas City 8-4 on Thursday and complete a
-- A look at what's happening around the majors Friday:
INFO: Fetching documents from https://sports.yahoo.com/mlb/rss.xml completed
INFO: Clearning 4 documents completed
[(0, '0.022*"look" + 0.022*"friday" + 0.022*"around" + 0.022*"majors"'), (1, '0.023*"leading" + 0.023*"semien" + 0.022*"inning" + 0.014*"homer"')]

NLP(十) 主题识别的更多相关文章

  1. 【NLP】主题识别文档

    http://www.biostatistic.net/thread-94974-1-1.html http://www.doc88.com/p-9843685205530.html http://w ...

  2. NLP十大里程碑

    NLP十大里程碑 2.1 里程碑一:1985复杂特征集 复杂特征集(complex feature set)又叫做多重属性(multiple features)描写.语言学里,这种描写方法最早出现在语 ...

  3. 算法工程师进化-NLP之主题模型

    1 引言 主题模型是文本挖掘的重要工具,近年来在学术界和工业届都获得了非常多的关注.学术界的工作主要集中在建模层面,即提出各种各样的主题模型来适应不同的场景,因此缺乏指导主题模型在工业场景落地的资源和 ...

  4. 『深度应用』NLP命名实体识别(NER)开源实战教程

    近几年来,基于神经网络的深度学习方法在计算机视觉.语音识别等领域取得了巨大成功,另外在自然语言处理领域也取得了不少进展.在NLP的关键性基础任务—命名实体识别(Named Entity Recogni ...

  5. NLP︱LDA主题模型的应用难题、使用心得及从多元统计角度剖析

    将LDA跟多元统计分析结合起来看,那么LDA中的主题就像词主成分,其把主成分-样本之间的关系说清楚了.多元学的时候聚类分为Q型聚类.R型聚类以及主成分分析.R型聚类.主成分分析针对变量,Q型聚类针对样 ...

  6. 写给程序员的机器学习入门 (十) - 对象识别 Faster-RCNN - 识别人脸位置与是否戴口罩

    每次看到大数据人脸识别抓逃犯的新闻我都会感叹技术发展的太快了,国家治安水平也越来越好了

  7. 【NLP】Python实例:申报项目查重系统设计与实现

    Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...

  8. 【NLP】Python实例:基于文本相似度对申报项目进行查重设计

    Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...

  9. 自然语言处理(NLP)

    苹果语音助手Siri的工作流程: 听 懂 思考 组织语言 回答 这其中每一步骤涉及的流程为: 语音识别 自然语言处理 - 语义分析 逻辑分析 - 结合业务场景与上下文 自然语言处理 - 分析结果生成自 ...

随机推荐

  1. 使用secureCRT上传下载

    secureCRT 的 下载 http://pan.baidu.com/s/1c1Mz1ks 下载完成后,输入yum install lrzsz,安装这个东西, 然后就可以直接在secureCRT中输 ...

  2. Jibx 只绑定需要的字段

    栗子:     binding.xml   <?xml version="1.0" encoding="UTF-8"?> <binding&g ...

  3. http状态码 400-499

    类比 服务器:便利店 客户端:客人 http报文:中文语言+钱 400-499 客户的错误 400 :服务器不理解客服端请求的意思是什么,如请求报文损坏 举例: 客户端:@#!3&* 服务器: ...

  4. 【iOS】Assertion failure in -[MASViewConstraint install]

    刚遇到了这个问题,详细信息如下: Assertion failure 错误原因: 控件没有添加到视图就使用 mas_makeConstraints 了……应该先把控件添加到视图.

  5. LVS + Keepalived + Nginx基于DR模式构建高可用方案

    在大型网站中一般服务端会做集群,同时利用负载均衡器做负载均衡.这样有利于将大量的请求分散到各个服务器上,提升网站的响应速度.当然为了解决单点故障的问题,还会做热备份方案.这里演示利用LVS做负载均衡器 ...

  6. react开发中的小细节

    目前开始使用react余遇到的问题还不是很多,但还是希望总结一下. react中的属性prop: 在react中组件的父子组件的通信是基于prop的,当然对于底层的东西不是特别了解,但可以说一说它的基 ...

  7. python协程详解

    目录 python协程详解 一.什么是协程 二.了解协程的过程 1.yield工作原理 2.预激协程的装饰器 3.终止协程和异常处理 4.让协程返回值 5.yield from的使用 6.yield ...

  8. 【Java例题】3.6 计算arcsin(x)的值

    6.使用泰勒展开式计算arcsin(x)的值. arcsin(x)=x+x^3/(2*3)+1*3*x^5/(2*4*5)+...+ (2n)!*x^(2n+1)/(2^2n)*(n!)^2*(2n+ ...

  9. 3PHP如何用PDO的连接方式方式导出mysql数据

    首先连接mysql,具体看上一篇 接下来在try{}中加入以下代码 $query="select * from 你的数据表名称"          //$query的内容给个SQL ...

  10. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...