软件工程第二次作业
| 所属课程 | https://edu.cnblogs.com/campus/gdgy/2023softwareengine?filter=all&page=2 |
|---|---|
| 作业要求 | https://edu.cnblogs.com/campus/gdgy/2023softwareengine/homework/12915 |
| 作业目的 | 文章查重 |
使用的接口
jieba.cut
可以对中文句子进行分词
re.match
可以过滤文本中的标点符号和换行符
gensim.dictionary.doc2bow
对过滤和分词得到的单词进行封装得到一个词典
gensim.similarities.Similarity
计算相似度
代码
点击查看代码
import jieba
import gensim
import re
#获取指定路径的文件内容
def get_file_contents(path):
str = ''
f = open(path, 'r', encoding='UTF-8')
line = f.readline()
while line:
str = str + line
line = f.readline()
f.close()
return str
#将读取到的文件内容先进行jieba分词,然后再把标点符号、转义符号等特殊符号过滤掉
def filter(str):
str = jieba.lcut(str)
result = []
for tags in str:
if (re.match(u"[a-zA-Z0-9\u4e00-\u9fa5]", tags)):
result.append(tags)
else:
pass
return result
#传入过滤之后的数据,通过调用gensim.similarities.Similarity计算余弦相似度
def calc_similarity(text1,text2):
texts=[text1,text2]
dictionary = gensim.corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
similarity = gensim.similarities.Similarity('-Similarity-index', corpus, num_features=len(dictionary))
test_corpus_1 = dictionary.doc2bow(text1)
cosine_sim = similarity[test_corpus_1][1]
return cosine_sim
if __name__ == '__main__':
path1 = "D:\1\wenben\orig.txt" #论文原文的文件的绝对路径(作业要求)
path2 = "D:\1\wenben\orig_0.8_add.txt" #抄袭版论文的文件的绝对路径
save_path = "D:\1\wenben\shuchu" #输出结果绝对路径
str1 = get_file_contents(path1)
str2 = get_file_contents(path2)
text1 = filter(str1)
text2 = filter(str2)
similarity = calc_similarity(text1, text2)
print("文章相似度: %.4f"%similarity)
#将相似度结果写入指定文件
f = open(save_path, 'w', encoding="utf-8")
f.write("文章相似度: %.4f"%similarity)
f.close()
PSP表格
| PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 30 | 90 |
| Estimate | 估计这个任务需要多少时间 | 300 | 360 |
| Development | 开发 | 540 | 600 |
| Analysis | 需求分析 (包括学习新技术) | 540 | 600 |
| Design Spec | 生成设计文档 | 20 | 10 |
| Design Review | 设计复审 | 20 | 10 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 5 |
| Design | 具体设计 | 10 | 5 |
| Coding | 具体编码 | 360 | 360 |
| Code Review | 代码复审 | 20 | 5 |
| Test | 测试(自我测试,修改代码,提交修改) | 60 | 20 |
| Reporting | 报告 | 30 | 15 |
| Test Repor | 测试报告 | 20 | 10 |
| Size Measurement | 计算工作量 | 5 | 5 |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 5 | 5 |

浙公网安备 33010602011771号