GitHub链接
| 这个作业属于哪个课程 | 工程概论 |
|---|---|
| 这个作业要求在哪里 | 个人项目 |
| 这个作业的目标 | 实现论文查重 |
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟 | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 120 | 180 |
| ·Estimate | .估计这个任务需要多少时间 | 200 | 300 |
| Development | 开发 | 30 | 30 |
| .Analysis | .需求分析(包括学习新技术) | 60 | 60 |
| .Design Spec | .生成设计文档 | 10 | 10 |
| .Design Review | .设计复审 | 10 | 10 |
| .Coding Standard | .代码规范(为目前的开发指定合适的规范) | 20 | 20 |
| .Design | .具体设计 | 30 | 50 |
| .Coding | .具体编码 | 60 | 100 |
| .Code Review | .代码复审 | 20 | 30 |
| .Test | .测试(自我测试,修改代码,提交修改) | 10 | 10 |
| Reporting | 报告 | 60 | 60 |
| .Test Report | .测试报告 | 20 | 30 |
| .Size Measurement | .计算工作量 | 10 | 10 |
| .Postmortem & Process Improvement Plan | .事后总结,并提出改进计划 | 10 | 10 |
| .合计 | 670 | 910 |
需求
题目:论文查重
描述如下:
设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率。
原文示例:今天是星期天,天气晴,今天晚上我要去看电影。
抄袭版示例:今天是周天,天气晴朗,我晚上要去看电影。
要求输入输出采用文件输入输出,规范如下:
从命令行参数给出:论文原文的文件的绝对路径。
从命令行参数给出:抄袭版论文的文件的绝对路径。
从命令行参数给出:输出的答案文件的绝对路径。
我们提供一份样例,课堂上下发,上传到班级群,使用方法是:orig.txt是原文,其他orig_add.txt等均为抄袭版论文。
注意:答案文件中输出的答案为浮点型,精确到小数点后两位
代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class PlagiarismChecker {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("请提供正确的命令行参数!");
System.out.println("使用方法:java PlagiarismChecker [原文文件] [抄袭版论文的文件] [答案文件]");
return;
}
String originalFile = args[0];
String plagiarizedFile = args[1];
String answerFile = args[2];
try {
String originalText = readFile(originalFile);
String plagiarizedText = readFile(plagiarizedFile);
double similarity = calculateSimilarity(originalText, plagiarizedText);
writeResult(similarity, answerFile);
System.out.println("查重完成!");
} catch (IOException e) {
System.out.println("发生错误:" + e.getMessage());
}
}
private static String readFile(String filePath) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
}
return sb.toString();
}
private static double calculateSimilarity(String originalText, String plagiarizedText) {
// 在这里实现计算字符串相似度的逻辑,可以使用LCS算法、编辑距离算法或字符串相似度算法
// 这里只是一个示例,使用简单的编辑距离算法来计算相似度
int editDistance = calculateEditDistance(originalText, plagiarizedText);
int maxLength = Math.max(originalText.length(), plagiarizedText.length());
double similarity = 1 - (double) editDistance / maxLength;
return similarity;
}
private static int calculateEditDistance(String originalText, String plagiarizedText) {
int[][] dp = new int[originalText.length() + 1][plagiarizedText.length() + 1];
for (int i = 0; i <= originalText.length(); i++) {
for (int j = 0; j <= plagiarizedText.length(); j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (originalText.charAt(i - 1) == plagiarizedText.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j]));
}
}
}
return dp[originalText.length()][plagiarizedText.length()];
}
private static void writeResult(double similarity, String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(String.format("%.2f", similarity));
}
}
}
结果
先别急