词移距离(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. Laravel开发:Laravel初始化简述

    二话不说,先从public/index.php撸起 require __DIR__.'/../bootstrap/autoload.php';//Composer 自动加载 $app = requir ...

  2. 第一章 MATLAB数字图像处理编程基础

    1 为什么用MATLAB MATLAB的图像处理工具箱(Image Processing Toolbox,IPT)封装了一系列不同图像处理需求的标准算法,它们都是通过直接或间接调用MATLAB中矩阵运 ...

  3. Discrete Function(简单数学题)

    Discrete Function There is a discrete function. It is specified for integer arguments from 1 to N (2 ...

  4. vs05字节对齐问题又一不小心就弄去了我一个下午的时间

    由于一字节的对齐问题,我调一个库调了我基本一个下午..... 犯错其实并不可怕, 可怕的是你一犯再犯...... 这也算得上是难能可贵... /Zp (Struct Member Alignment) ...

  5. tmpfs(转)

    什么是tmpfs tmpfs是Linux/Unix系统上的一种基于内存的文件系统.tmpfs可以使用您的内存或swap分区来存储文件. 实现原理:基于VM子系统 tmpfs是基于Linux的虚拟内存管 ...

  6. 关于Future

    1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...

  7. android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗

    1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...

  8. JVM相关的几个基本概念

    1.虚拟机是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统 2.常见的虚拟机有VMWare,Visual Box,JVM等 VMWare或者Visual Box模拟的 ...

  9. 从springmvc启动日志学习

    javaee标准中,tomcat等web容器启动时走web.xml 先将各种contex-param 放到servletcontxt中变成parameter,然后开始启动容器,容器对外提供了liste ...

  10. LeetCode:删除排序链表中的重复元素【83】

    LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...