唐诗掠影:基于词移距离(Word Mover's Distance)的唐诗诗句匹配实践
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('sim = %.4f' % sims[i][1])
print(documents[sims[i][0]])







<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
唐诗掠影:基于词移距离(Word Mover's Distance)的唐诗诗句匹配实践的更多相关文章
- 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 ...
- 文本情感分析(一):基于词袋模型(VSM、LSA、n-gram)的文本表示
现在自然语言处理用深度学习做的比较多,我还没试过用传统的监督学习方法做分类器,比如SVM.Xgboost.随机森林,来训练模型.因此,用Kaggle上经典的电影评论情感分析题,来学习如何用传统机器学习 ...
- 【CV知识学习】【转】beyond Bags of features for rec scenen categories。基于词袋模型改进的自然场景识别方法
原博文地址:http://www.cnblogs.com/nobadfish/articles/5244637.html 原论文名叫Byeond bags of features:Spatial Py ...
- Earth Mover's Distance (EMD)
原文: http://d.hatena.ne.jp/aidiary/20120804/1344058475作者: sylvan5翻译: Myautsai和他的朋友们(Google Translate. ...
- [转]Earth Mover's Distance (EMD)
转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 Earth Mover's Distance (EMD)原文: htt ...
- 基于ABP落地领域驱动设计-05.实体创建和更新最佳实践
目录 系列文章 数据传输对象 输入DTO最佳实践 不要在输入DTO中定义不使用的属性 不要重用输入DTO 输入DTO中验证逻辑 输出DTO最佳实践 对象映射 学习帮助 系列文章 基于ABP落地领域驱动 ...
- The Earth Mover's Distance
The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...
- [Swift]LeetCode748. 最短完整词 | Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- [python] 基于词云的关键词提取:wordcloud的使用、源码分析、中文词云生成和代码重写
1. 词云简介 词云,又称文字云.标签云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思.常见于博客.微博 ...
随机推荐
- oracle10g安装问题
oracle10g的安装还是比较容易的,一直下一步就行了,但是今天安装的时候遇到了一个新问题,在安装的过程中提示提示一些 Configuration Assistant失败刚开始,我直接跳过去,但后面 ...
- 批处理--复制,解压文件,goto,nul
rem 复制文件 copy "D:\xxxx" "C:\xxxx" rem 复制文件夹 xcopy "D:\xxxx" "C:\x ...
- iOS 7 修改默认布局从status bar 底部开始
最近在对公司的一个老项目进行版本升级,添加了导航栏和tabBar,并且在个人中心界面隐藏navigationBar,于是在控制器里添加了如下对象方法: - (void)viewWillAppear:( ...
- 杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse' Trade》《今年暑假不AC》《排名》《开门人和关门人》
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2Fpc2luaV92Yw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 洛谷P2296 寻找道路==codevs3731 寻找道路
P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- javascript修改图片链接地址
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- nginx日志自动切分
#!/bin/bash NGINX_LOG_PATH=/data/nginx-/logs # 昨天 YESTERDAY=$(date -d "yesterday" +%Y-%m-% ...
- python基础-第九篇-9.2线程与多线程
单线程 import time beginTime = time.time() for a in range(10): print(a) time.sleep(1) shijian = time.ti ...
- oracle 查询重复数据并且删除, 只保留一条数据重复数据
最近面试中都遇到了这样一个数据库题: 删除表中的重复数据,有且只保留一条重复数据. 思路: 1)这个题需要用到rowid,首先找到重复数据的rowid,并找出rowid最大或最小值,作为删除的条件: ...
- 分布式文件存储——GlusterFS
一.概论 1.简介 GlusterFS (Gluster File System) 是一个开源的分布式文件系统,主要由 Z RESEARCH 公司负责开发. GlusterFS 是 Scale-Out ...