这个作业属于哪个课程 | 软件工程 |
---|---|
这个作业要求在哪里 | 作业要求 |
这个作业的目标 | 论文查重 |
PSP表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 40 | 30 |
Estimate | 估计这个任务需要多少时间 | 10 | 10 |
Development | 开发 | 300 | 300 |
Analysis | 需求分析 (包括学习新技术) | 120 | 180 |
Design Spec | 生成设计文档 | 20 | 20 |
Design Review | 设计复审 | 20 | 30 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 10 | 10 |
Design | 具体设计 | 40 | 3 |
Coding | 具体编码 | 240 | 240 |
Code Review | 代码复审 | 60 | 30 |
Test | 测试(自我测试,修改代码,提交修改) | 60 | 50 |
Reporting | 报告 | 90 | 120 |
Test Repor | 测试报告 | 20 | 20 |
Size Measurement | 计算工作量 | 20 | 10 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 60 | 70 |
合计 | 1110 | 1150 |
前言
一开始我以为查重需要匹配基于web的数据库,或是自行建库,没想到本次作业直接降低一个维度,只需要在本地文件中提取txt文档,然后用一个简单的算法查重,作业重点在于编程环境的运行,所以难度不大
编程接口
本人用的是pycharm,但是文本查重的重点在于匹配
jieba
这就涉及到分词的算法,这里我推荐结巴分词,具体是什么,我想dddd,这里也就不多赘述
如果环境和我一样,那这里推荐pip安装
1. pip install jieba
gensim
还有gensim需要安装,这是一个多功能的包,包括dictionary.doc2bow和similarities.Similarity,可以有效解决论文查重的大部分问题
简介在此
1. pip install gensim
代码解析
similarities
通过余弦来计算其相似度
1. def calc_similarity(text1,text2):
2. corpus=convert_corpus(text1,text2)
3. similarity = gensim.similarities.Similarity('-Similarity-index', corpus, num_features=len(dictionary))
4. test_corpus_1 = dictionary.doc2bow(text1)
5. cosine_sim = similarity[test_corpus_1][1]
6. return cosine_sim
re.match
由于对比对象为中文或英文单词,因此应该对读取到的文件数据中存在的换行符\n、标点符号过滤掉,这里选择用正则表达式来匹配符合的数据
1. def filter(str):
2. str = jieba.lcut(str)
3. result = []
4. for tags in str:
5. if (re.match(u"[a-zA-Z0-9\u4e00-\u9fa5]", tags)):
6. result.append(tags)
7. else:
8. pass
9. return result
dictionary
Doc2Bow是gensim中封装的一个方法,主要用于实现Bow模型。
1. text1='John likes to watch movies. Mary likes too.'
2. text2='John also likes to watch football games.'
测试结果
在txt_code中输入
1. path1 = "D:/Personal project/txt/orig.txt" #论文原文的文件的绝对路径(作业要求)
2. path2 = "D:/Personal project/txt/orig_0.8_add.txt" #抄袭版论文的文件的绝对路径
然后得出相似度结果
接口的性能改进
通过Run --> Profile ‘xxxx’:来进行时间消耗的分析
关注到filter函数:由于cut和lcut暂时找不到可提到的其他方法(jieba库已经算很强大了),暂时没办法进行改进,因此考虑对正则表达式匹配改进
这里是先用lcut处理后再进行匹配过滤,这样做显得过于臃肿,可以考虑先匹配过滤之后再用lcut来处理
1. def filter(string):
2. pattern = re.compile(u"[^a-zA-Z0-9\u4e00-\u9fa5]")
3. string = pattern.sub("", string)
4. result = jieba.lcut(string)
5. return result
进行对比后,明显发现速度提升了许多
单元测试
重新设计源代码,新建txt_new_check
然后新建单元测试文件unit_test.py
可以发现预测值为0.99正确:
代码覆盖率
运行过程中异常处理
其实大部分unresolved reference之类的问题可以通过pip的使用解决
附上教程
在读取指定文件路径时,如果文件路径不存在,程序将会出现异常,因此可以在读取指定文件内容之前先判断文件是否存在,若不存在则做出响应并且结束程序。
这里引入os.path.exists()方法用于检验文件是否存在:
1. def main_test():
2. path1 = input("输入论文原文的文件的绝对路径:")
3. path2 = input("输入抄袭版论文的文件的绝对路径:")
4. if not os.path.exists(path1) :
5. print("论文原文文件不存在!")
6. exit()
7. if not os.path.exists(path2):
8. print("抄袭版论文文件不存在!")
9. exit()