余弦相似性计算及python代码实现
A:西米喜欢健身
B:超超不爱健身,喜欢打游戏
step1:分词
A:西米/喜欢/健身
B:超超/不/喜欢/健身,喜欢/打/游戏
step2:列出两个句子的并集
西米/喜欢/健身/超超/不/打/游戏
step3:计算词频向量
A:[1,1,1,0,0,0,0]
B:[0,1,1,1,1,1,1]
step4:计算余弦值

余弦值越大,证明夹角越小,两个向量越相似。
step5:python代码实现
import jieba
import jieba.analyse def words2vec(words1=None, words2=None):
v1 = []
v2 = []
tag1 = jieba.analyse.extract_tags(words1, withWeight=True)
tag2 = jieba.analyse.extract_tags(words2, withWeight=True)
tag_dict1 = {i[0]: i[1] for i in tag1}
tag_dict2 = {i[0]: i[1] for i in tag2}
merged_tag = set(tag_dict1.keys()) | set(tag_dict2.keys())
for i in merged_tag:
if i in tag_dict1:
v1.append(tag_dict1[i])
else:
v1.append(0)
if i in tag_dict2:
v2.append(tag_dict2[i])
else:
v2.append(0)
return v1, v2 def cosine_similarity(vector1, vector2):
dot_product = 0.0
normA = 0.0
normB = 0.0
for a, b in zip(vector1, vector2):
dot_product += a * b
normA += a ** 2
normB += b ** 2
if normA == 0.0 or normB == 0.0:
return 0
else:
return round(dot_product / ((normA**0.5)*(normB**0.5)) * 100, 2) def cosine(str1, str2):
vec1, vec2 = words2vec(str1, str2)
return cosine_similarity(vec1, vec2) print(cosine('阿克苏苹果', '阿克苏苹果'))
余弦相似性计算及python代码实现的更多相关文章
- [转] MachingLearning中的距离相似性计算以及python实现
参考:https://blog.csdn.net/gamer_gyt/article/details/75165842#t16 https://blog.csdn.net/ymlgrss/artic ...
- python之验证码识别 特征向量提取和余弦相似性比较
0.目录 1.参考2.没事画个流程图3.完整代码4.改进方向 1.参考 https://en.wikipedia.org/wiki/Cosine_similarity https://zh.wikip ...
- 文本相似性计算总结(余弦定理,simhash)及代码
最近在工作中要处理好多文本文档,要求找出和每个文档的相识的文档.通过查找资料总结如下几个计算方法: 1.余弦相似性 我举一个例子来说明,什么是"余弦相似性". 为了简单起见,我们先 ...
- 10行Python代码计算汽车数量
当你还是个孩子坐车旅行的时候,你玩过数经过的汽车的数目的游戏吗? 在这篇文章中,我将教你如何使用10行Python代码构建自己的汽车计数程序. 以下是环境及相应的版本库: Python版本 3.6.9 ...
- 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...
- python学习之——计算给出代码中注释、代码、空行的行数
题目:计算给出代码中注释.代码.空行的行数 来源:网络 思路:注释行以 ‘#’开头,空行以 ‘\n’ 开头,以此作为判断 def count_linenum(fname): fobj = open(f ...
- 计算Python代码运行时间长度方法
在代码中有时要计算某部分代码运行时间,便于分析. import time start = time.clock() run_function() end = time.clock() print st ...
- Solr In Action 笔记(2) 之 评分机制(相似性计算)
Solr In Action 笔记(2) 之评分机制(相似性计算) 1 简述 我们对搜索引擎进行查询时候,很少会有人进行翻页操作.这就要求我们对索引的内容提取具有高度的匹配性,这就搜索引擎文档的相似性 ...
- 如何在batch脚本中嵌入python代码
老板叫我帮他测一个命令在windows下消耗的时间,因为没有装windows那个啥工具包,没有timeit那个命令,于是想自己写一个,原理很简单: REM timeit.bat echo %TIME% ...
随机推荐
- Python:笔记(4)——高级特性
Python:笔记(4)——高级特性 切片 取一个list或tuple的部分元素是非常常见的操作.Python提供了切片操作符,来完成部分元素的选取 除了上例简单的下标范围取元素外,Python还支持 ...
- maven依赖排除、顺序原则、版本统一管理
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core& ...
- 华为交换机S5700系列配置通过STelnet登录设备示例
配置通过STelnet登录设备示例 组网图形 图1 配置用户通过STelnet登录设备组网图 在服务器端生成本地密钥对 <HUAWEI> system-view [HUAWEI] sysn ...
- Spring积累
<tx:annotation-driven/> (Spring的XML配置里两大Bean的声明之一) 那我们是否就可以在程序中所有被spring管理的类(@Controller.@Ser ...
- jvm-java内存模型与锁优化
java内存模型与锁优化 参考: https://blog.csdn.net/xiaoxiaoyusheng2012/article/details/53143355 https://blog.csd ...
- 配置阿里云maven中央库
<mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> ...
- java中Vector类的常用方法
Vector类是实现List接口,所以继承的方法就不在这里讲了 https://www.cnblogs.com/xiaostudy/p/9503199.html public void add(int ...
- Remove Duplicates from Sorted List ,除去链表中相邻的重复元素
Remove Duplicates from Sorted List : Given a sorted linked list, delete all duplicates such that eac ...
- Longest Substring Without Repeating Characters,求没有重复字符的最长字串
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- 分享:JAVA各种对象
PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...