第一次个人编程作业
github地址https://github.com/ElEgent1/ElEgent1/tree/main/202121331083
源码
`
package lianxi;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
public class PlagiarismChecker {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java PlagiarismChecker <OriginalFile> <CopiedFile> <AnswerFile>");
return;
}
String originalFilePath = args[0];
String copiedFilePath = args[1];
String answerFilePath = args[2];
try {
String originalText = readTextFromFile(originalFilePath);
String copiedText = readTextFromFile(copiedFilePath);
double plagiarismRate = calculatePlagiarismRate(originalText, copiedText);
writeResultToFile(answerFilePath, plagiarismRate);
System.out.println("Plagiarism rate: " + plagiarismRate);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String readTextFromFile(String filePath) throws IOException {
StringBuilder text = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
text.append(line).append("\n");
}
}
return text.toString();
}
private static double calculatePlagiarismRate(String originalText, String copiedText) {
Set<String> originalWords = extractWords(originalText);
Set<String> copiedWords = extractWords(copiedText);
if (originalWords.isEmpty() || copiedWords.isEmpty()) {
return 0.0; // 两个集合中有一个为空,返回默认值0.0
}
int commonWordsCount = 0;
for (String word : copiedWords) {
if (originalWords.contains(word)) {
commonWordsCount++;
}
}
// 计算重复率
double plagiarismRate = (double) commonWordsCount / Math.max(originalWords.size(), copiedWords.size());
return plagiarismRate;
}
private static Set<String> extractWords(String text) {
Set<String> words = new HashSet<>();
String[] tokens = text.split("\\s+");
for (String token : tokens) {
// 过滤掉标点符号等非字母字符
String word = token.replaceAll("[^a-zA-Z]", "").toLowerCase();
if (!word.isEmpty()) {
words.add(word);
}
}
return words;
}
private static void writeResultToFile(String filePath, double plagiarismRate) throws IOException {
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(String.format("%.2f", plagiarismRate));
}
}
}
`
异常处理
*命令行参数检查
`
if (args.length != 3) {
System.out.println("Usage: java PlagiarismChecker
return;
}
首先,程序会检查命令行参数的数量是否为3。如果不是,就会输出一条使用说明并且程序返回。 *获取文件路径
String originalFilePath = args[0];
String copiedFilePath = args[1];
String answerFilePath = args[2];
如果命令行参数数量正确,程序会从参数中获取原文文件路径、抄袭版论文文件路径以及答案文件路径 *读取原文和抄袭版论文
String originalText = readTextFromFile(originalFilePath);
String copiedText = readTextFromFile(copiedFilePath);
`
使用readTextFromFile方法分别读取原文和抄袭版论文的内容,并将其存储在originalText和copiedText变量中。
提取输入文本单词模块
private static Set<String> extractWords(String text) { Set<String> words = new HashSet<>(); String[] tokens = text.split("\\s+"); for (String token : tokens) { // 过滤掉标点符号等非字母字符 String word = token.replaceAll("[^a-zA-Z]", "").toLowerCase(); if (!word.isEmpty()) { words.add(word); } } return words; }
创建一个HashSet用于存储不重复的的元素。String[] tokens = text.split("\s+"); 使用正则表达式 \s+ 将文本 text 拆分成单词(以空白字符为分隔符)。拆分后的结果将存储在 tokens 数组中。使用正则表达式将非字母字符替换为空字符串,这样可以去掉标点符号等非字母字符。toLowerCase() 将单词转换为小写字母,以保证大小写不影响最终查重率的计算。
计算模块
`
private static double calculatePlagiarismRate(String originalText, String copiedText) {
Set
Set
if (originalWords.isEmpty() || copiedWords.isEmpty()) {
return 0.0; // 两个集合中有一个为空,返回默认值0.0
}
int commonWordsCount = 0;
for (String word : copiedWords) {
if (originalWords.contains(word)) {
commonWordsCount++;
}
}
// 计算重复率
double plagiarismRate = (double) commonWordsCount / Math.max(originalWords.size(), copiedWords.size());
return plagiarismRate;
}
`
通过将原文和抄袭版文本转换成单词集合,然后统计它们之间相同单词的数量,最后计算查重率。如果其中任意一个文本为空,则返回查重率为 0.0。
测试图例

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