全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
1219 1
2024-07-23
悬赏 5 个论坛币 已解决
使用Simhash模块 计算出了两个文本的海明距离,但是不知道该如何使用simhash自带的函数进一步计算相似度。
如果进一步自定义函数,应该怎么计算呢?求各位相助

from simhash import Simhash
hash1 = Simhash(u'what is your real answer he asked the criminal when they firstly meet each other in ')
hash2 = Simhash(u'the criminal did not tell anybody his name and motivation when they' )
print('hash1',hash1)
print('hash2',hash2)
print('hash1.distance(hash2):',hash1.distance(hash2))

>--hash1 <simhash.Simhash object at 0x000002026372E310>>--hash2 <simhash.Simhash object at 0x000002026372E730>>
--hash1.distance(hash2): 28






最佳答案

赵安豆 查看完整内容

在`simhash`模块中,并没有直接提供计算相似度的函数。但是,根据海明距离(Hamming Distance)你可以自行设计一个函数来计算相似度。 通常情况下,两个Simhash值之间的海明距离越小,则表示它们所代表的文本片段越相似。我们可以基于这个原理定义一个简单的相似度计算方法:将最大可能的海明距离减去实际测得的海明距离后归一化到0~1区间内作为相似度。 考虑到Simhash使用64位哈希值,最大可能的距离为64(即所有比特位都不同) ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2024-7-23 22:05:45
在`simhash`模块中,并没有直接提供计算相似度的函数。但是,根据海明距离(Hamming Distance)你可以自行设计一个函数来计算相似度。

通常情况下,两个Simhash值之间的海明距离越小,则表示它们所代表的文本片段越相似。我们可以基于这个原理定义一个简单的相似度计算方法:将最大可能的海明距离减去实际测得的海明距离后归一化到0~1区间内作为相似度。

考虑到Simhash使用64位哈希值,最大可能的距离为64(即所有比特位都不同),可以按照以下公式计算相似度:

```
similarity = (max_distance - distance) / max_distance
```

因此对于上述代码中的示例,你可以这样计算相似度:

```python
from simhash import Simhash

# 原始文本
text1 = u'what is your real answer he asked the criminal when they firstly meet each other in '
text2 = u'the criminal did not tell anybody his name and motivation when they'

# 生成Simhash值
hash1 = Simhash(text1)
hash2 = Simhash(text2)

# 计算海明距离
distance = hash1.distance(hash2)

# 定义最大可能的海明距离(64位哈希)
max_distance = 64

# 根据公式计算相似度
similarity = (max_distance - distance) / max_distance

print('文本相似度:', similarity)
```

这样,你就可以得到两个文本片段之间的相似度了。

此文本由CAIE学术大模型生成,添加下方二维码,优先体验功能试用



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群