論文查重python文本相似性計算simhash源碼
場景:
1.計算SimHash值,及Hamming距離。
2.SimHash適用于較長文本(大于三五百字)的相似性比較,文本越短誤判率越高。
Python實現(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 = '咱哥倆誰跟誰啊' str2 = '咱們倆誰跟誰啊' 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文本相似性計算simhash源碼的詳細內(nèi)容,更多關(guān)于python文本相似性計算simhash的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Python編寫一個簡單的服務(wù)注冊發(fā)現(xiàn)服務(wù)器
我們都知道有很多的非常著名的注冊服務(wù)器,例如:?Consul、ZooKeeper、etcd,甚至借助于redis完成服務(wù)注冊發(fā)現(xiàn)。但是本篇文章我們將使用python?socket寫一個非常簡單的服務(wù)注冊發(fā)現(xiàn)服務(wù)器,感興趣的可以了解一下2023-04-04python函數(shù)存儲在模塊的優(yōu)點及用法總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于python函數(shù)存儲在模塊的優(yōu)點及用法相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10Python Numpy 自然數(shù)填充數(shù)組的實現(xiàn)
今天小編就為大家分享一篇Python Numpy 自然數(shù)填充數(shù)組的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python破解BiliBili滑塊驗證碼的思路詳解(完美避開人機識別)
這篇文章主要介紹了Python破解BiliBili滑塊驗證碼的思路,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02python接口自動化之ConfigParser配置文件的使用詳解
這篇文章主要介紹了python接口自動化之ConfigParser配置文件的使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08