在python中,如何使用word2vec来计算句子的相似度呢?

第一种解决方法

如果使用word2vec,需要计算每个句子/文档中所有单词的平均向量,并使用向量之间的余弦相似度来计算句子相似度,代码示例如下:

import numpy as np
from scipy import spatialindex2word_set = set(model.index2word)def avg_feature_vector(sentence, model, num_features, index2word_set):words = sentence.split()feature_vec = np.zeros((num_features, ), dtype='float32')n_words = 0for word in words:if word in index2word_set:n_words += 1feature_vec = np.add(feature_vec, model[word])if (n_words > 0):feature_vec = np.divide(feature_vec, n_words)return feature_vec

计算相似度:

s1_afv = avg_feature_vector('this is a sentence', model=model, num_features=300, index2word_set=index2word_set)
s2_afv = avg_feature_vector('this is also sentence', model=model, num_features=300, index2word_set=index2word_set)
sim = 1 - spatial.distance.cosine(s1_afv, s2_afv)
print(sim)> 0.915479828613

第二种解决思路

Word2Vec有一些扩展用于比较较长的文本,可以解决短语或句子比较的问题。其中之一是paragraph2vec或doc2vec。

详见“分布式句子和文档表示”http://cs.stanford.edu/~quocle/paragraph_vector.pdf

http://rare-technologies.com/doc2vec-tutorial/

其他解决方法

要计算句子相似度,也可以使用Word Mover距离算法。这里是一个easy description about WMD。

#load word2vec model, here GoogleNews is used
model = gensim.models.KeyedVectors.load_word2vec_format('../GoogleNews-vectors-negative300.bin', binary=True)
#two sample sentences
s1 = 'the first sentence'
s2 = 'the second text'#calculate distance between two sentences using WMD algorithm
distance = model.wmdistance(s1, s2)print ('distance = %.3f' % distance)

P.s .:如果您遇到有关导入pyemd库的错误,可以使用以下命令进行安装:

pip install pyemd

另外,也可以使用sklearn cosine_similarity加载两个句子向量并计算相似度。

参考文献

  • How to calculate the sentence similarity using word2vec model of gensim with python

文章地址: https://vimsky.com/article/3677.html

【转载保存】在python中如何用word2vec来计算句子的相似度相关推荐

  1. Linux-C基础知识学习:C语言作业-将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印。

    Linux基础知识学习 C语言作业:将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印. #include <stdi ...

  2. Python中如何用random模块生成随机数并重现

    使用random模块生成随机数 Python广为人们喜爱的原因就是Python中有许许多多非常便捷的模块可供我们随意调用,在Python中我们可以通过调用random模块来生成一个伪随机数. --只有 ...

  3. python变量必须以字母和下划线_【转载】关于python中带下划线的变量和函数的意义...

    Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读, ...

  4. [转载] 玩转python中with的使用与上下文管理器

    参考链接: Python中的上下文管理器 人是随着时间不断进化而来的,同样编程语言也是随着IT行业的更新换代,功能模块不断地优化与丰富才壮大起来的.比如在python2.5之前使用open读写文件操作 ...

  5. [转载] 整理总结 python 中时间日期类数据处理与类型转换(含 pandas)

    参考链接: Python中的时间函数 2(日期操作) 我自学 python 编程并付诸实战,迄今三个月. pandas可能是我最高频使用的库,基于它的易学.实用,我也非常建议朋友们去尝试它.--尤其当 ...

  6. python中的pylab_【转载】有关python中的pylab的下载安装

    原博文地址:http://www.th7.cn/Program/Python/201501/352209.shtml 有关python中的pylab的下载安装,有需要的朋友可以参考下. 作为一个pyt ...

  7. python中ndarray除_python科学计算_numpy_ndarray

    ndarray:n-dimensional array object,即多维数组对象,是python自带的array对象的扩展,array对象和list对象的区别是array对象的每一个元素都是数值, ...

  8. 转载:简单介绍Python中的try和finally和with方法

    用 Python 做一件很平常的事情: 打开文件, 逐行读入, 最后关掉文件; 进一步的需求是, 这也许是程序中一个可选的功能, 如果有任何问题, 比如文件无法打开, 或是读取出错, 那么在函数内需要 ...

  9. [转载] issubclass在python中的意思_python issubclass 和 isinstance函数

    参考链接: Python issubclass() Python issubclass() 函数 issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类. ...

最新文章

  1. 【 MATLAB 】信号处理工具箱之波形产生函数 tripuls
  2. 怎样把centos 6.2 系统里的网卡em1还原为eth0
  3. miniui 加载文件时会做的一些事情
  4. ![CDATA[ ]]
  5. leetcode538 把二叉搜索树转换成累加树
  6. LeetCode 1134. 阿姆斯特朗数
  7. Maven入门详解与安装配置
  8. [文件系统]Image映象文件
  9. tu-ctf-2016:re-for-50-plz-50
  10. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)0-------通过应用程序域AppDomain加载和卸载程序集...
  11. 云计算平台构建与实验设计
  12. Javashop B2B2C 系统之社区团购商城
  13. A problem has been detected and windows has been shut down to prevent damage to your computer.
  14. STM32 CAN编程详解
  15. 5G时代的到来,对网络公关将产生哪些深远影响?
  16. requests中get请求没有referer得不到数据
  17. java设计五子棋_JAVA课程设计+五子棋(团队博客)
  18. 上拉加载更多其他方法
  19. 开发者工具绕过前端debuger
  20. 固化EOS智能合约,监管升级权限,净化EOS DAPP生态

热门文章

  1. error:java:无效的源发行版_IDEA java出现无效的源发行版14解决方案_java
  2. 电脑环境变量设置 java_如何设置自己电脑的系统环境变量?
  3. win7查看 本地计算机策略,win7系统本地组策略编辑器打不开怎么办
  4. python定位元素在列表中的位置_python定位列表元素
  5. 【LeetCode笔记(水)】s = null 与 s.length() == 0
  6. java jbutton 不显示_java – JButton中的图像未显示
  7. c语言stanf,stanf
  8. 联想gen系列服务器,Hpe Microserver Gen10 Plus开箱
  9. realtek网卡mac硬改工具_七彩虹联合Realtek发布粉色固态硬盘 首发评测
  10. 如何查看linux 是否安装软件包,linux 查看软件包是否安装 linux查看软件包