【python】实现一个简易的文本相似度检测程序

1,780次阅读
没有评论

## 前言

我手头存了不少小说txt文档,一直都想整理一番,但是手工整理太累了所以也没动手。这半年来感觉我的python水平有了很大提升,于是打算写个程序帮我整理。

首先是去重,因为txt文档来源于不同地方,标题作者名什么的有可能不一致,内容上排版稍微差一点hash值也就变了,所以无论是靠文件名还是hash值检测重复文档都不可靠,干脆直接检测文本的相似度吧。网上搜了一下别人写的代码,整合了一下实现了一个最简单的文本相似度检测。

因为之前没接触过自然语言学习,所以本文的理解和代码可能有误,欢迎评论区指正。

(https://github.com/caly5144/shu-s-project/blob/master/text/)

## 原理

可以参考这两篇文章:

[https://blog.csdn.net/qq_43350003/article/details/105392702](https://blog.csdn.net/qq_43350003/article/details/105392702)

[https://www.cnblogs.com/airnew/p/9563703.html](https://www.cnblogs.com/airnew/p/9563703.html)

首先介绍一下什么是**词袋模型**。

例句:

* Jane wants to go to Shenzhen.
* Bob wants to go to Shanghai.

将所有词语装进一个袋子里,**不考虑其词法和语序的问题** ,即每个词语都是独立的。例如上面 2 个例句,就可以构成一个词袋,袋子里包括 Jane、wants、to、go、Shenzhen、Bob、Shanghai。假设建立一个数组(或词典)用于映射匹配

> [Jane, wants, to, go, Shenzhen, Bob, Shanghai]

那么上面两个例句就可以用以下两个向量表示,对应的下标与映射数组的下标相匹配,其值为该词语出现的次数

> [1,1,2,1,1,0,0]
> [0,1,2,1,0,1,1]

这两个词频向量就是词袋模型。

我们知道,一篇文章除了词频外,语序也是很重要的,词向量模型就是考虑**词语位置关系**的一种模型。因为这次任务并不是专业的查抄袭,所以词袋模型就足够了。

有了词频向量后,还需要一种指标来衡量两端文本间的相似度。本文采用**余弦相似度算法**,所谓余弦相似度算法,就是用一个向量空间中两个向量夹角间的余弦值作为衡量两个个体之间差异的大小,余弦值接近 1,夹角趋于 0,表明两个向量越相似,余弦值接近于 0,夹角趋于 90 度,表明两个向量越不相似。

公式如下:

$$
cos (theta)=frac{sum_{i=1}^{n}left(x_{i} times y_{i}right)}{sqrt{sum_{i=1}^{n}left(x_{i}right)^{2}} times sqrt{sum_{i=1}^{n}left(y_{i}right)^{2}}}

$$

## 代码

主要参考的就是这篇文章:[https://www.cnblogs.com/airnew/p/9563703.html](https://www.cnblogs.com/airnew/p/9563703.html)

我主要是将其numpy化,加快了运行速度。另外,考虑到txt文档的编码方式可能不一样,所以写了一个函数判断所打开文档的编码。

代码实现思路和上一节类似,首先用jieba分词将文档进行分词,然后去掉标点符号。为了防止介词状语等影响相似度的准确性,所以分词后要将这类词语去掉。本文使用了百度停用词,存放在`./stopwords`文件夹下,里面还有中文停用词表、哈工大停用词表、四川大学机器智能实验室停用词库,数据来源于[https://github.com/goto456/stopwords](https://github.com/goto456/stopwords),可自行修改。

接着就是生成两个文档的one-hot向量,计算出余弦相似度。代码如下:

“`python
import jieba
import re
import os
import numpy as np

import codecs
import chardet

class SimilarTest(object):
def __init__(self, a_path, b_path):
s1_code = self.get_code(a_path)
s2_code = self.get_code(b_path)
chars_pattern = re.compile(
r'[s·’!”#$%&'()#!()*+,-./:;<=>?@,:?¥★*、….>【】[]《》?“”‘’[]^_`{|}~。—]+’)
s1 = open(a_path, ‘r’, encoding=s1_code, errors=’ignore’).read()
s2 = open(b_path, ‘r’, encoding=s2_code, errors=’ignore’).read()
# 去除标点符号
self.s1 = chars_pattern.sub(“”, s1)
self.s2 = chars_pattern.sub(“”, s2)

# 设置停用词
self.stopwords = open(‘./stopwords/baidu_stopwords.txt’,
‘r’, encoding=’utf-8-sig’).read().splitlines()

def test(self):
s1_split = np.array(jieba.lcut(self.s1))
s2_split = np.array(jieba.lcut(self.s2))
s1_cut = s1_split[~np.isin(s1_split, self.stopwords)]
s2_cut = s2_split[~np.isin(s2_split, self.stopwords)]
word_set = np.union1d(s1_cut, s2_cut)
# 用字典保存两篇文章中出现的所有词并编上号
word_dict = dict()
for i, word in enumerate(word_set):
word_dict[word] = i

# 根据词袋模型统计词在每篇文档中出现的次数,形成向量
s1_cut_code = np.zeros(len(word_dict))
s2_cut_code = np.zeros(len(word_dict))

for word in s1_cut:
s1_cut_code[word_dict[word]] += 1
for word in s2_cut:
s2_cut_code[word_dict[word]] += 1

# 计算余弦相似度
try:
cos_arr = np.sum(s1_cut_code * s2_cut_code)
sq1 = np.power(s1_cut_code, 2).sum()
sq2 = np.power(s2_cut_code, 2).sum()
result = np.around(cos_arr / (np.sqrt(sq1) * np.sqrt(sq2)), 3)
except ZeroDivisionError:
result = 0.0
print(“文本相似度为:{}”.format(result))

@staticmethod
def get_code(path):
with open(path, ‘rb’) as file:
data = file.read(200)
dicts = chardet.detect(data)
return dicts[“encoding”]

if __name__ == ‘__main__’:
a_path = ‘atext.txt’
b_path = ‘btext.txt’
textST = SimilarTest(a_path, b_path)
textST.test()
“`

效果:

![image.png](https://i.yanshu.work/image/601299737a905.jpg)Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 3 月
 12345
6789101112
13141516171819
20212223242526
2728293031  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别 量子位的朋友们 2025-1...