第一次个人编程作业
第一次个人编程作业
这个作业属于哪个课程 | 软件工程 |
---|---|
这个作业要求在哪里 | 作业要求 |
这个作业的目标 | 论文查重个人项目 + 单元测试 + PSP表格 + Git管理 + 性能测试 |
github链接:https://github.com/Haizhaoli/Haizhaoli/tree/master/3118005415
PSP表格(预计花费的时间)
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | |
· Estimate | · 估计这个任务需要多少时间 | 4320 | |
Development | 开发 | 1080 | |
· Analysis | · 需求分析 (包括学习新技术) | 1080 | |
· Design Spec | · 生成设计文档 | 300 | |
· Design Review | · 设计复审 | 180 | |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 300 | |
· Design | · 具体设计 | 300 | |
· Coding | · 具体编码 | 180 | |
· Code Review | · 代码复审 | 180 | |
· Test | · 测试(自我测试,修改代码,提交修改) | 1080 | |
Reporting | 报告 | 90 | |
· Test Repor | · 测试报告 | 90 | |
· Size Measurement | · 计算工作量 | 120 | |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 90 | |
· 合计 | 9420 |
模块接口的设计与实现过程
- 1)自定义余弦相似度函数
def get_cos(x, y):
myx = np.array(x)
myy = np.array(y)
cos1 = np.sum(myy * myx)
cos2 = np.sqrt(sum(myy * myy))
cos3 = np.sqrt(sum(myx * myx))
return cos1 / float(cos2 * cos3)
余弦相似度函数是此程序最为关键的地方,我们将向量A,B扩展到多维,向量A (A1,A2,Ai,····,An,),向量 B(B1,B2,Bi,····,Bn,) 则有:
而我们也可以知道余弦值的范围在[-1,1]之间,值越趋近于1,代表两个向量的方向越接近(文档越相似);接近于0,表示两个向量近乎于正交;越趋近于-1,他们的方向越相反。
-2) 定义不四舍五入保留小数点后两位的方法
def result(num):
num_str = str(num).split('.')
num = float(num_str[0] + '.' + num_str[1][0:2])
return num
-3) 导入论文原文并分词以及计算每个词的词频
try:
with open(sys.argv[1], 'r', encoding='UTF-8') as text1:
data1 = text1.read()
text1.close()
except FileNotFoundError:
msg = "Sorry,the file" + sys.argv[1] + " does not exist."
print(msg)
word1 = jieba.cut(data1)
all_words = {}
for myword in word1:
all_words.setdefault(myword, 0)
all_words[myword] += 1
-4) 导入抄袭版论文并分词以及计算每个词的词频
与导入论文原文实现思路一致
-5) 计算两篇论文的余弦相似度
sample_data = []
compare_data = []
for key in all_words.keys():
sample_data.append(all_words[key])
compare_data.append(compare_word[key])
similarity = get_cos(sample_data, compare_data)
result1 = result(similarity)
with open(sys.argv[3], 'w') as file_object:
file_object.write(str(result1))
file_object.close()
print('两论文的相似度为:')
with open(sys.argv[3]) as file_object:
contents = file_object.read()
print(contents)
file_object.close()
模块接口部分的性能改进
借助pycharm的相关工具我们可以得到程序的代码覆盖率以及相关函数的耗时:
在改进方面,整个程序的核心(余弦相似度算法)目前暂时还没有其他办法更好的优化,也许日后可以把其他部分用新的思路去实现,以缩短整个程序的耗时。
模块部分单元测试展示
整个程序的核心函数是我们的余弦相似度函数,这一单元的代码以及情况如下:
-1) 余弦相似度函数
def get_cos(x, y):
myx = np.array(x)
myy = np.array(y)
cos1 = np.sum(myy * myx)
cos2 = np.sqrt(sum(myy * myy))
cos3 = np.sqrt(sum(myx * myx))
return cos1 / float(cos2 * cos3)
-2) 测试单元代码
import unittest
from main import get_cos, sample_data, compare_data
class MyTestCase(unittest.TestCase):
def test_something(self):
self.assertEqual(get_cos(sample_data, compare_data), False)
if __name__ == '__main__':
unittest.main()
—3) 运行测试单元文件情况如下:
这一结果与我们预期的结果一致即可。
模块部分异常处理说明
在计算部分,程序的异常应该不多,但在文件获取的过程中,文件的路径有可能出错,所以后期在代码中增加了关于FileNotFoundError的异常处理
读取原论文的例子如下
try:
with open(sys.argv[1], 'r', encoding='UTF-8') as text1:
data1 = text1.read()
text1.close()
except FileNotFoundError:
msg = "Sorry,the file" + sys.argv[1] + " does not exist."
print(msg)
输入错误路径后的异常情况也得到了正常处理
PSP表格(实际花费的时间)
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 30 |
· Estimate | · 估计这个任务需要多少时间 | 4320 | 7200 |
Development | 开发 | 1080 | 2160 |
· Analysis | · 需求分析 (包括学习新技术) | 1080 | 1080 |
· Design Spec | · 生成设计文档 | 300 | 300 |
· Design Review | · 设计复审 | 180 | 480 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 300 | 300 |
· Design | · 具体设计 | 300 | 560 |
· Coding | · 具体编码 | 180 | 360 |
· Code Review | · 代码复审 | 180 | 300 |
· Test | · 测试(自我测试,修改代码,提交修改) | 1080 | 1670 |
Reporting | 报告 | 90 | 90 |
· Test Repor | · 测试报告 | 90 | 90 |
· Size Measurement | · 计算工作量 | 120 | 120 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 90 | 90 |
· 合计 | 9420 | 14830 |