論文查重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)文章
Python創(chuàng)建對(duì)稱矩陣的方法示例【基于numpy模塊】
這篇文章主要介紹了Python創(chuàng)建對(duì)稱矩陣的方法,結(jié)合實(shí)例形式分析了Python基于numpy模塊實(shí)現(xiàn)矩陣運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法
今天小編就為大家分享一篇用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
分享十個(gè)Python超級(jí)好用提高工作效率的自動(dòng)化腳本
在這個(gè)自動(dòng)化時(shí)代,我們有很多重復(fù)無(wú)聊的工作要做。?想想這些你不再需要一次又一次地做的無(wú)聊的事情,讓它自動(dòng)化,讓你的生活更輕松。本文分享了10個(gè)Python自動(dòng)化腳本,希望對(duì)大家有所幫助2022-11-11
python批量導(dǎo)出導(dǎo)入MySQL用戶的方法
這篇文章主要介紹了2013-11-11
python 定時(shí)器,輪詢定時(shí)器的實(shí)例
今天小編就為大家分享一篇python 定時(shí)器,輪詢定時(shí)器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼
使用微信,定時(shí)往指定的微信群里發(fā)送指定信息。接下來(lái)通過(guò)本文給大家分享Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼,需要的朋友可以參考下2019-07-07

