第一次个人编程作业
1.计算模块接口的设计与实现过程
流程图

实现过程
- (1)先用读入文件,用jieba.analyse.set_stop_words对停用词处理,在对其分词处理。
部分代码如下
def readt(name):
try:
with open(name,'r',encoding='utf-8') as f:
lines=f.read()
except Exception:
with open(name,'r',encoding='gbk') as f:
lines=f.read()
return lines
def word_jieba(name):
jieba.analyse.set_stop_words("stopword.txt")
word=jieba.cut(name)
result=jieba.analyse.extract_tags("".join(word),topK=45)
return result
- (2)将得到的列表,运用jaccard系数的计算方法得到两个文本的相似度。
def jaccard(name1,name2):
word1=word_jieba(readt(name1))
word2=word_jieba(readt(name2))
len_mixed=len(list(set(word1).intersection(set(word2))))
len_union=len(list(set(word1).union(set(word2))))
if len_union!=0:
sim=float(len_mixed)/len_union
return sim
else :
return 0
- (3) 给定两个集合A,B,Jaccard 系数定义为A与B交集的大小与A与B并集的大小的比值。

2.计算模块接口部分的性能测试
性能分析使用的是python自带的profile,得到下图


3.计算模块部分单元测试展示
部分测试代码
import unittest
import main
import sys
class MyTest(unittest.TestCase):
def test_add(self):
print("orig_0.8_add.txt 相似度")
sim1 = main.jaccard("orig.txt","orig_0.8_add.txt")
sim = ("%.2f" % sim1)
print(sim)
def test_del(self):
print("orig_0.8_del.txt 相似度")
sim1 = main.jaccard("orig.txt", "orig_0.8_add.txt")
sim = ("%.2f" % sim1)
print(sim)

4.计算模块部分异常处理说明
- (1)读取异常
try:
with open(name,'r',encoding='utf-8') as f:
lines=f.read()
except Exception:
with open(name,'r',encoding='gbk') as f:
lines=f.read()
- (2)jaccard分母数为0则会出现异常
if len_union!=0:
sim=float(len_mixed)/len_union
return sim
else :
return 0
5.PSP表格
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 60 | 70 |
| Estimate | 估计这个任务需要多少时间 | 60 | 90 |
| Development | 开发 | 350 | 500 |
| Analysis | 需求分析 (包括学习新技术) | 300 | 260 |
| Design Spec | 生成设计文档 | 50 | 70 |
| Design Review | 设计复审 | 30 | 40 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 20 |
| Design | 具体设计 | 40 | 60 |
| Coding | 具体编码 | 300 | 400 |
| Code Review | 代码复审 | 30 | 40 |
| Test | 测试(自我测试,修改代码,提交修改) | 200 | 600 |
| Reporting | 报告 | 100 | 120 |
| Test Repor | 测试报告 | 40 | 60 |
| Size Measurement | 计算工作量 | 20 | 30 |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 60 | 60 |
| 合计 | 1660 | 2420 |
6.总结
在这次作业中,很多东西都是第一次去接触的,花费了大量时间去百度,去下载,去使用。在弄这测试,博客,github,由于接触不多,不大熟悉,还是磨蹭了很久。收获还是很大的。

浙公网安备 33010602011771号