python实现simhash算法实例

时间:2023-11-02 23:26:30 

Simhash的算法简单的来说就是,从海量文本中快速搜索和已知simhash相差小于k位的simhash集合,这里每个文本都可以用一个simhash值来代表,一个simhash有64bit,相似的文本,64bit也相似,论文中k的经验值为3。该方法的缺点如优点一样明显,主要有两点,对于短文本,k值很敏感;另一个是由于算法是以空间换时间,系统内存吃不消。

python实现simhash算法实例


#!/usr/bin/python
# coding=utf-8
class simhash:

    #构造函数
    def __init__(self, tokens='', hashbits=128):       
        self.hashbits = hashbits
        self.hash = self.simhash(tokens);

    #toString函数   
    def __str__(self):
        return str(self.hash)

    #生成simhash值   
    def simhash(self, tokens):
        v = [0] * self.hashbits
        for t in [self._string_hash(x) for x in tokens]: #t为token的普通hash值          
            for i in range(self.hashbits):
                bitmask = 1 << i
                if t & bitmask :
                    v[i] += 1 #查看当前bit位是否为1,是的话将该位+1
                else:
                    v[i] -= 1 #否则的话,该位-1
        fingerprint = 0
        for i in range(self.hashbits):
            if v[i] >= 0:
                fingerprint += 1 << i
        return fingerprint #整个文档的fingerprint为最终各个位>=0的和

    #求海明距离
    def hamming_distance(self, other):
        x = (self.hash ^ other.hash) & ((1 << self.hashbits) - 1)
        tot = 0;
        while x :
            tot += 1
            x &= x - 1
        return tot

    #求相似度
    def similarity (self, other):
        a = float(self.hash)
        b = float(other.hash)
        if a > b : return b / a
        else: return a / b

    #针对source生成hash值   (一个可变长度版本的Python的内置散列)
    def _string_hash(self, source):       
        if source == "":
            return 0
        else:
            x = ord(source[0]) << 7
            m = 1000003
            mask = 2 ** self.hashbits - 1
            for c in source:
                x = ((x * m) ^ ord(c)) & mask
            x ^= len(source)
            if x == -1:
                x = -2
            return x
            

if __name__ == '__main__':
    s = 'This is a test string for testing'
    hash1 = simhash(s.split())

    s = 'This is a test string for testing also'
    hash2 = simhash(s.split())

    s = 'nai nai ge xiong cao'
    hash3 = simhash(s.split())

    print(hash1.hamming_distance(hash2) , "   " , hash1.similarity(hash2))
    print(hash1.hamming_distance(hash3) , "   " , hash1.similarity(hash3))


 

标签:python,simhash,算法
0
投稿

猜你喜欢

  • Microsoft VBScript 运行时错误 错误800a0005 无效的过程调用或参数

    2010-03-25 21:51:00
  • 从错误中学习改正Go语言五个坏习惯提高编程技巧

    2023-10-12 20:06:33
  • 在数据库‘master’中拒绝CREATE DATABASE权限问题的解决方法

    2011-10-24 19:46:55
  • python使用numpy实现直方图反向投影示例

    2022-09-18 11:28:55
  • python批量生成条形码的示例

    2023-02-22 17:49:03
  • python实现按任意键继续执行程序

    2021-02-12 12:47:10
  • python静态web服务器实现方法及代码详解

    2023-01-13 17:17:12
  • Python对列表的操作知识点详解

    2022-05-08 09:06:39
  • Pygame Event事件模块的详细示例

    2022-04-23 14:55:46
  • JavaScript caller与callee属性

    2009-01-19 13:39:00
  • PHP字符转义相关函数小结(php下的转义字符串)

    2023-11-16 14:29:18
  • Python实现12种降维算法的示例代码

    2023-03-17 14:04:21
  • python实现登录与注册系统

    2022-04-26 02:32:38
  • Python lambda表达式原理及用法解析

    2021-03-02 18:52:12
  • Python Tkinter Menu组件详解

    2021-07-24 06:51:18
  • 详解python中@classmethod和@staticmethod方法

    2022-10-24 11:47:16
  • Python在线和离线安装第三方库的方法

    2023-08-24 19:37:11
  • python新手练习实例之万年历

    2021-01-29 02:20:35
  • Tensorflow加载Vgg预训练模型操作

    2023-10-13 10:56:23
  • 在ASP应用程序中加入智能搜索

    2007-09-18 13:15:00
  • asp之家 网络编程 m.aspxhome.com