安装gensim 并且有pyemd,详情见下

 def wmdistance(self, document1, document2):"""Compute the Word Mover's Distance between two documents. When using thiscode, please consider citing the following papers:.. Ofir Pele and Michael Werman, "A linear time histogram metric for improved SIFT matching"... Ofir Pele and Michael Werman, "Fast and robust earth mover's distances"... Matt Kusner et al. "From Word Embeddings To Document Distances".Note that if one of the documents have no words that exist in theWord2Vec vocab, `float('inf')` (i.e. infinity) will be returned.This method only works if `pyemd` is installed (can be installed via pip, but requires a C compiler).Example:>>> # Train word2vec model.>>> model = Word2Vec(sentences)>>> # Some sentences to test.>>> sentence_obama = 'Obama speaks to the media in Illinois'.lower().split()>>> sentence_president = 'The president greets the press in Chicago'.lower().split()>>> # Remove their stopwords.>>> from nltk.corpus import stopwords>>> stopwords = nltk.corpus.stopwords.words('english')>>> sentence_obama = [w for w in sentence_obama if w not in stopwords]>>> sentence_president = [w for w in sentence_president if w not in stopwords]>>> # Compute WMD.>>> distance = model.wmdistance(sentence_obama, sentence_president)"""if not PYEMD_EXT:raise ImportError("Please install pyemd Python package to compute WMD.")# Remove out-of-vocabulary words.len_pre_oov1 = len(document1)len_pre_oov2 = len(document2)document1 = [token for token in document1 if token in self]document2 = [token for token in document2 if token in self]diff1 = len_pre_oov1 - len(document1)diff2 = len_pre_oov2 - len(document2)if diff1 > 0 or diff2 > 0:logger.info('Removed %d and %d OOV words from document 1 and 2 (respectively).',diff1, diff2)if len(document1) == 0 or len(document2) == 0:logger.info('At least one of the documents had no words that were''in the vocabulary. Aborting (returning inf).')return float('inf')dictionary = Dictionary(documents=[document1, document2])vocab_len = len(dictionary)if vocab_len == 1:# Both documents are composed by a single unique tokenreturn 0.0# Sets for faster look-up.docset1 = set(document1)docset2 = set(document2)# Compute distance matrix.distance_matrix = zeros((vocab_len, vocab_len), dtype=double)for i, t1 in dictionary.items():for j, t2 in dictionary.items():if not t1 in docset1 or not t2 in docset2:continue# Compute Euclidean distance between word vectors.distance_matrix[i, j] = sqrt(np_sum((self[t1] - self[t2])**2))if np_sum(distance_matrix) == 0.0:# `emd` gets stuck if the distance matrix contains only zeros.logger.info('The distance matrix is all zeros. Aborting (returning inf).')return float('inf')def nbow(document):d = zeros(vocab_len, dtype=double)nbow = dictionary.doc2bow(document)  # Word frequencies.doc_len = len(document)for idx, freq in nbow:d[idx] = freq / float(doc_len)  # Normalized word frequencies.return d# Compute nBOW representation of documents.d1 = nbow(document1)d2 = nbow(document2)# Compute WMD.return emd(d1, d2, distance_matrix)

