論文查重python文本相似性計(jì)算simhash源碼
場(chǎng)景:
1.計(jì)算SimHash值,及Hamming距離。
2.SimHash適用于較長(zhǎng)文本(大于三五百字)的相似性比較,文本越短誤判率越高。
Python實(shí)現(xiàn):
代碼如下
# -*- encoding:utf-8 -*- import math import jieba import jieba.analyse class SimHash(object): def getBinStr(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 getWeight(self, source): return ord(source) def unwrap_weight(self, arr): ret = "" for item in arr: tmp = 0 if int(item) > 0: tmp = 1 ret += str(tmp) return ret def sim_hash(self, rawstr): seg = jieba.cut(rawstr) keywords = jieba.analyse.extract_tags("|".join(seg), topK=100, withWeight=True) ret = [] for keyword, weight in keywords: binstr = self.getBinStr(keyword) keylist = [] for c in binstr: weight = math.ceil(weight) if c == "1": keylist.append(int(weight)) else: keylist.append(-int(weight)) ret.append(keylist) # 降維 rows = len(ret) cols = len(ret[0]) result = [] for i in range(cols): tmp = 0 for j in range(rows): tmp += int(ret[j][i]) if tmp > 0: tmp = "1" elif tmp <= 0: tmp = "0" result.append(tmp) return "".join(result) def distince(self, hashstr1, hashstr2): length = 0 for index, char in enumerate(hashstr1): if char == hashstr2[index]: continue else: length += 1 return length if __name__ == "__main__": simhash = SimHash() str1 = '咱哥倆誰(shuí)跟誰(shuí)啊' str2 = '咱們倆誰(shuí)跟誰(shuí)啊' hash1 = simhash.sim_hash(str1) print(hash1) hash2 = simhash.sim_hash(str2) distince = simhash.distince(hash1, hash2) value = 5 print("simhash", distince, "距離:", value, "是否相似:", distince<=value)
以上就是論文查重python文本相似性計(jì)算simhash源碼的詳細(xì)內(nèi)容,更多關(guān)于python文本相似性計(jì)算simhash的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python2.7使用plotly繪制本地散點(diǎn)圖和折線圖
這篇文章主要為大家詳細(xì)介紹了python2.7使用plotly繪制本地散點(diǎn)圖和折線圖實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04基于Python編寫一個(gè)簡(jiǎn)單的服務(wù)注冊(cè)發(fā)現(xiàn)服務(wù)器
我們都知道有很多的非常著名的注冊(cè)服務(wù)器,例如:?Consul、ZooKeeper、etcd,甚至借助于redis完成服務(wù)注冊(cè)發(fā)現(xiàn)。但是本篇文章我們將使用python?socket寫一個(gè)非常簡(jiǎn)單的服務(wù)注冊(cè)發(fā)現(xiàn)服務(wù)器,感興趣的可以了解一下2023-04-04python函數(shù)存儲(chǔ)在模塊的優(yōu)點(diǎn)及用法總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于python函數(shù)存儲(chǔ)在模塊的優(yōu)點(diǎn)及用法相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10Python Numpy 自然數(shù)填充數(shù)組的實(shí)現(xiàn)
今天小編就為大家分享一篇Python Numpy 自然數(shù)填充數(shù)組的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python實(shí)現(xiàn)簡(jiǎn)單遺傳算法(SGA)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單遺傳算法SGA,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Python破解BiliBili滑塊驗(yàn)證碼的思路詳解(完美避開人機(jī)識(shí)別)
這篇文章主要介紹了Python破解BiliBili滑塊驗(yàn)證碼的思路,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02python接口自動(dòng)化之ConfigParser配置文件的使用詳解
這篇文章主要介紹了python接口自動(dòng)化之ConfigParser配置文件的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08解決項(xiàng)目pycharm能運(yùn)行,在終端卻無(wú)法運(yùn)行的問(wèn)題
今天小編就為大家分享一篇解決項(xiàng)目pycharm能運(yùn)行,在終端卻無(wú)法運(yùn)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01