GitHub连接
作业要求
| 这个作业属于哪个课程 | 工程概论 |
|---|---|
| 这个作业要求在哪里 | 作业要求 |
| 这个作业的目标 | 编写论文查重代码 熟悉GitHub |
需求
题目:论文查重
设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率。
- 原文示例:今天是星期天,天气晴,今天晚上我要去看电影。
- 抄袭版示例:今天是周天,天气晴朗,我晚上要去看电影。
要求输入输出采用文件输入输出,规范如下:
- 从命令行参数给出:论文原文的文件的绝对路径。
- 从命令行参数给出:抄袭版论文的文件的绝对路径。
- 从命令行参数给出:输出的答案文件的绝对路径。
我们提供一份样例,课堂上下发,上传到班级群,使用方法是:orig.txt是原文,其他orig_add.txt等均为抄袭版论文。
注意:答案文件中输出的答案为浮点型,精确到小数点后两位
PSP表格记录
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 20 | |
| Estimate | 估计这个任务需要多少时间 | 300 | |
| Development | 开发 | 60 | |
| Analysis | 需求分析 (包括学习新技术) | 60 | |
| Design Spec | 生成设计文档 | 20 | |
| Design Review | 设计复审 | 60 | |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 30 | |
| Design | 具体设计 | 120 | |
| Coding | 具体编码 | 120 | |
| Code Review | 代码复审 | 30 | |
| Test | 测试(自我测试,修改代码,提交修改) | 10 | |
| Reporting | 报告 | 30 | |
| Test Repor | 测试报告 | 20 | |
| Size Measurement | 计算工作量 | 30 | |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | |
| 合计 | 940 |
异常处理
当读取原文文件或抄袭版论文文件时发生错误,例如文件路径错误、文件不存在或无法读取文件时,就会在catch块中打印错误消息。
点击查看代码
try {
String originalContent = readFile(originalFilePath);
String plagiarizedContent = readFile(plagiarizedFilePath);
double plagiarismRate = calculatePlagiarismRate(originalContent, plagiarizedContent);
writeOutput(outputFilePath, plagiarismRate);
System.out.println("查重完成,结果已写入输出文件。");
} catch (IOException e) {
System.out.println("发生错误:" + e.getMessage());
}
测试结果


完整代码
点击查看代码
package Checker;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;
public class PaperPlagiarismChecker {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("请提供原文文件路径、抄袭版论文文件路径和输出答案文件路径作为命令行参数。");
return;
}
String originalFilePath = args[0];
String plagiarizedFilePath = args[1];
String outputFilePath = args[2];
try {
String originalContent = readFile(originalFilePath);
String plagiarizedContent = readFile(plagiarizedFilePath);
double plagiarismRate = calculatePlagiarismRate(originalContent, plagiarizedContent);
writeOutput(outputFilePath, plagiarismRate);
System.out.println("查重完成,结果已写入输出文件。");
} catch (IOException e) {
System.out.println("发生错误:" + e.getMessage());
}
}
private static String readFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
}
return content.toString();
}
private static double calculatePlagiarismRate(String originalContent, String plagiarizedContent) {
String[] originalWords = originalContent.split("\\s+");
String[] plagiarizedWords = plagiarizedContent.split("\\s+");
Set<String> originalWordSet = new HashSet<>();
for (String word : originalWords) {
originalWordSet.add(word);
}
int commonWordCount = 0;
for (String word : plagiarizedWords) {
if (originalWordSet.contains(word)) {
commonWordCount++;
}
}
double plagiarismRate = (double) commonWordCount / originalWords.length * 100;
return roundToTwoDecimalPlaces(plagiarismRate);
}
private static double roundToTwoDecimalPlaces(double plagiarismRate) {
return 0;
}
private static void writeOutput(String filePath, double plagiarismRate) throws IOException {
DecimalFormat decimalFormat = new DecimalFormat("0.00");
String formattedPlagiarismRate = decimalFormat.format(plagiarismRate);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write("重复率:" + formattedPlagiarismRate + "%");
}
}
}