词移距离(Word Mover's Distance)是在词向量的基础上发展而来的用来衡量文档相似性的度量。
 
词移距离的具体介绍参考http://blog.csdn.net/qrlhl/article/details/78512598  或网上的其他资料
 
此处,用词移距离来衡量唐诗诗句的相关性。为什么用唐诗?因为全唐诗的txt很容易获取,随便一搜就可以下载了。全唐诗txt链接:https://files.cnblogs.com/files/combfish/%E5%85%A8%E5%94%90%E8%AF%97.zip。
 
步骤:
1. 预处理语料集: 唐诗的断句分词,断句基于标点符号,分词依靠结巴分词
2. gensim训练词向量模型与wmd相似性模型
3. 查询
 
代码:
import jieba
from nltk import word_tokenize
from nltk.corpus import stopwords
from time import time
start_nb = time()
import logging print(20*'*','loading data',40*'*')
f=open('全唐诗.txt',encoding='utf-8')
lines=f.readlines()
corpus=[]
documents=[]
useless=[',','.','(',')','!','?','\'','\"',':','<','>',
',', '。', '(', ')', '!', '?', '’', '“',':','《','》','[',']','【','】']
for each in lines:
each=each.replace('\n','')
each.replace('-','')
each=each.strip()
each=each.replace(' ','')
if(len(each)>3):
if(each[0]!='卷'):
documents.append(each)
each=list(jieba.cut(each))
text=[w for w in each if not w in useless]
corpus.append(text) print(len(corpus)) print(20*'*','trainning models',40*'*')
from gensim.models import Word2Vec
model = Word2Vec(corpus, workers=3, size=100) # Initialize WmdSimilarity.
from gensim.similarities import WmdSimilarity
num_best = 10
instance = WmdSimilarity(corpus, model, num_best=10) print(20*'*','testing',40*'*')
while True:
sent = input('输入查询语句: ')
sent_w = list(jieba.cut(sent))
query = [w for w in sent_w if not w in useless] sims = instance[query] # A query is simply a "look-up" in the similarity class. # Print the query and the retrieved documents, together with their similarities.
print('Query:')
print(sent)
for i in range(num_best):
print
print('sim = %.4f' % sims[i][1])
print(documents[sims[i][0]])

  

结果:从结果kan
 
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 
 
 
 

唐诗掠影:基于词移距离(Word Mover's Distance)的唐诗诗句匹配实践的更多相关文章

  1. Distributed Sentence Similarity Base on Word Mover's Distance

    Algorithm: Refrence from one ICML15 paper: Word Mover's Distance. 1. First use Google's word2vec too ...

  2. 文本情感分析(一):基于词袋模型(VSM、LSA、n-gram)的文本表示

    现在自然语言处理用深度学习做的比较多,我还没试过用传统的监督学习方法做分类器,比如SVM.Xgboost.随机森林,来训练模型.因此,用Kaggle上经典的电影评论情感分析题,来学习如何用传统机器学习 ...

  3. 【CV知识学习】【转】beyond Bags of features for rec scenen categories。基于词袋模型改进的自然场景识别方法

    原博文地址:http://www.cnblogs.com/nobadfish/articles/5244637.html 原论文名叫Byeond bags of features:Spatial Py ...

  4. Earth Mover's Distance (EMD)

    原文: http://d.hatena.ne.jp/aidiary/20120804/1344058475作者: sylvan5翻译: Myautsai和他的朋友们(Google Translate. ...

  5. [转]Earth Mover's Distance (EMD)

    转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 Earth Mover's Distance (EMD)原文: htt ...

  6. 基于ABP落地领域驱动设计-05.实体创建和更新最佳实践

    目录 系列文章 数据传输对象 输入DTO最佳实践 不要在输入DTO中定义不使用的属性 不要重用输入DTO 输入DTO中验证逻辑 输出DTO最佳实践 对象映射 学习帮助 系列文章 基于ABP落地领域驱动 ...

  7. The Earth Mover's Distance

    The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...

  8. [Swift]LeetCode748. 最短完整词 | Shortest Completing Word

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  9. [python] 基于词云的关键词提取:wordcloud的使用、源码分析、中文词云生成和代码重写

    1. 词云简介 词云,又称文字云.标签云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思.常见于博客.微博 ...

随机推荐

  1. Unity3D研究院之在开始学习拓展编辑器

    Unity拥有非常丰富的拓展编辑器接口,如果是在网上下载过别人写的插件,你会发现为什么它的监测面板视图和普通的不一样?其实是他通过代码自己绘制的监测面板,这篇博文MOMO带大家来学习编辑器.如下图所示 ...

  2. ios 10 sticker pack application

    看了WWDC2016直播,我们发现变得谨慎而开放的苹果在新一版四大平台系统中展示了很多变化,当然重中之重还是伟大的iOS.通过试用iOS10beta版,除了长大了的更强大的Siri主要感受到iMess ...

  3. ubuntu16.04下hive安装与配置

    Hive是什么? 由Facebook开源用于解决海量 结构化日志的数据统计: Hive是基于Hadoop的一个 数据仓库工具,可以将结构化的数据文件映射 成一张表,并提供类SQL查询功能: 构建在Ha ...

  4. java servlet中上传文件的简单实现(基于第三方jar)

    这里的案例使用了两种文件上传的组件.分别介绍 1.使用JSPSmartUpload完成上传 package test_servlet_package; import java.io.File; imp ...

  5. hdu 2108 Shape of HDU【判断多边形是否是凸多边形模板】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2108 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. Python中为什么要使用线程池?如何使用线程池?

    系统处理任务时,需要为每个请求创建和销毁对象.当有大量并发任务需要处理时,再使用传统的多线程就会造成大量的资源创建销毁导致服务器效率的下降.这时候,线程池就派上用场了.线程池技术为线程创建.销毁的开销 ...

  7. bash: /home/aprilpeng/.bashrc: Permission denied

    bash: /home/aprilpeng/.bashrc: Permission denied 出现这样的权限问题,一般是在切换用户的时候,用户没有该目录的权限 可以$chown -R git:us ...

  8. python3的时间日期处理

    1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时 ...

  9. Struts2-Value Stack浅析

    http://my.oschina.net/mlongbo/blog/88250 Value Stack的作用: 1.       可以作为一个数据中转站 2.       用于在前台-后台之间传递数 ...

  10. Docker的前世今生

    核心知识点: 1.Docker的构想:对应用的封装.分发.部署.运行的生命周期的管理,一次封装到处运行 2.Docker的优点:一站式解决方案 3.Docker由LXC演变而来,迟迟没有集成到Linu ...