第二次作业
| 这个作业属于哪个课程 | 课程链接 |
|---|---|
| 这个作业要求在哪里 | 作业要求 |
| 这个作业的目标 | 实现个人项目 |
github链接
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 30 | 40 |
| · Estimate | · 估计这个任务需要多少时间 | 30 | 40 |
| Development | 开发 | 390 | 420 |
| · Analysis | · 需求分析 (包括学习新技术) | 60 | 70 |
| · Design Spec | · 生成设计文档 | 30 | 30 |
| · Design Review | · 设计复审 | 30 | 30 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
| · Design | · 具体设计 | 60 | 70 |
| · Coding | · 具体编码 | 120 | 130 |
| · Code Review | · 代码复审 | 30 | 30 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 30 | 30 |
| Reporting | 报告 | 90 | 100 |
| · Test Repor | · 测试报告 | 30 | 30 |
| · Size Measurement | · 计算工作量 | 30 | 30 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 40 |
| · 合计 | 510 | 560 |
计算模块接口的设计与实现过程
代码组织
类:可以设计一个PlagiarismChecker类,用于处理论文查重的主要逻辑。
函数:- readFile(const std::string& filePath):从文件中读取内容。
- calculateSimilarity(const std::string& original, const std::string& plagiarized):计算两篇论文的相似度。
- writeResult(const std::string& resultPath, double similarity):将相似度结果写入文件。
函数关系
PlagiarismChecker类包含上述三个函数,main函数调用PlagiarismChecker类的成员函数完成整个查重流程。
算法关键
使用余弦相似度算法计算两篇论文的相似度。具体步骤如下:
- 将论文文本进行分词处理。
- 构建词频向量。
- 计算两个词频向量的余弦相似度。
独到之处
采用余弦相似度算法,该算法在文本相似度计算中具有较高的准确性。
对文本进行分词处理,考虑了词语的语义信息,而不仅仅是字符匹配。
性能分析

改进思路
- 优化分词算法,减少分词时间。
- 采用更高效的数据结构存储词频向量,如哈希表。
- 避免不必要的重复计算。
部分单元测试展示
#include "PlagiarismChecker.h"
TEST(PlagiarismCheckerTest, ReadFile) {
PlagiarismChecker checker;
std::string filePath = "test.txt";
std::ofstream file(filePath);
file << "Test text";
file.close();
std::string content = checker.readFile(filePath);
EXPECT_EQ(content, "Test text");
}
TEST(PlagiarismCheckerTest, CalculateSimilarity) {
PlagiarismChecker checker;
std::string original = "Test text";
std::string plagiarized = "Test text";
double similarity = checker.calculateSimilarity(original, plagiarized);
EXPECT_EQ(similarity, 1.0);
}
TEST(PlagiarismCheckerTest, WriteResult) {
PlagiarismChecker checker;
std::string resultPath = "result.txt";
double similarity = 0.8;
checker.writeResult(resultPath, similarity);
std::ifstream file(resultPath);
std::string content;
file >> content;
EXPECT_EQ(content, "0.80");
}
// 其他测试用例...
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
测试函数
readFile:测试文件读取功能。
calculateSimilarity:测试相似度计算功能。
writeResult:测试结果写入功能。
构造测试数据的思路
对于readFile,创建一个临时文件并写入测试内容,然后读取该文件并验证内容是否一致。
对于calculateSimilarity,构造相同和不同的文本进行测试。
对于writeResult,写入一个测试相似度值,然后读取文件验证结果是否正确
异常处理
文件打开失败:当尝试打开文件时,如果文件不存在或没有权限访问,抛出std::runtime_error异常。
命令行参数错误:当命令行参数数量不正确时,输出错误信息并退出程序。
单元测试样例
TEST(PlagiarismCheckerTest, ReadFileError) {
PlagiarismChecker checker;
std::string filePath = "nonexistent.txt";
EXPECT_THROW(checker.readFile(filePath), std::runtime_error);
}
上述测试用例模拟了文件不存在的场景,当尝试读取不存在的文件时,readFile函数会抛出std::runtime_error异常。

浙公网安备 33010602011771号