NLP(十) 主题识别
原文链接: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(十) 主题识别的更多相关文章
- 【NLP】主题识别文档
http://www.biostatistic.net/thread-94974-1-1.html http://www.doc88.com/p-9843685205530.html http://w ...
- NLP十大里程碑
NLP十大里程碑 2.1 里程碑一:1985复杂特征集 复杂特征集(complex feature set)又叫做多重属性(multiple features)描写.语言学里,这种描写方法最早出现在语 ...
- 算法工程师进化-NLP之主题模型
1 引言 主题模型是文本挖掘的重要工具,近年来在学术界和工业届都获得了非常多的关注.学术界的工作主要集中在建模层面,即提出各种各样的主题模型来适应不同的场景,因此缺乏指导主题模型在工业场景落地的资源和 ...
- 『深度应用』NLP命名实体识别(NER)开源实战教程
近几年来,基于神经网络的深度学习方法在计算机视觉.语音识别等领域取得了巨大成功,另外在自然语言处理领域也取得了不少进展.在NLP的关键性基础任务—命名实体识别(Named Entity Recogni ...
- NLP︱LDA主题模型的应用难题、使用心得及从多元统计角度剖析
将LDA跟多元统计分析结合起来看,那么LDA中的主题就像词主成分,其把主成分-样本之间的关系说清楚了.多元学的时候聚类分为Q型聚类.R型聚类以及主成分分析.R型聚类.主成分分析针对变量,Q型聚类针对样 ...
- 写给程序员的机器学习入门 (十) - 对象识别 Faster-RCNN - 识别人脸位置与是否戴口罩
每次看到大数据人脸识别抓逃犯的新闻我都会感叹技术发展的太快了,国家治安水平也越来越好了
- 【NLP】Python实例:申报项目查重系统设计与实现
Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...
- 【NLP】Python实例:基于文本相似度对申报项目进行查重设计
Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...
- 自然语言处理(NLP)
苹果语音助手Siri的工作流程: 听 懂 思考 组织语言 回答 这其中每一步骤涉及的流程为: 语音识别 自然语言处理 - 语义分析 逻辑分析 - 结合业务场景与上下文 自然语言处理 - 分析结果生成自 ...
随机推荐
- div span img对齐,垂直居中对齐问题
我想你们在前端开发中或多或少都遇到过这种问题,文字和图片不能平齐,很是头疼. HTML代码: <div class="">小太阳<span>小太阳</ ...
- 使用log4net记录ABP日志
demo地址:ABP.WindowsService 该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. 参考:https://aspnet ...
- UE4中UMG与C++交互 页面文本修改
在UE4中,有两种方式创建ui,一种是使用slate的方式,一种是UMG,UMG是slate的封装,是一个可视化的ui编辑器.slate则是纯c++方式(之前实验过一次slate创建页面,代码相当麻烦 ...
- iOS基础面试题汇总
目录 1. #import 跟#include.@class有什么区别?#import<> 跟 #import""又什么区别? 都可以完整包含某个文件的内容,但是#im ...
- kylin Retrieving hive dependency...
由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...
- 把Jar包加入windows系统服务
之前在服务器上不一个Java服务时候,总是开着一堆黑框框,非常不雅,重点是极其容易误关,所以把可执行Jar文件加入Windows系统服务,看起来是个非常不错的选择!(实际上也确实是非常不错的选择) ! ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- CentOS7.x 搭建 GitLab 教程
今天闲来无事,想起之前买了一个阿里云 ECS,一直闲置着没用,一时兴起就想搭个自己的 GitLab 玩玩,GitLab 官网也提供了安装教程,很简单,照着步骤一步步基本没什么问题,可能安装的过程中有一 ...
- java NIO知多少
背景 Linux系统中的IO操作内部相当复杂,下面是一张带图片的LinuxIO相关层级关系: 下面是一个简化版本Linux内部IO层级图: 对此我的理解,java程序员版本的IO理解: java中的I ...
- cmd中,查询sqlcmd命令的选项
像我这样的小白,有时候看到-d,-S,-P这些都不知道什么意思,后面知道了是一些命令的选项.如sqlcmd,打开cmd,输入sqlcmd -? 即可获得选项的含义. .