1、github链接:https://github.com/BStory28/BStory28/tree/main/202121331008/202121331008/text
2、PSP表格
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
60 |
85 |
· Estimate |
· 估计这个任务需要多少时间 |
180 |
210 |
Development |
开发 |
50 |
45 |
· Analysis |
· 需求分析 (包括学习新技术) |
30 |
20 |
· Design Spec |
· 生成设计文档 |
20 |
10 |
· Design Review |
· 设计复审 |
20 |
10 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
45 |
50 |
· Design |
· 具体设计 |
30 |
30 |
· Coding |
· 具体编码 |
30 |
25 |
· Code Review |
· 代码复审 |
25 |
20 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
30 |
28 |
Reporting |
报告 |
45 |
30 |
· Test Repor |
· 测试报告 |
20 |
10 |
· Size Measurement |
· 计算工作量 |
345 |
278 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
做作业别玩手机 |
好好做,多查文献 |
|
· 合计 |
|
|
3、代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String file1 = "orig.txt";
String file2 = "orig_add.txt";
String resultFile = "result.txt";
try {
double similarity = calculateCosineSimilarity(file1, file2);
writeResultToFile(resultFile, similarity);
System.out.println("重复率计算完成!");
} catch (IOException e) {
System.out.println("计算重复率时出现错误:" + e.getMessage());
}
}
public static double calculateCosineSimilarity(String file1, String file2) throws IOException {
// 读取文件内容
String content1 = readFileContent(file1);
String content2 = readFileContent(file2);
// 分割字符串为单词
String[] words1 = content1.split("\\s+");
String[] words2 = content2.split("\\s+");
// 计算词频向量
int[] vector1 = calculateWordFrequency(words1);
int[] vector2 = calculateWordFrequency(words2);
// 计算余弦相似度
double dotProduct = calculateDotProduct(vector1, vector2);
double magnitude1 = calculateMagnitude(vector1);
double magnitude2 = calculateMagnitude(vector2);
return dotProduct / (magnitude1 * magnitude2);
}
private static String readFileContent(String file) throws IOException {
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append(" ");
}
reader.close();
return content.toString();
}
private static int[] calculateWordFrequency(String[] words) {
int[] frequency = new int[words.length];
for (int i = 0; i < words.length; i++) {
frequency[i] = 1;
for (int j = i + 1; j < words.length; j++) {
if (words[i].equals(words[j])) {
frequency[i]++;
words[j] = "";
}
}
}
return frequency;
}
private static double calculateDotProduct(int[] vector1, int[] vector2) {
double dotProduct = 0;
for (int i = 0; i < vector1.length; i++) {
dotProduct += vector1[i] * vector2[i];
}
return dotProduct;
}
private static double calculateMagnitude(int[] vector) {
double magnitude = 0;
for (int value : vector) {
magnitude += Math.pow(value, 2);
}
return Math.sqrt(magnitude);
}
private static void writeResultToFile(String file, double similarity) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("重复率: " + similarity);
writer.close();
}
}
4、结果