第一次个人编程作业

项目|内容
--|:--😐--:
这个作业属于哪个课程|软件工程
这个作业要求在哪里|作业要求
这个作业的目标|计算文本相似度+PSP表格+GitHub管理+单元测试

Github链接

核心算法及接口设计展示

核心算法

  • 我通过查阅资料发现,余弦相似性虽然被广泛应用,但计算复杂度偏高,于是我选择使用Jaccard相似度
  • 核心算法:Jaccard相似度
    • 通过提取样本描述的关键词,再计算两组关键词的交集并集,从而检测样本描述的重复度,即Jaccard相似度
    • 计算两个集合之间的相似程度,元素的“取值”为0或1
    • 对集合A和B,Jaccard相似度计算如下:Jaccard(A, B)= |A intersectB| / |A union B|相似度数值在[0, 1]之间,当A==B的时候,为1.

接口设计及实现

  • 设计思路

    • 获取文件
    • 过滤特殊字符
    • 分词并提取关键词
    • 计算及除零处理
    • 得出相似度
  • 分词:需要用到jieba库的分词模式,我选择了精确模式

    • 精确模式:试图将句子最精确地切开,适合文本分析
  • 计算及除零处理:收集样本交集个数和样本并集个数的比值,用两个集合中不同元素所占元素的比例来衡量两个样本之间的相似度

  • 实现总代码

import jieba
import jieba.analyse
import re

class JaccardSim(object):
# 构造函数
def init(self, a, b):
self.c1 = a
self.c2 = b

# 提取关键词
@staticmethod
def getKeywords(content):
    # 过滤特殊字符
    filter = re.compile(u"[^a-zA-Z0-9\u4e00-\u9fa5]")
    content = filter.sub(' ', content)
    # 利用jieba分词切开句子,并提取关键词
    string = [i for i in jieba.cut(content, cut_all=True) if i != '']
    result = jieba.analyse.extract_tags("|".join(string), topK=200, withWeight=False)
    return result

def main(self):
    # 提取关键词
    keyA = self.getKeywords(self.c1)
    keyB = self.getKeywords(self.c2)

    # 计算
    intersection = len(list(set(keyA).intersection(set(keyB))))
    union = len(list(set(keyA).union(set(keyB))))

    # 除零处理
    sim = float(intersection) / union if union != 0 else 0
    return sim

测试

if name == 'main':
p1 = input("输入论文原文的文件的绝对路径:")
p2 = input("输入抄袭版论文的文件的绝对路径:")
if not jieba.os.path.exists(p1):
print("原文路径输入错误")
exit()
if not jieba.os.path.exists(p2):
print("待查重文章路径输入错误")
exit()
with open(p1, 'r', encoding='UTF-8') as x,
open(p2, 'r', encoding='UTF-8') as y:
a = x.read()
b = y.read()
similarity = JaccardSim(a, b)
similarity = similarity.main()
print('相似度: %.2f%%' % (similarity * 100))`

性能改进

  • 改进前
    • 可以看到,除了main.py以外,获取关键词函数——getKeywords耗费时间较多,过滤与分词算法有待改进
  • 改进后
    • 可以看到,经改进后,getKeywords耗费时间减少

代码覆盖率及测试展示

  • 使用pycharm进行单元测试并统计代码覆盖率,得到代码覆盖率如下

  • 测试结果




附录

  • PSP表格
项目 内容 预估耗时(分钟) 预估耗时(分钟)
Planning 计划 25 36
Estimate 估计这个任务需要多少时间 240 260
Development 开发 300 350
Analysis 需求分析 (包括学习新技术) 100 145
Design Spec 生成设计文档 30 40
Design Review 设计复审 20 18
Coding Standard 代码规范 (为目前的开发制定合适的规范) 10 6
Design 具体设计 20 30
Coding 具体编码 200 230
Code Review 代码复审 20 10
Test 测试(自我测试,修改代码,提交修改) 50 40
Reporting 报告 20 25
Test Repor 测试报告 25 20
Size Measurement 计算工作量 10 10
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 5 10
total 合计 1045 1210
posted @ 2020-09-25 02:47  RabbitLau  阅读(146)  评论(0编辑  收藏  举报