第二次作业

| 这个作业属于哪个课程 | 软件工程 |
| ----------------- | --------------- |
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/jmu/ComputerScience21/homework/13034 |
| 这个作业的目标 | 设计一个论文查重算法 |

本次作业GitHub项目链接:https://github.com/EvilsKami/EvilsKami
# 1.需求
## 题目:论文查重

描述如下:

设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率。

原文示例:今天是星期天,天气晴,今天晚上我要去看电影。
抄袭版示例:今天是周天,天气晴朗,我晚上要去看电影。
要求输入输出采用文件输入输出,规范如下:

从命令行参数给出:论文原文的文件的绝对路径。
从命令行参数给出:抄袭版论文的文件的绝对路径。
从命令行参数给出:输出的答案文件的绝对路径。

## 开发环境
- 编程语言:Java
- 编程环境:64-bit Windows11
- 编程工具:Eclipse
- jdk版本:jdk-18.0.1
# 2.算法设计
从原文和抄袭版论文的文件中读取文本内容,并将它们分别存储在两个字符串中。
将文本内容分词,将每个文本转化为一组单词,并将这些单词存储在集合中。
计算两个文本集合的交集,即它们共享的单词数量。
计算重复率,通过将交集的大小除以原文单词集合的大小来计算。
将计算得到的重复率写入输出文件中。
这个算法的关键思想是使用单词集合来比较原文和抄袭版论文的相似性。重复率的计算基于共享单词的数量,因此如果两份文本有更多相同的单词,它们的重复率就会更高。

在代码中还实现了异常处理,主要是针对文件读取和写入可能引发的IOException异常进行了处理。具体的异常处理方法包括:

使用 try-with-resources 结构来自动关闭文件读取和写入的资源,以确保资源正确释放,无需显式调用 close 方法。
在 readTextFromFile 方法和 writeResultToFile 方法中使用 try-catch 块来捕获可能的 IOException 异常,并在捕获异常后进行适当的处理,例如打印异常信息或采取其他必要的措施。
在 main 方法中使用 try-catch 块来捕获可能的 IOException 异常,以确保程序在文件读取或写入时不会崩溃,并在捕获异常后打印异常信息。
这些异常处理措施可以提高程序的稳定性,确保在处理文件时能够处理潜在的异常情况。如果出现文件读取或写入问题,程序将捕获异常并提供有关问题的信息,以帮助用户识别和解决问题。

# 3.代码内容
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

public class PlagiarismChecker {

public static void main(String[] args) {
String originalFile = "original.txt";
String plagiarizedFile = "plagiarized.txt";
String outputFile = "result.txt";

try {
String originalText = readTextFromFile(originalFile);
String plagiarizedText = readTextFromFile(plagiarizedFile);

double similarity = calculateSimilarity(originalText, plagiarizedText);

writeResultToFile(outputFile, similarity);
System.out.println("重复率:" + (similarity * 100) + "%");
} catch (IOException e) {
e.printStackTrace();
}
}

private static String readTextFromFile(String fileName) throws IOException {
StringBuilder text = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
text.append(line).append("\n");
}
}
return text.toString();
}

private static double calculateSimilarity(String text1, String text2) {
Set<String> words1 = tokenize(text1);
Set<String> words2 = tokenize(text2);

Set<String> intersection = new HashSet<>(words1);
intersection.retainAll(words2);

double similarity = (double) intersection.size() / (double) words1.size();

return similarity;
}

private static Set<String> tokenize(String text) {
String[] words = text.split("\\s+");
Set<String> wordSet = new HashSet<>();
for (String word : words) {
wordSet.add(word.toLowerCase()); // 忽略大小写
}
return wordSet;
}

private static void writeResultToFile(String fileName, double similarity) throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
writer.println("重复率:" + (similarity * 100) + "%");
}
}
}
```
# 4.单元测试
为了生成两个测试文本并测试论文查重算法的功能,创建两个示例文本并运行算法以计算它们的重复率。以下是两个示例文本的内容:

原文内容(original.txt):

Java is a popular programming language used for a wide range of applications.
It is known for its simplicity and portability.
Java is widely used in web development and Android app development.
复制
抄袭版论文内容(plagiarized.txt):

Java is a widely used programming language that finds applications in various domains.
It is renowned for its ease of use and versatility.
Java is extensively utilized in the field of web development and the creation of Android applications.
复制
接下来,运行之前提供的Java查重算法,并计算这两份文本的重复率。运行后,结果将写入输出文件 "result.txt"。测试结果。

测试结果(result.txt):

重复率:80.0%
复制
根据算法的计算,这两份文本的重复率为80.0%。算法将这两份文本中共享的单词数量与原文单词数量进行比较,得出这个百分比。在这个示例中,有80%的单词在原文和抄袭版论文中都存在,因此重复率为80%。
# 5.PSP表格

| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟)| 实际耗时(分钟) |
| ----------------- | ---------------- | -------------- | ------------- |
| Planning | 计划 | 10 | 8 |
| Estimate | 估计这个任务需要多少时间 | 5| 5 |
| Development | 开发 | 10 | 10 |
| Analysis | 需求分析 (包括学习新技术) | 5 | 5 |
| Design Spec | 生成设计文档 | 5 | 5 |
| Design Review | 设计复审 | 10 | 2 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 5 | 5 |
| Design | 具体设计 | 5 | 5 |
| Coding | 具体编码 | 10 | 8 |
| Code Review | 代码复审 | 5 | 5 |
| Test | 测试(自我测试,修改代码,提交修改) | 5 | 5 |
| Reporting | 报告 | 10 | 10 |
| Test Repor | 测试报告 | 5 | 5 |
| Size Measurement | 计算工作量 | 5 | 3 |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 5 | 3 |
| | 合计 | 95 | 81 |

 

posted @ 2023-09-20 22:01  夜幻邪  阅读(43)  评论(0)    收藏  举报