Python3与simhash实现文本去重

以下代码是计算两个文本相似的值, 值越大越相似

import math

import jieba.analyse
from simhash import Simhash


def gen_post_simhash(body, topK=10, withWeight=True):
    tags = jieba.analyse.extract_tags(body, topK=topK, withWeight=withWeight)
    sim = Simhash(tags, f=56)
    return sim.value


# 比较不同的个数
def distance_post_simhash(simhash_value1, simhash_value2, f=56):
    x = (simhash_value1 ^ simhash_value2) & ((1 << f) - 1)
    ans = 0
    while x:
        ans += 1
        x &= x - 1
    return ans


def dot_product2(v1, v2):
    return sum(map(lambda x, y: x * y, v1, v2))


def vector_cos5(v1, v2):
    prod = dot_product2(v1, v2)
    len1 = math.sqrt(dot_product2(v1, v1))
    len2 = math.sqrt(dot_product2(v2, v2))
    return prod / (len1 * len2)


def similar_ratio_to_two_texts(body1, body2):
    """
    余弦相似度
    http://stackoverflow.com/questions/18424228/cosine-similarity-between-2-number-lists
    """
    topK = 10
    tag1s = jieba.analyse.extract_tags(body1, topK=topK, withWeight=True)
    tag2s = jieba.analyse.extract_tags(body2, topK=topK, withWeight=True)
    tag1s_dict = dict(tag1s)
    tag2s_dict = dict(tag2s)
    all_tags = set([t[0] for t in tag2s] + [t[0] for t in tag1s])
    # numpy
    # vec1 = np.array([tag1s_dict.get(tag, 0) for tag in all_tags])
    # vec2 = np.array([tag2s_dict.get(tag, 0) for tag in all_tags])
    # similar = np.sum(vec1*vec2)/(np.sqrt(np.sum(np.square(vec1))) * np.sqrt(np.sum(np.square(vec2))))
    vec1 = [tag1s_dict.get(tag, 0) for tag in all_tags]
    vec2 = [tag2s_dict.get(tag, 0) for tag in all_tags]
    similar = vector_cos5(vec1, vec2)
    return similar
复制代码

转载于:https://juejin.im/post/5d023e18f265da1b614ff2f7

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值