个人项目:论文查重

软件工程< 网工19级(3)班 >
作业要求    <作业要求>
作业目标 设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率。

 

 

 

 

 

 

 

 

 

个人项目GitHub

一、PSP表格

SP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划 40 40
· Estimate · 估计这个任务需要多少时间 40 40
Development 开发 1060 1360
· Analysis · 需求分析 (包括学习新技术) 300 420
· Design Spec · 生成设计文档 25 20
· Design Review · 设计复审 15 20
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 20 10
· Design · 具体设计 90 120
· Coding · 具体编码 480 600
· Code Review · 代码复审 30 50
· Test · 测试(自我测试,修改代码,提交修改) 100 120
Reporting 报告 100 130
· Test Repor · 测试报告 40 60
· Size Measurement · 计算工作量 20 10
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 40 60
  · 合计 1200 1530

二、计算模块接口的设计和实现过程

 1.开发环境
  • 编程语言:Java 14

  • IDE:Intellij IDEA 2020.1

  • 项目构建工具:maven

  • 单元测试:JUnit-4.12

  • 性能分析工具:JProfiler 9.2

  • 依赖的外部 jar 包:汉语言处理包

 
1 <dependency>
2     <groupId>com.hankcs</groupId>
3     <artifactId>hanlp</artifactId>
4     <version>portable-1.5.4</version>
5 </dependency>

 2.整体流程

 

3.类

  • MainPaperCheck:main 方法所在的类
  • HammingUtils:计算海明距离的类
  • SimHashUtils:计算 SimHash 值的类
  • TxtIOUtils:读写 txt 文件的工具类
  • ShortStringException:处理文本内容过短的异常类

4.核心算法

  • simhash+海明距离

 

 

 

5.接口的设计和实现
(1)读写 txt 文件的模块
         类:TxtIOUtils

    包含了两个静态方法:

  •  readTxt:读取txt文件
  •  writeTxt:写入txt文件

    实现:都是调用 Java.io 包提供的接口,比较简单,这里省略。

(2)SimHash 模块(核心模块)
         类:SimHashUtils

     包含了两个静态方法:

  •   getHash:传入String,计算出它的hash值,并以字符串形式输出,(使用了MD5获得hash值)
  •   getSimHash:传入String,计算出它的simHash值,并以字符串形式输出,(需要调用 getHash 方法)

     getSimHash 是核心算法,主要流程如下:

  •   分词(使用了外部依赖 hankcs 包提供的接口)
1 List<String> keywordList = HanLP.extractKeyword(str, str.length());//取出所有关键词

 

  •  获取 hash 值
1 String keywordHash = getHash(keyword);
2            if (keywordHash.length() < 128) {
3                // hash值可能少于128位,在低位以0补齐
4                int dif = 128 - keywordHash.length();
5                for (int j = 0; j < dif; j++) {
6                    keywordHash += "0";
7                }
8            }
  •  加权、合并
1 for (int j = 0; j < v.length; j++) {
2              // 对keywordHash的每一位与'1'进行比较
3              if (keywordHash.charAt(j) == '1') {
4                  //权重分10级,由词频从高到低,取权重10~0
5                  v[j] += (10 - (i / (size / 10)));
6              } else {
7                  v[j] -= (10 - (i / (size / 10)));
8              }
9          }
  •  降维
1 String simHash = "";// 储存返回的simHash值
2         for (int j = 0; j < v.length; j++) {
3             // 从高位遍历到低位
4             if (v[j] <= 0) {
5                 simHash += "0";
6             } else {
7                 simHash += "1";
8             }
9         }

6.海明距离模块

  • 类:HammingUtils

包含了两个静态方法:

  •  getHammingDistance:输入两个 simHash 值,计算出它们的海明距离 distance
1 for (int i = 0; i < simHash1.length(); i++) {
2                 // 每一位进行比较
3                 if (simHash1.charAt(i) != simHash2.charAt(i)) {
4                     distance++;
5                 }
6             }

 

  •  getSimilarity:输入两个 simHash 值,调用 getHammingDistance 方法得出海明距离 distance,在由 distance 计算出相似度。

 

7.main 主模块

(1)main 方法的主要流程:

  • 从命令行输入的路径名读取对应的文件,将文件的内容转化为对应的字符串
  • 由字符串得出对应的 simHash值
  • 由 simHash值求出相似度
  • 把相似度写入最后的结果文件中
  • 退出程序

 

三、接口部分的性能改进

1.性能分析

  • Overview

     

  •  方法的调用情况

 

  • 从分析图可以看到:

调用次数最多的是com.hankcs.hanlp包提供的接口, 即分词、取关键词与计算词频花费了最多的时间。

所以在性能上基本没有什么需要改进的。

四、单元测试

