python實現(xiàn)Simhash算法
1、simhash步驟
simhash包含分詞、hash、加權、合并、降維五大步驟
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提取關鍵詞
? ? ? ? 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):
? ? ? ? '''
? ? ? ? ? ? 計算兩個simhash的漢明距離
? ? ? ? '''
? ? ? ? length = 0
? ? ? ? for index, char in enumerate(hashstr1):
? ? ? ? ? ? if char == hashstr2[index]:
? ? ? ? ? ? ? ? continue
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? length += 1
? ? ? ? return length1.1分詞
分詞是將文本文檔進行分割成不同的詞組,比如詞1為:今天星期四,詞2為:今天星期五
得出分詞結果為【今天,星期四】【今天,星期五】
1.2hash
hash是將分詞結果取hash值
星期四hash為:0010001100100000101001101010000000101111011010010001100011011110
今天hash為:0010001111010100010011110001110010100011110111111011001011110101
星期五hash為:0010001100100000101001101010000000101111011010010000000010010001
1.3加權

1.4合并

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

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

二者的海明距離為:

其中:

舉例:
1000與1111的海明距離為3
到此這篇關于python實現(xiàn)Simhash算法的文章就介紹到這了,更多相關python實現(xiàn)Simhash算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV)
這篇文章主要介紹了Python實現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
NumPy實現(xiàn)多維數(shù)組中的線性代數(shù)
本文主要介紹了NumPy實現(xiàn)多維數(shù)組中的線性代數(shù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
python中from module import * 的一個坑
from module import *把module中的成員全部導到了當前的global namespace,訪問起來就比較方便了。當然,python style一般不建議這么做,因為可能引起name conflict。2014-07-07

