1 Fork me on GitHub

第一次个人编程作业

论文查重

GitHub链接

计算模块接口的设计与实现过程

  1. 面向网络编程了好些天,一开始看了好多java的实现方法,奈何Java刚开始学,只知道非常基础的东西,于是就转战python。最后学习到了这个利用jieba+余弦向量的方法。
    大佬的链接呈上: 文本相似度算法的对比
    2.流程图


没错,流程就是这么简单

  • 类的定义:
    定义了一个CosineSimilarity类用来jieba分词与余弦相似度计算
    从路径读取文本,通过extract_keyword(content)函数提取关键词,函数最后调用了 jieba.analyse.extract_tags( )来获取关键词
    def __init__(self, content_x1, content_y2):
        self.s1 = content_x1
        self.s2 = content_y2

    @staticmethod
    def extract_keyword(content):  # 提取关键词
        # 正则过滤 html 标签
        re_exp = re.compile(r'(<style>.*?</style>)|(<[^>]+>)', re.S)
        content = re_exp.sub(' ', content)
        # html 转义符实体化
        content = html.unescape(content)
        # 切割
        seg = [i for i in jieba.cut(content, cut_all=True) if i != '']
        # 提取关键词
        keywords = jieba.analyse.extract_tags("|".join(seg), topK=200, withWeight=False)
        return keywords
  • 余弦相似度计算函数
    def main(self):
        # 提取关键词
        keywords1 = self.extract_keyword(self.s1)
        keywords2 = self.extract_keyword(self.s2)
        # 词的并集
        union = set(keywords1).union(set(keywords2))
        # 编码
        word_dict = {}
        i = 0
        for word in union:
            word_dict[word] = i
            i += 1
        # oneHot编码
        s1_cut_code = self.one_hot(word_dict, keywords1)
        s2_cut_code = self.one_hot(word_dict, keywords2)
        # 余弦相似度计算
        sample = [s1_cut_code, s2_cut_code]
        # 除零处理
        try:
            sim = cosine_similarity(sample)
            return sim[1][0]
        except Exception as e:
            print(e)
            return 0.0

计算模块接口部分的性能改进

用pycharm自带的profile分析性能


可以看到这次分析得到程序总共花了3591ms,整体来看大部分程序都花了3秒多

各部分耗时具体如下

计算模块部分单元测试展示

if __name__ == '__main__':
    with open("sys.argv[1], 'r', encoding='utf-8') as x, open("sys.argv[2], 'r', encoding='utf-8') as y :
        content_x = x.read()
        content_y = y.read()
        sim = CosineSimilarity(content_x, content_y)
        sim = sim.main()
        print("%.2f%%" % (sim * 100))

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_add.txt
83.50%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_del.txt
73.00%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_dis_1.txt
88.50%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_dis_3.txt
87.00%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_dis_7.txt
83.50%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_dis_10.txt
75.00%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_dis_15.txt
63.00%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_0.8_mix.txt
80.00%

D:\software_practice\sim\orig.txt
D:\software_practice\sim\orig_rep.txt
73.50%

CosineSimilarity()类和 main()函数在上文都有所解释

计算模块异常处理

  • 个人想不到其他什么异常了,我觉得助教给的这些测试用例已经足够了。

总结

  • 对于这次的项目,面向网络花了好多天的时间,学习到了jieba+向量余弦值的方法来求得相似度。
  • 发现了自己的很多不足,很多要求不知道要怎么去完成。也可以说学习到了很多新的东西,github的各种用法就够我学的。求学之路还是任重而道远

PSP表格

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 60 50
Estimate 估计这个任务需要多少时间 60 60
Development 开发 480 500
Analysis 需求分析(包括学习新技术) 420 480
Design Spec 生成设计文档 60 75
Design Review 设计复审 45 45
Coding Standard 代码规范 (为目前的开发制定合适的规范) 45 40
Design 具体分析 60 50
Coding 具体编码 400 420
Code Review 代码复审 45 45
Test 测试(自我测试,修改代码,提交修改) 180 180
Reporting 报告 90 80
Test Report 测试报告 60 80
Size Measurement 计算工作量 30 45
Postmorten & Process Improvement Plan 事后总结, 并提出过程改进计划 60 60
合计 2095 2210
posted @ 2020-09-16 23:59  九点半  阅读(277)  评论(0)    收藏  举报
AmazingCounters.com