1.读写 txt 文件的模块 的测试

  • 基本思路:

    (1)测试正常读取

    (2)测试正常写入

    (3)测试错误读取

    (4)测试错误写入

 1 public class TxtIOUtilsTest {
 2     @Test
 3     public void readTxtTest() {
 4         // 路径存在,正常读取
 5         String str = TxtIOUtils.readTxt("D:/test/orig.txt");
 6         String[] strings = str.split(" ");
 7         for (String string : strings) {
 8             System.out.println(string);
 9         }
10     }
11     @Test
12     public void writeTxtTest() {
13         // 路径存在,正常写入
14         double[] elem = {0.11, 0.22, 0.33, 0.44, 0.55};
15         for (int i = 0; i < elem.length; i++) {
16             TxtIOUtils.writeTxt(elem[i], "D:/test/ans.txt");
17         }
18     }
19     @Test
20     public void readTxtFailTest() {
21         // 路径不存在,读取失败
22         String str = TxtIOUtils.readTxt("D:/test/none.txt");
23     }
24     @Test
25     public void writeTxtFailTest() {
26         // 路径错误,写入失败
27         double[] elem = {0.11, 0.22, 0.33, 0.44, 0.55};
28         for (int i = 0; i < elem.length; i++) {
29             TxtIOUtils.writeTxt(elem[i], "User:/test/ans.txt");
30         }
31     }
32 }
  • 测试结果:

  •  代码覆盖率:

 

2.SimHash 模块 的测试

 1 public class SimHashUtilsTest {
 2     @Test
 3     public void getHashTest(){
 4         String[] strings = {"余华", "", "一位", "真正", "", "作家"};
 5         for (String string : strings) {
 6             String stringHash = SimHashUtils.getHash(string);
 7             System.out.println(stringHash.length());
 8             System.out.println(stringHash);
 9         }
10     }
11     @Test
12     public void getSimHashTest(){
13         String str0 = TxtIOUtils.readTxt("D:/test/orig.txt");
14         String str1 = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
15         System.out.println(SimHashUtils.getSimHash(str0));
16         System.out.println(SimHashUtils.getSimHash(str1));
17     }
18 }
  • 测试结果:

 

  • 代码覆盖率:

(SimHashUtils 的 getHash 方法的异常 catch 测试不了)

 

3.海明距离模块 的测试

  • 部分代码:
 1 public class HammingUtilsTest {
 2     @Test
 3     public void getHammingDistanceTest() {
 4         String str0 = TxtIOUtils.readTxt("D:/test/orig.txt");
 5         String str1 = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
 6         int distance = HammingUtils.getHammingDistance(SimHashUtils.getSimHash(str0), SimHashUtils.getSimHash(str1));
 7         System.out.println("海明距离:" + distance);
 8         System.out.println("相似度: " + (100 - distance * 100 / 128) + "%");
 9     }
10 }
  •  测试结果:

 

  •  代码覆盖率:

 

4.主测试 MainTest

  • 部分测试代码:

 

 1 public class MainTest {
 2     @Test
 3     public void origAndAllTest(){
 4         String[] str = new String[6];
 5         str[0] = TxtIOUtils.readTxt("D:/test/orig.txt");
 6         str[1] = TxtIOUtils.readTxt("D:/test/orig_0.8_add.txt");
 7         str[2] = TxtIOUtils.readTxt("D:/test/orig_0.8_del.txt");
 8         str[3] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_1.txt");
 9         str[4] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_10.txt");
10         str[5] = TxtIOUtils.readTxt("D:/test/orig_0.8_dis_15.txt");
11         String ansFileName = "D:/test/ansAll.txt";
12         for(int i = 0; i <= 5; i++){
13             double ans = HammingUtils.getSimilarity(SimHashUtils.getSimHash(str[0]), SimHashUtils.getSimHash(str[i]));
14             TxtIOUtils.writeTxt(ans, ansFileName);
15         }
16     }
17 }

 

  •  测试结果:

 

  •  结果文件:

      

 

 

五、异常处理

1.设计与实现

当文本长度太短时,HanLp无法取得关键字,需要抛出异常。

1   try{
2             if(str.length() < 200) throw new ShortStringException("文本过短!");
3         }catch (ShortStringException e){
4             e.printStackTrace();
5             return null;
6         }

 实现了一个处理这个异常的类:ShortStringException(继承了Exception)

1 public ShortStringException(String message) {
2         super(message);
3     }

   2.测试

  • 测试结果:

 

 

 

从分析图可以看到:
调用次数最多的是com.hankcs.hanlp包提供的接口, 即分词、取关键词与计算词频花费了最多的时间。
所以在性能上基本没有什么需要改进的。

 

 

 

 

 

 

 

 

 

posted @ 2021-09-19 16:48  夏yiii  阅读(128)  评论(0)    收藏  举报