溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

python如何實(shí)現(xiàn)Simhash算法

發(fā)布時(shí)間:2022-02-15 09:09:42 來(lái)源:億速云 閱讀:239 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹python如何實(shí)現(xiàn)Simhash算法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

1、simhash步驟

simhash包含分詞、hash、加權(quán)、合并、降維五大步驟

simhash代碼如下:

import jieba
import jieba.analyse
import numpy as np

class SimHash(object):
    def simHash(self, content):
        seg = jieba.cut(content)
        # jieba.analyse.set_stop_words('stopword.txt')
        # jieba基于TF-IDF提取關(guān)鍵詞
        keyWords = jieba.analyse.extract_tags("|".join(seg), topK=10, withWeight=True)

        keyList = []
        for feature, weight in keyWords:
            # print('feature:' + feature)
            print('weight: {}'.format(weight))
            # weight = math.ceil(weight)
            weight = int(weight)
            binstr = self.string_hash(feature)
            print('feature: %s , string_hash %s' % (feature, binstr))
            temp = []
            for c in binstr:
                if (c == '1'):
                    temp.append(weight)
                else:
                    temp.append(-weight)
            keyList.append(temp)
        listSum = np.sum(np.array(keyList), axis=0)
        if (keyList == []):
            return '00'
        simhash = ''
        for i in listSum:
            if (i > 0):
                simhash = simhash + '1'
            else:
                simhash = simhash + '0'
        return simhash

    def string_hash(self, source):
        if source == "":
            return 0
        else:
            temp = source[0]
            temp1 = ord(temp)
            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 getDistance(self, hashstr1, hashstr2):
        '''
            計(jì)算兩個(gè)simhash的漢明距離
        '''
        length = 0
        for index, char in enumerate(hashstr1):
            if char == hashstr2[index]:
                continue
            else:
                length += 1

        return length

1.1分詞

分詞是將文本文檔進(jìn)行分割成不同的詞組,比如詞1為:今天星期四,詞2為:今天星期五

得出分詞結(jié)果為【今天,星期四】【今天,星期五】

1.2hash

hash是將分詞結(jié)果取hash值
星期四hash為:0010001100100000101001101010000000101111011010010001100011011110
今天hash為:0010001111010100010011110001110010100011110111111011001011110101
星期五hash為:0010001100100000101001101010000000101111011010010000000010010001

1.3加權(quán)

python如何實(shí)現(xiàn)Simhash算法

1.4合并

python如何實(shí)現(xiàn)Simhash算法

1.5降維

降維是將合并的結(jié)果進(jìn)行降維,如果值大于0,則置為1小于0 則置為0,因此得到的結(jié)果為:

python如何實(shí)現(xiàn)Simhash算法

2、simhash比對(duì)

一般simhash采用海明距離來(lái)進(jìn)行計(jì)算相似度,海明距離計(jì)算如下:

對(duì)于A,B兩個(gè)n維二進(jìn)制數(shù)

python如何實(shí)現(xiàn)Simhash算法

二者的海明距離為:

python如何實(shí)現(xiàn)Simhash算法

其中:

python如何實(shí)現(xiàn)Simhash算法

舉例:

1000與1111的海明距離為3

以上是“python如何實(shí)現(xiàn)Simhash算法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI