simhash的python实现

混江龙づ霸主 提交于 2019-12-06 13:53:28
import hashlib


def hash_str(s):
    md5 = hashlib.md5()
    md5.update(s)
    res = int(md5.hexdigest()[:16], base=16)
    return bin(res)[2:].zfill(64)


def simhash(words, weights):
    words = map(hash_str, words)

    def func(pair):
        word, weight = pair
        return map(lambda ch: weight if ch == '1' else -weight, word)

    nums = map(sum, zip(*map(func, zip(words, weights))))
    return ''.join(map(lambda num: '1' if num > 0 else '0', nums))


x = 'welcome to beijing'.split()
y = range(len(x))
print int(simhash(x, y), 2)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!