192
社区成员




这个作业属于哪个课程 | 班级的链接 |
---|---|
这个作业要求在哪里 | 作业要求的链接 |
这个作业的目标 | 设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率 |
其他参考文献 | … |
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 60 | 60 |
· Estimate | · 估计这个任务需要多少时间 | 360 | 400 |
Development | 开发 | 120 | 160 |
· Analysis | · 需求分析 (包括学习新技术) | 120 | 150 |
· Design Spec | · 生成设计文档 | 20 | 20 |
· Design Review | · 设计复审 | 30 | 20 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 15 | 10 |
· Design | · 具体设计 | 60 | 45 |
· Coding | · 具体编码 | 120 | 150 |
· Code Review | · 代码复审 | 30 | 40 |
· Test | · 测试(自我测试,修改代码,提交修改) | 60 | 70 |
Reporting | 报告 | 60 | 80 |
· Test Repor | · 测试报告 | 20 | 25 |
· Size Measurement | · 计算工作量 | 30 | 40 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 40 |
· 合计 | 1135 | 1310 |
计算两篇论文中词语的频率,然后将频率转换为向量形式,最后用余弦相似性测量两个向量的夹角的余弦值来度量它们之间的相似性。
1.预处理文本
将中文文本去除标点符号和空格,并使用中文分词库jieba将文本分成单词,然后过滤掉常用停用词。这一步的目的是将文本转换成一个词序列,方便后续的相似度计算。
2.计算词语出现频率
计算每个词在文本中出现的次数,并将其存储为一个字典,键为词,值为出现次数。
3.计算余弦相似度
首先计算两篇论文中词语的频率,然后使用余弦相似度公式计算相似度。在余弦相似度公式中,分子表示向量点积,即两个向量中对应位置元素的乘积之和;分母表示向量长度,即各个元素的平方和的平方根。
一个functions类中有三个函数
分别为preprocess(),get_word_freq(),cosine_similarity(),cosine_similarity()函数中调用了前两个函数
运行程序后,在一行中输入原文地址 抄袭论文地址 答案文件地址,三个绝对路径用空格隔开。
关键函数为cosine_similarity()函数
流程图如下
算法中对文本进行了预处理,去除标点符号和空格并过滤掉常用停用词,能使查重结果更准确。
用pycharm的Profile插件对程序进行性能分析
程序中消耗最大的函数是cosine_similarity()函数
def cosine_similarity(self, text1, text2):
try:
# 计算余弦相似度
words1 = Functions.preprocess(self, text1)
words2 = Functions.preprocess(self, text2)
# 计算词语频率
words1_freq = Functions.get_word_freq(self, words1)
words2_freq = Functions.get_word_freq(self, words2)
# 计算向量点积
dot_product = 0
for word in words1_freq:
if word in words2_freq:
dot_product += words1_freq[word] * words2_freq[word]
# 计算向量长度
words1_length = math.sqrt(sum([freq ** 2 for freq in words1_freq.values()]))
words2_length = math.sqrt(sum([freq ** 2 for freq in words2_freq.values()]))
# 计算余弦相似度
cosine_sim = dot_product / (words1_length * words2_length)
return round(cosine_sim, 2) # 结果保留两位小数
except:
print("传入参数错误")
对wen1.txt和wen2.txt两个文本进行查重,并把查重率写入答案文档
对cosine_similarity()进行测试
对已知查重率的文本进行测试,看测试结果是否正确
测试代码
import unittest
from test_cosine_similarity import cosine_similarity
class MyTestCase(unittest.TestCase):
def test_something(self):
article1, article2, ans = input('请输三个地址:').split()
# 读取文本文件
with open(article1, 'r', encoding='utf-8') as f1, open(article2, 'r', encoding='utf-8') as f2:
texta = f1.read()
textb = f2.read()
# 计算余弦相似度
cos_sim = cosine_similarity(texta, textb)
f1.close()
f2.close()
self.assertEqual(cosine_similarity(texta,textb),0.94)
if __name__ == '__main__':
unittest.main()
代码覆盖率
在模块函数中进行异常检测,检测出错误即提示传入参数错误
在主函数中进行异常检测,检测文件是否存在,检测出错误即提示文件不存在