相似度算法——SimHash算法(附带:python和java实现)

SimHash算法



概述

  • SimHash算法来自于 GoogleMoses Charikar发表的一篇论文“detecting near-duplicates for web crawling” ,其主要思想是降维, 将高维的特征向量映射成低维的特征向量,通过两个向量的Hamming Distance(汉明距离)来确定文章是否重复或者高度近似。

  • Hamming Distance: 又称汉明距离,在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的不同字符的个数。也就是说,它就是将一个字符串变换成另外一个字符串所需要替换的字符个数。


一、实现思路

  1. 分词:对需要比较的文本进行分词,提取特征向量。并对特征向量,进行权重(weight)设置。
  2. hash: 通过hash函数计算各个特征向量的hash值。hash值为二进制数0 1 组成的n-bit签名。
  3. 加权:在hash值的基础上,给所有特征向量进行加权,即W = Hash * weight,且遇到1则hash值和权值正相乘,遇到0则hash值和权值负相乘。
  4. 合并:将上述各个特征向量的加权结果累加,变成只有一个序列串。拿前两个特征向量举例。
  5. 降维:对于n-bit签名的累加结果,如果大于0则置1,否则置0,从而得到该语句的simhash值,最后我们便可以根据不同语句simhash的海 明距离来判断它们的相似度。
  6. 计算:通过Simhash签名值,计算汉明距离。

二、实现流程图

在这里插入图片描述

三、代码实现

python版——jieba分词

import jieba
import jieba.analyse
import numpy as np


class Simhash(object):
    def simhash(self, content):
        keylist = []
        # jieba分词
        seg = jieba.cut(content)
        # 去除停用词永祥
        # jieba.analyse.set_stop_words("stopwords.txt")
        # 得到前20个分词和tf-idf权值
        keywords = jieba.analyse.extract_tags("|".join(seg), topK=20, withWeight=True, allowPOS=())
        print(keywords)
        for feature, weight in keywords:
            print(weight)
            weight = int(weight * 20)
            print(weight)
            print("k="+feature)
            feature = self.string_hash(feature)
            print("v="+feature)
            temp = []
            for i in feature:
                if i == "1":
                    temp.append(weight)
                else:
                    temp.append(-1 * weight)
            keylist.append(temp)


        list1 = np.sum(np.array(keylist), axis=0)
        if keylist == []:
            return "00"
        simhash = ""
        # 降维处理
        for i in list1:
            if i > 0:
                simhash += "1"
            else:
                simhash += "0"
        return simhash

    def string_hash(self, source):
        if source == "":
            return 0
        else:
            x = ord(source[0]) << 7
            m = 1000003
            mask = 2 ** 128 - 1
            for c in source:
                x = ((x * m) ^ ord(c)) & mask
            x ^= len(source)
            if x == -1:
                x = -2
            x = bin(x).replace('0b', '').zfill(64)[-64:]
        return str(x)

def hammingDis(s1,s2):
    t1 = "0b" + s1
    t2 = "0b" + s2
    n = int(t1,2) ^ int(t2,2)
    i = 0
    print(bin(n))
    while n:
        n &= (n-1)
        i += 1
    print(i)
    max_hashbit = max(len(bin(int(t1,2))), len(bin(int(t2,2))))
    sim = i/max_hashbit
    print(sim)

if __name__ == "__main__":
    text1 = "天气非常好"
    text2 = "天气真不错"
    s1 = Simhash();
    t1_hash = s1.simhash(text1)
    t2_hash = s1.simhash(text2)
    print(t1_hash)
    print(t2_hash)
    hammingDis(t1_hash,t2_hash)

Java 版——ansj分词

