網(wǎng)站首頁 編程語言 正文
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): ? ? ? ? ''' ? ? ? ? ? ? 計算兩個simhash的漢明距離 ? ? ? ? ''' ? ? ? ? length = 0 ? ? ? ? for index, char in enumerate(hashstr1): ? ? ? ? ? ? if char == hashstr2[index]: ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? length += 1 ? ? ? ? return length
1.1分詞
分詞是將文本文檔進行分割成不同的詞組,比如詞1為:今天星期四,詞2為:今天星期五
得出分詞結(jié)果為【今天,星期四】【今天,星期五】
1.2hash
hash是將分詞結(jié)果取hash值
星期四hash為:0010001100100000101001101010000000101111011010010001100011011110
今天hash為:0010001111010100010011110001110010100011110111111011001011110101
星期五hash為:0010001100100000101001101010000000101111011010010000000010010001
1.3加權(quán)
1.4合并
1.5降維
降維是將合并的結(jié)果進行降維,如果值大于0,則置為1小于0 則置為0,因此得到的結(jié)果為:
2、simhash比對
一般simhash采用海明距離來進行計算相似度,海明距離計算如下:
對于A,B兩個n維二進制數(shù)
二者的海明距離為:
其中:
舉例:
1000與1111的海明距離為3
原文鏈接:https://blog.csdn.net/qq_39187538/article/details/122853908
相關(guān)推薦
- 2022-06-16 Rancher+Docker+SpringBoot實現(xiàn)微服務(wù)部署、擴容、環(huán)境監(jiān)控_docker
- 2022-12-10 Flutter?狀態(tài)管理scoped?model源碼解讀_Android
- 2022-10-24 C語言詳解分析進程控制中進程終止的實現(xiàn)_C 語言
- 2022-06-12 Python中常用的內(nèi)置函數(shù)_python
- 2023-04-19 清楚詳解Android?進程間圖傳遞圖形buffer原理_Android
- 2022-10-11 C++函數(shù)模板與類模板相同與不同介紹_C 語言
- 2022-11-24 Flutter開發(fā)setState能否在build中直接調(diào)用詳解_Android
- 2022-08-01 GoLand一鍵上傳項目到遠程服務(wù)器的方法步驟_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支