simhash的python实现

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)

你可能感兴趣的:(python)