论文查重python文本相似性计算simhash源码
作者:别None了 发布时间:2023-02-05 18:11:35
标签:python,simhash,论文查重,文本相似性
场景:
1.计算SimHash值,及Hamming距离。
2.SimHash适用于较长文本(大于三五百字)的相似性比较,文本越短误判率越高。
Python实现:
代码如下
# -*- 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)
来源:https://coderl.blog.csdn.net/article/details/122740744


猜你喜欢
- 本文详细讲述了Python使用MySQLdb for Python操作数据库的方法,分享给大家供大家参考。具体如下:一般来说网站就是要和数据
- 前言我们这里主要是利用requests模块和bs4模块进行简单的爬虫的讲解,让大家可以对爬虫有了初步的认识,我们通过爬几个简单网站,让大家循
- 一、Go语言下载go语言官方下载地址:https://golang.org/dl/找到适合你系统的版本下载,本人下载的是windows版本。
- 模糊数据库指能够处理模糊数据的数据库。一般的数据库都是以二直逻辑和精确的数据工具为基础的,不能表示许多模糊不清的事情。随着模糊数学理论体系的
- python random库简单使用demo当我们需要生成随机数或者从一个序列中随机选择元素时,可以使用 Python 内置的 random
- 本文主要内容python MySQLdb数据库批量插入insert,更新update的:1.python MySQLdb的使用,写了一个基类
- 合并在numpy中合并两个arraynumpy中可以通过concatenate,参数axis=0表示在垂直方向上合并两个数组,等价于np.v
- 1、文件编码:指的是页面文件(.html,.php等)本身是以何种编码来保存的。记事本和Dreamweaver在打开页面时候会自动识别文件编
- 前端使用ajax进行数据交互时:$.ajax({ cache: false, type: "POST", url: {%
- 例一:使用httplib访问某个url然后获取返回的内容:import httplibconn=httplib.HTTPConne
- MaxDB和MySQL是独立的数据库管理服务器。系统间的协同性是可能的,通过相应的方式,系统能够彼此交换数据。要想在MaxDB和MySQL之
- 目录相关背景直方图计算法图像指纹与汉明距离平均哈希法(aHash)感知哈希算法(pHash)dHash算法在网上看到python做图像识别的
- 方法说明: 同步版的fs.writeFile() 。语法:fs.writeFileSync(filename, data, [o
- 1.单列运算在Pandas中,DataFrame的一列就是一个Series, 可以通过map来对一列进行操作:df['col2
- Go文档中展示了多种方式实现外部资源嵌入,包括文本文件、图片、ios文件等:文本文件package mainimport _ "e
- 一、复习首先将上次画的矩形做复杂一些的小程序:import pygame,sys, randompygame.init()screen =
- 一、导入excel文件和相关库import pandas;import matplotlib;from pandas.tools.plott
- 之前用Class类来搭建神经网络class Neuro_net(torch.nn.Module): ""&q
- 1.Django默认已经提供了认证系统Auth模块。认证系统包含:用户管理权限用户组密码哈希系统用户登录或内容显示的表单和视图一个可插拔的后
- 关于Ajax在使用中要使浏览器产生前进后退的方法,网上比较多的方法有两种:一是采用hash值的方式,这是我们在地图preview版中使用的方