python實(shí)現(xiàn)Simhash算法
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 length1.1分詞
分詞是將文本文檔進(jìn)行分割成不同的詞組,比如詞1為:今天星期四,詞2為:今天星期五
得出分詞結(jié)果為【今天,星期四】【今天,星期五】
1.2hash
hash是將分詞結(jié)果取hash值
星期四hash為:0010001100100000101001101010000000101111011010010001100011011110
今天hash為:0010001111010100010011110001110010100011110111111011001011110101
星期五hash為:0010001100100000101001101010000000101111011010010000000010010001
1.3加權(quán)

1.4合并

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

2、simhash比對(duì)
一般simhash采用海明距離來(lái)進(jìn)行計(jì)算相似度,海明距離計(jì)算如下:
對(duì)于A,B兩個(gè)n維二進(jìn)制數(shù)

二者的海明距離為:

其中:

舉例:
1000與1111的海明距離為3
到此這篇關(guān)于python實(shí)現(xiàn)Simhash算法的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)Simhash算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV MediaPipe實(shí)現(xiàn)顏值打分功能
這篇文章主要介紹了通過(guò)OpenCV MediaPipe實(shí)現(xiàn)攝像頭實(shí)時(shí)檢測(cè)顏值打分功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以了解一下2021-12-12
使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼
這篇文章主要介紹了使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
淺談Python 中整型對(duì)象的存儲(chǔ)問(wèn)題
這篇文章主要介紹了淺談Python 中整型對(duì)象的存儲(chǔ)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-05-05
Keras中的多分類損失函數(shù)用法categorical_crossentropy
這篇文章主要介紹了Keras中的多分類損失函數(shù)用法categorical_crossentropy,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理
這篇文章主要介紹了Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理,需要的朋友可以參考下2020-02-02
詳解python項(xiàng)目實(shí)戰(zhàn):模擬登陸CSDN
這篇文章主要介紹了python項(xiàng)目實(shí)戰(zhàn):模擬登陸CSDN,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
一小時(shí)學(xué)會(huì)TensorFlow2之大幅提高模型準(zhǔn)確率
這篇文章主要介紹了TensorFlow2之大幅提高模型準(zhǔn)確率,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
pytorch查看torch.Tensor和model是否在CUDA上的實(shí)例
今天小編就為大家分享一篇pytorch查看torch.Tensor和model是否在CUDA上的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

