1.作业地址
Charles_valuntin / 3121005173 · GitCode
2.PSP表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
Planning | 计划 | 20 | 30 |
· Estimate | · 估计这个任务需要多少时间 | 60 | 80 |
Development | 开发 | 240 | 300 |
· Analysis | · 需求分析 (包括学习新技术) | 60 | 90 |
· Design Spec | · 生成设计文档 | 30 | 50 |
· Design Review | · 设计复审 | 20 | 40 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 15 | 30 |
· Design | · 具体设计 | 50 | 60 |
· Coding | · 具体编码 | 250 | 300 |
· Code Review | · 代码复审 | 40 | 60 |
· Test | · 测试(自我测试,修改代码,提交修改) | 40 | 120 |
Reporting | 报告 | 120 | 150 |
· Test Repor | · 测试报告 | 60 | 60 |
· Size Measurement | · 计算工作量 | 20 | 25 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 30 |
| · 合计 | 1055 | 1425 |
3. 模块接口的设计与实现过程
3.1 共5个类8个方法
类 | 方法 |
MainPaperCheck(查重主程序类) | main方法 |
MyException(异常处理类) | cast(异常抛出方法) |
HammingUtils(海明距离模块工具类) | getHammingDistance(计算海明距离) |
getSimilarity(输出相似度) | |
SimHashUtils(SimHash模块工具类) | getHash(计算哈希值) |
getSimHash(计算simHash值) | |
TxtIOUtils(txt文件读写工具类) | readTxt(读取txt文本内容) |
writeTxt(向txt文件输出文本) |
3.2 类,方法之间的关系
主程序调用文件读写工具对文本路径中的文件进行读取;
对读取的文件类型进行判断,非txt文件将抛出异常;
对文件文本内容进行字数判断,少于300字抛出异常;
使用SimHash模块工具类分别对两个文本获取simHash值;
使用海明距离模块工具类计算两个文本的海明距离,在根据海明距离计算相似度;
对结果进行小数点后保留两位处理;
结果输出。
3.3模块具体实现
3.3.1读写 txt 文件的模块
类:TxtIOUtils
包含了两个静态方法:
1、readTxt:读取txt文件
2、writeTxt:写入txt文件
实现:都是调用 Java.io 包提供的接口
3.3.2 SimHash 模块(核心模块)
类:SimHashUtils
包含了两个静态方法:
1、getHash:传入String,计算出它的hash值,并以字符串形式输出,(使用了MD5获得hash值)
2、getSimHash:传入String,计算出它的simHash值,并以字符串形式输出,(需要调用 getHash 方法)
getSimHash 是核心算法,主要流程如下:
1、分词(使用了外部依赖 hankcs 包提供的接口)
List<String> keywordList = HanLP.extractKeyword(str, str.length());//取出所有关键词
获取 hash 值
String keywordHash = getHash(keyword);
if (keywordHash.length() < 128) {
// hash值可能少于128位,在低位以0补齐
int dif = 128 - keywordHash.length();
for (int j = 0; j < dif; j++) {
keywordHash += "0";
}
}
加权、合并
for (int j = 0; j < v.length; j++) {
// 对keywordHash的每一位与'1'进行比较
if (keywordHash.charAt(j) == '1') {
//权重分10级,由词频从高到低,取权重10~0
v[j] += (10 - (i / (size / 10)));
} else {
v[j] -= (10 - (i / (size / 10)));
}
}
降维
String simHash = "";// 储存返回的simHash值
for (int j = 0; j < v.length; j++) {
// 从高位遍历到低位
if (v[j] <= 0) {
simHash += "0";
} else {
simHash += "1";
}
}
3.3.3 海明距离模块
类:HammingUtils
包含了两个静态方法:
getHammingDistance:输入两个 simHash 值,计算出它们的海明距离 distance
for (int i = 0; i < simHash1.length(); i++) {
// 每一位进行比较
if (simHash1.charAt(i) != simHash2.charAt(i)) {
distance++;
}
}
getSimilarity:输入两个 simHash 值,调用 getHammingDistance 方法得出海明距离 distance,在由 distance 计算出相似度。
return 0.01 * (100 - distance * 100 / 128);
4.接口部分的性能改进
4.1性能分析
Overview
![](https://img-blog.csdnimg.cn/img_convert/424a68fbbd6085aba54e932de69feed9.png)
方法的调用情况
![](https://img-blog.csdnimg.cn/img_convert/f05d429129cde0e0b0293b3b9dc72fab.png)
从分析图可以看到:
调用次数最多的是com.hankcs.hanlp包提供的接口, 即分词、取关键词与计算词频花费了最多的时间。
所以在性能上基本没有什么需要改进的。
5.单元测试
5.1 读写 txt 文件的模块 的测试
基本思路:
1、测试正常读取
2、测试正常写入
3、测试错误读取
4、测试错误写入
public class TxtIOUtilsTest {
@Test
public void readTxtTest() {
// 路径存在,正常读取
String str = TxtIOUtils.readTxt("D:/test/orig.txt");
String[] strings = str.split(" ");
for (String string : strings) {
System.out.println(string);
}
}
@Test
public void writeTxtTest() {
// 路径存在,正常写入
double[] elem = {0.11, 0.22, 0.33, 0.44, 0.55};
for (int i = 0; i < elem.length; i++) {
TxtIOUtils.writeTxt(elem[i], "D:/test/ans.txt");
}
}
@Test
public void readTxtFailTest() {
// 路径不存在,读取失败
String str = TxtIOUtils.readTxt("D:/test/none.txt");
}
@Test
public void writeTxtFailTest() {
// 路径错误,写入失败
double[] elem = {0.11, 0.22, 0.33, 0.44, 0.55};
for (int i = 0; i < elem.length; i++) {
TxtIOUtils.writeTxt(elem[i], "User:/test/ans.txt");
}
}
}
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/1e1f56839ca300c204b41bfb3b93e2e3.png)
代码覆盖率:
![](https://img-blog.csdnimg.cn/img_convert/8681f6cd18e64b21b117f3dc6965a2ab.png)
5.2 SimHash 模块 的测试
public class SimHashUtilsTest {
@Test
public void getHashTest(){
String[] strings = {"余华", "是", "一位", "真正", "的", "作家"};
for (String string : strings) {
String stringHash = SimHashUtils.getHash(string);
System.out.println(stringHash.length());
System.out.println(stringHash);
}
}
@Test
public void getSimHashTest(){
String str0 = TxtIOUtils.readTxt("D:/test/orig.txt");
String str1 = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
System.out.println(SimHashUtils.getSimHash(str0));
System.out.println(SimHashUtils.getSimHash(str1));
}
}
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/7ec97ef021d825620110141ce9618e44.png)
代码覆盖率:
![](https://img-blog.csdnimg.cn/img_convert/85527814b96d5b7ad996c99110c65072.png)
5.3、海明距离模块 的测试
部分代码:
public class HammingUtilsTest {
@Test
public void getHammingDistanceTest() {
String str0 = TxtIOUtils.readTxt("D:/test/orig.txt");
String str1 = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
int distance = HammingUtils.getHammingDistance(SimHashUtils.getSimHash(str0), SimHashUtils.getSimHash(str1));
System.out.println("海明距离:" + distance);
System.out.println("相似度: " + (100 - distance * 100 / 128) + "%");
}
}
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/47fefc853c482c612dcaedeebc0f2091.png)
代码覆盖率:
![](https://img-blog.csdnimg.cn/img_convert/eae632e02bd437a3a8e153991af48a5b.png)
5.4、主测试 MainTest
部分测试代码:
public class MainTest {
@Test
public void origAndAllTest(){
String[] str = new String[6];
str[0] = TxtIOUtils.readTxt("D:/test/orig.txt");
str[1] = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
str[2] = TxtIOUtils.readTxt("D:/test/orig_0.8_del.txt");
str[3] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_1.txt");
str[4] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_10.txt");
str[5] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_15.txt");
String ansFileName = "D:/test/ansAll.txt";
for(int i = 0; i <= 5; i++){
double ans = HammingUtils.getSimilarity(SimHashUtils.getSimHash(str[0]), SimHashUtils.getSimHash(str[i]));
TxtIOUtils.writeTxt(ans, ansFileName);
}
}
}
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/e8c359d339667330e1a31e318b6490a5.png)
结果文件:
![](https://img-blog.csdnimg.cn/img_convert/c961bc00269c9d632d2ede4feee74f27.png)
![](https://img-blog.csdnimg.cn/img_convert/e99b204ff956a9ffe1b57b91aecb041f.png)
6、异常处理
6.1、设计与实现
当文本长度太短时,HanLp无法取得关键字,需要抛出异常。
try{
if(str.length() < 200) throw new ShortStringException("文本过短!");
}catch (ShortStringException e){
e.printStackTrace();
return null;
}
实现了一个处理这个异常的类:ShortStringException(继承了Exception)
public ShortStringException(String message) {
super(message);
}
6.2、测试
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/e5f940a43c68acb104d486b39ef82ea8.png)