Word2vec 计算两个文本之间相似度相关推荐

  1. python 两个word文档之间的相似度_如何用 word2vec 计算两个句子之间的相似度?

    现在是2018年7月,在这里总结这个问题下已有的答案,并补充一些2017年以来这方面研究的新进展. 从大类上分,计算句子相似度的方法可以分为两类: 1)无监督的方法,即不使用额外的标注数据,常用的方法 ...

  2. python 句子相似度 库_利用python语句的word2vec查找两个句子之间的相似度

    我想用word2vectors计算两个句子之间的相似度,我试图得到一个句子向量的向量,这样我就可以计算出一个句子向量的平均值来找到余弦相似度.我试过这个代码,但它不起作用.它给出的输出是带有一的句子向 ...

  3. ITK:计算两个图像之间的均方度量值

    ITK:计算两个图像之间的均方度量值 内容提要 输出结果 C++实现代码 内容提要 计算两个图像之间的均方根度量. 输出结果 来自2张图像的数据通过输出传递: [-10, -10]: 23101.7 ...

  4. 如何利用计算机计算天数,如何应用Win10系统电脑中的计算器计算两个日期之间的天数?...

    如何应用Win10系统电脑中的计算器计算两个日期之间的天数? 相信很多小伙伴都有在使用windows10系统的电脑,其中的计算器如何才能用来计算两个日期之间的天数呢?方法很简单,下面小编就来为大家介绍 ...

  5. C++/JAVA 计算两篇文章的相似度

    C++/JAVA 计算两篇文章的相似度 这位少侠,要不要进店瞧瞧? 实验介绍及思路 问题描述: 编写程序,计算任意两篇文章的相似度. 基本思路: 利用余弦相似度来计算其相似度. 完整代码 C++ 代码 ...

  6. 计算两组标签/关键词 相似度算法

    原文连接 http://www.zhaochao.net/index.php/2016/02/05/14/ 写作背景 标签在互联网行业有大量的应用,给博客打标签,给商品打标签,给新闻打标签.通常每篇文 ...

  7. Python计算向量夹角代码:如何用代码计算两个向量之间的夹角?

    Python计算向量夹角代码:如何用代码计算两个向量之间的夹角? 向量夹角是指两个向量之间的夹角,通常用cosine来表示.在机器学习和数据科学领域,计算向量夹角是一个非常基本的概念.Python是一 ...

  8. 计算两个日期之间的年数

    //问题2:计算两个日期之间的年数 #include <iostream.h> class Date { private :int year, month, day;public :Dat ...

  9. python计算两个向量之间的欧氏距离

    python计算两个向量之间的欧氏距离 代码: import numpy as np # 向量的值 feature_1 = np.array([1,2,3]) feature_2 = np.array ...

最新文章

  1. Ext JS 6正式版的GPL版本下载地址
  2. selenium 无法定位打开a链接_测试干货 :Selenium8种元素定位法
  3. MySQL集群节点宕机,数据库脑裂!如何排障?
  4. Sonar-project.properties配置
  5. 弹出taskeng.exe窗口的解决方法
  6. mysql与oracle链接超时_数据库连接超时问题(求解)
  7. clistctrl获取选中行数据_Power Query基础6:筛选、排序、删重复行
  8. Oracle数据库备份dmp文件,使用cmd命令导入导出步骤,以及忘记Oracle密码
  9. php容器原理,容器与依赖注入的原理
  10. Linux shell 脚本入门教程+实例
  11. Let’s Encrypt 免费ssl加密
  12. 继承学习第一天 共有派生
  13. 【优化算法】粒子群优化算法(PSO)【含Matlab源码 1073期】
  14. 【jmeter安装】jmeter下载安装超详细简单步骤
  15. 机敏问答[博弈][0] #20210628
  16. Burp Suite使用介绍
  17. autojs微信运动自动点赞
  18. 【云原生 | Kubernetes 实战】18、K8s 安全实战篇之 RBAC 认证授权(上)
  19. 基于django+html+flask的英语词汇量估算工具设计与实现 文档+项目源码
  20. 我对响应式编程中Mono和Flux的理解

热门文章

  1. 7-156 输出大写英文字母 (15 分)
  2. .net 和javaee
  3. CAD多行文本中文字的堆叠
  4. 大众疑问:学会PS 可以找什么工作
  5. SD 格式化錯誤提示Windows無法完成格式化
  6. 中国首条3D刷脸地铁开通,终于理解数加加众包为啥采集人脸了
  7. 古月 ROS移动机器人实战 二维slam地图构建 笔记
  8. redis持久化--AOF(九)
  9. 英国内政部(Home Office)间谍机构(spy powers)假装它是Ofcom咨询中的一名私人公民1514378282474...
  10. 微信公众号开发笔记(八)发送图片消息