import org.ansj.domain.Term;
import org.ansj.recognition.impl.StopRecognition;
import org.ansj.splitWord.analysis.ToAnalysis;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySimHash {
    /**
     * 字符串
     */
    private String tokens;
    /**
     * 字符产的hash值
     */
    private BigInteger strSimHash;
    /**
     * 分词后的hash数
     */
    private int hashbits = 64;

    private StopRecognition filter;


    public MySimHash(String tokens) {
        this.tokens = tokens;
        this.strSimHash = this.simHash();
    }

    private MySimHash(String tokens, int hashbits) {
        this.tokens = tokens;
        this.hashbits = hashbits;
        this.strSimHash = this.simHash();

    }

    /**
     * 这个是对整个字符串进行hash计算
     * @return
     */
    private BigInteger simHash() {
        //初始化分词器
        StopRecognition filter = new StopRecognition();
        //过滤停词
        List<String> stopWordList = new ArrayList<>();
        stopWordList.add(",");
        filter.insertStopWords(stopWordList);

        int[] v = new int[this.hashbits];
        //对字符串进行分词
        List<Term> terms = ToAnalysis.parse(this.tokens).recognition(filter).getTerms();

        //设定超频词汇的界限 ;
        int overCount = 5;
        Map<String, Integer> wordCount = new HashMap<String, Integer>();
        for (Term term : terms) {
            //分词字符串
            String word = term.getName();
            //  过滤超频词
            if (wordCount.containsKey(word)) {
                int count = wordCount.get(word);
                if (count > overCount) {
                    continue;
                }
                wordCount.put(word, count + 1);
            } else {
                wordCount.put(word, 1);
            }

            // 计算hash
            BigInteger t = this.hash(word);
            for (int i = 0; i < this.hashbits; i++) {
                BigInteger bitmask = new BigInteger("1").shiftLeft(i);

                //添加权重(统一设置20,后续可以自行根据词的不同设置权重)
                int weight = 20;
                if (t.and(bitmask).signum() != 0) {
                    // 这里是计算整个文档的所有特征的向量和
                    v[i] += weight;
                } else {
                    v[i] -= weight;
                }
            }
        }
        BigInteger fingerprint = new BigInteger("0");
        for (int i = 0; i < this.hashbits; i++) {
            if (v[i] >= 0) {
                fingerprint = fingerprint.add(new BigInteger("1").shiftLeft(i));
            }
        }
        return fingerprint;
    }


    /**
     * 对单个的分词进行hash计算;
     * @param source
     * @return
     */
    private BigInteger hash(String source) {
        if (source == null || source.length() == 0) {
            return new BigInteger("0");
        } else {
            //当sourece 的长度过短,会导致hash算法失效,因此需要对过短的词补偿
            while (source.length() < 3) {
                source = source + source.charAt(0);
            }
            char[] sourceArray = source.toCharArray();
            BigInteger x = BigInteger.valueOf(((long) sourceArray[0]) << 7);
            BigInteger m = new BigInteger("1000003");
            BigInteger mask = new BigInteger("2").pow(this.hashbits).subtract(new BigInteger("1"));
            for (char item : sourceArray) {
                BigInteger temp = BigInteger.valueOf((long) item);
                x = x.multiply(m).xor(temp).and(mask);
            }
            x = x.xor(new BigInteger(String.valueOf(source.length())));
            if (x.equals(new BigInteger("-1"))) {
                x = new BigInteger("-2");
            }
            return x;
        }
    }

    /**
     * 计算海明距离,海明距离越小说明越相似;
     * @param other
     * @return
     */
    private int hammingDistance(MySimHash other) {
        BigInteger m = new BigInteger("1").shiftLeft(this.hashbits).subtract(
                new BigInteger("1"));
        BigInteger x = this.strSimHash.xor(other.strSimHash).and(m);
        int tot = 0;
        while (x.signum() != 0) {
            tot += 1;
            x = x.and(x.subtract(new BigInteger("1")));
        }
        return tot;
    }


    public double getSemblance(MySimHash s2 ){
        double i = (double) this.hammingDistance(s2);
        return i/this.hashbits ;
    }

    public static void main(String[] args) {
        String text1 = "天气非常好";
        String text2 = "天气真不错";
        MySimHash hash1 = new MySimHash(text1, 64);
        MySimHash hash2 = new MySimHash(text2, 64);
        System.out.println(hash1.hammingDistance(hash2) );
        System.out.println(hash1.getSemblance(hash2));
    }
}

注意:
python版是使用jieba分词,分词后带有权重值。java版使用ansj分词,需要自行手动设置权重。


参考:
https://www.cnblogs.com/xujunkai/p/12038649.html

  • 4
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Simhash算法是一种用于文本相似度比较的算法,它可以通过将文本的特征哈希为一个固定长度的二进制数字,然后比较这些数字的汉明距离来确定文本的相似度。 以下是一个简单的Simhash算法实现Python代码: ```python import re import hashlib def simhash(text): # 定义哈希位数 hashbits = 64 # 定义特征向量 v = [0] * hashbits # 分词 words = re.findall(r'\w+', text.lower()) # 遍历每个单词 for word in words: # 计算单词的哈希值 hashval = int(hashlib.md5(word.encode('utf-8')).hexdigest(), 16) # 遍历哈希值的每一位 for i in range(hashbits): # 如果该位为1,加上权重,否则减去权重 if (hashval & (1 << i)): v[i] += 1 else: v[i] -= 1 # 生成simhashsimhash = 0 for i in range(hashbits): if v[i] > 0: simhash |= 1 << i return simhash ``` 使用示例: ```python text1 = "This is a test text for simhash algorithm." text2 = "This is a test text for simhash algorithms." # 计算simhashsimhash1 = simhash(text1) simhash2 = simhash(text2) # 计算汉明距离 hamming_distance = bin(simhash1 ^ simhash2).count('1') # 打印结果 print("Simhash1:", simhash1) print("Simhash2:", simhash2) print("Hamming distance:", hamming_distance) ``` 输出结果: ``` Simhash1: 12605323822822352684 Simhash2: 12769845372629383580 Hamming distance: 9 ``` 上述代码计算了两个文本的simhash值,并计算了它们的汉明距离。由于两个文本很相似,它们的simhash值很接近,但是由于有一些不同的单词,它们的汉明距离为9,说明它们的相似度比较高。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值