【NLP学习笔记】(三)gensim使用之相似性查询(Similarity Queries)
相似性查询(Similarity Queries)
本文主要翻译自https://radimrehurek.com/gensim/tut3.html
在之前的教程语料和向量空间和主题和转换中,我们学会了如何在向量空间模型中表示语料和如何在不同的向量空间之间转换。实际工作中,这样做的一个最常见的目的是比较两个文档之间的相似性或比较某一个文档与其它文档的相似性(比如用户查询已经索引的文档中的某一个文档)
加载字典和语料
与上一章相同,首先加载第一章中保存的字典和语料。
from gensim import corpora, models, similarities import os if(os.path.exists('./gensim_out/deerwester.dict')): dictionary = corpora.Dictionary.load('./gensim_out/deerwester.dict') corpus = corpora.MmCorpus('./gensim_out/deerwester.mm') print("使用之前已经存储的字典和语料向量") else: print("请先通过第一章生成deerwester.dict和deerwester.mm")
第一步
定义模型LSI,并将语料corpus转换为索引
lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=2) index = similarities.MatrixSimilarity(lsi[corpus]) index.save('./gensim_out/deerwester.index') #保存训练后的index index = similarities.MatrixSimilarity.load('./gensim_out/deerwester.index')#从已保存的文件中加载index。
第二步
假设我们要查询新文本 'human computer interaction'。我们期望得出与新文本最相思的三个文本。
doc = 'human computer interaction' vec_bow = dictionary.doc2bow(doc.lower().split()) vec_lsi = lsi[vec_bow] print(vec_lsi)
第三步
比较新文本vec_lsi与语料库的相似性
sims = index[vec_lsi] print(list(enumerate(sims))) #打印结果(document_number, document_similarity) 2-tuples
上面结果为:
[(0, 0.99809301), (1, 0.93748635), (2, 0.99844527), (3, 0.9865886), (4, 0.90755945),(5, -0.12416792), (6, -0.1063926), (7, -0.098794639), (8, 0.05004178)]
(0, 0.99809301)的意思是第0篇文章与新文档的相似性为 0.99809301
将上面结果按相似性降序排列
sims = sorted(enumerate(sims), key = lambda item : -item[1]) print(sims)
结果:
[(2, 0.99844527), # The EPS user interface management system
(0, 0.99809301), # Human machine interface for lab abc computer applications
(3, 0.9865886), # System and human system engineering testing of EPS
(1, 0.93748635), # A survey of user opinion of computer system response time
(4, 0.90755945), # Relation of user perceived response time to error measurement
(8, 0.050041795), # Graph minors A survey
(7, -0.098794639), # Graph minors IV Widths of trees and well quasi ordering
(6, -0.1063926), # The intersection graph of paths in trees
(5, -0.12416792)] # The generation of random binary unordered trees
可以看出与文档“human computer interface”最相似的三篇文章分别是第2篇、第0篇、第三篇。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Python编辑器sublime text3环境配置
Python编辑器sublime text3环境配置 最近又在玩Python啦,以此记录学历的一些东西,也算是激励自己吧。 Python安装 Python的截止目前最新版本是3.6.1,这里直接就安装最新版本了。 安装过程如下:这里我去除了不想用的部分,各位看官可以根据自己的需求进行选择 下一步:这里一定要勾选上Add Python to environmentvariable,意思是把Python加入到系统的环境遍历里,这样我们就可以直接在编辑器中访问了。 最后,install等待完成即可。 编辑器sublime text3安装 sublime text3是比较流行的一款编辑器,好处这里就不说了。目前的最新版本是Build 3126,也下载最新版本进行安装。sublime text3的安装直接下一步即可,值得一提的是勾选上add to explorer context menu选项,这样我们就可以在右键菜单里直接打开文件了。 测试安装的成功与否环境 打开sublime text3,选择新建一个文件,并保存为uid.py,然后拷贝如下代码: import uuid for x in r...
- 下一篇
第7课:Java Spring Boot 2.0安全机制、漏洞与MVC身份验证实战
《阿里巴巴Java Spring Boot 2.0开发实战课程》07课本期分享专家:徐雷—阿里巴巴特邀Java讲师,MongoDB讲师 本期分享主题:Java Spring Boot2.0实战MyBatis与优化 (Java面试题)Java Spring Boot 2.0是最新的开发平台,深入介绍Spring Boot 2.0 安全特性与原理、机制,以及如何实现 MVC网站和API身份验证,对于常见的安全问题和处理策略,以及可行的安全方案,最后是经典Java面试题。。 视频地址:https://yq.aliyun.com/live/712 PPT地址:https://yq.aliyun.com/live/712 PS:阿里巴巴Java学习进阶大群” 1067人大群直播地址:Java技术进阶群进群方式:钉钉扫码入群
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,CentOS7官方镜像安装Oracle11G
- Red5直播服务器,属于Java语言的直播服务器
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境