目录
Github项目地址
红黑树是一种自平衡的二叉查找树,是一种高效的查找树。
本词频统计器基于红黑树算法,具有以下优点:
提供良好的效率:可在O(logN)时间内完成查找、增加、删除等操作,能保证在最坏情况下,基本的动态几何操作的时间均为O(lgn)。只要求部分地达到平衡要求,降低了对旋转的要求,任何不平衡都会在三次旋转之内解决,从而提高了效率。本词频统计器设计大量增删改查操作,红黑树算法提供了良好的效率支撑。
提供性能下限保证:相比于BST,红黑树可以能确保树的最长路径不大于两倍的最短路径的长度,可见其查找效果的最低保证。最坏的情况下也可以保证O(logN)的复杂度,好于二叉查找树O(N)复杂度。在大数据量情况下,红黑树算法为词频统计器提供良好的性能保证。
提供词频统计的高性能:红黑树的算法时间复杂度和AVL树相同,但统计性能更高。插入 AVL树和红黑树的速度取决于所插入的数据。在数据比较杂乱的情况,则红黑树的统计性能优于AVL树。在词频统计时,数据分布较为杂乱,在此应用场景下,红黑树算法与词频统计器契合。
提供较小的资源开销:与基于哈希的算法相比,基于红黑树方法带来更小的资源开销,程序消耗内存较少。哈希的算法占用大量资源,需要维护大量的计数器,并且在哈希过程中消耗了大量的计算资源。本词频统计器消耗的资源较少。
假定在待排序的记录序列中,存在多个具有相同的关键字的记录,若经过排序,这些记录的相对次序保持不变,则称这种排序算法是稳定的。本词频统计器利用了稳定排序。
本程序代码为如上结构,分为两个部分:
定义:
声明
下列过程中,从上到下为词频统计的实现大致过程:
class Counter
{
private:int Line;
int Ch;
int Words;
string Freq;
string sfn, dfn;
public:Counter(){}
Counter(string sfn,string dfn)
{
this->sfn = sfn;
this->dfn = dfn;
Line = 0;
Ch = 0;
Words = 0;
Freq = "\0";
}
int LineCount();
int CharCount();
int WordCount();
string WordFreq();
void Write();
};
#define Linethreshold 5000
#define Charthreshold 50000
#define Wordthreshold 20000
int DetectFileOpen(ifstream &infile);
int DetectOutfileOpen(ofstream &outfile);
string Conventor(int src);
string Counter::WordFreq()
for (int i = 0; i
for (int i = 0; i> str1[j];
j++;
}
}
for (int i = 0; i
map mymap;
map::iterator it;
for (int i = 0; i ::value_type(str1[i], 1));
}
else
{
mymap[str1[i]]++;
}
}
it = mymap.begin();
string temps = "\0";
stringstream ss;
int i = 0;
WF a[100];
for (i = 0; it != mymap.end(); it++, i++)
{
a[i].key = it->first;
a[i].value = it->second;
}
stable_sort(&a[0], &a[i+1], cmp);
for (j = 0; j> temps;
str[j] = "<" + a[j].key + "" + ">: " + temps;
}
for (i = 0; str[i] != "\0"; i++)
{
if (i >= 10)break;
if(str[i][0]=='<')
result += str[i] + "\n";
else break;
}
//cout <
基于以上要求,设计了以下单元测试:
序号 | 测试用例 | 测试对象 | 测试结果 |
---|---|---|---|
1 | 有空白字符的行 | LineCount() | 通过 |
2 | 存在各种样式的字符 | CharCount() | 通过 |
3 | 数字和单词正确判断 | WordCount() | 少算一个,更改后通过 |
4 | 处理特殊字符 | WordCount() | 通过 |
5 | 处理大写字母 | WordFreq() | 通过 |
6 | 单词种类超过十个 | WordFreq() | 通过 |
7 | 只有字母 | WordCount() | 迭代器崩溃,更改后通过 |
8 | 单词频率是否正确排序 | WordFreq() | 符合字典序 |
9 | 无空行 | LineCount() | 通过 |
10 | 空白文本 | LineCount() | 输出为0,通过 |
以下列出序号1的单元测试代码:
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
// TODO: 在此输入测试代码
Counter C("F:\\软工\\WordCount\\TEST\\1.txt", "F:\\软工\\WordCount\\TEST\\result.txt");
int re = C.LineCount();
Assert::AreEqual(re, 2);
}
};
}
法一
#define Linethreshold 5000
#define Charthreshold 50000
#define Wordthreshold 20000
法二
int DetectFileOpen(ifstream &infile)
{
if (!infile.is_open())
{
cout <<"Cannot open the file, please input right filename!";
system("pause");
return 0;
}
}
int DetectOutfileOpen(ofstream &outfile)
{
if (outfile.is_open())
{
cout <<"Cannot open the file!";
system("pause");
return 0;
}
}
if (argc != 2)
{
cout <<"Uncorrect parameters, Please attach .exe and .txt file in order.";
system("pause");
}
PSP2.1 | header 2 | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 35 | 30 |
· Estimate | ·估计这个任务需要多少时间 | 15 | 5 |
Development | 开发 | 645 | 1220 |
· Analysis | 需求分析(包括学习新技术) | 40 | 50 |
· Design Spec | · 生成设计文档 | 40 | 60 |
· Design Review | · 设计复审 | 10 | 10 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 5 | 10 |
· Design | · 具体设计 | 60 | 120 |
· Coding | · 具体编码 | 240 | 600 |
· Code Review | · 代码复审 | 10 | 10 |
· Test | ·测试(自我测试,修改代码,提交修改) | 240 | 360 |
Reporting | 报告 | 245 | 145 |
· Test Repor | · 测试报告 | 240 | 120 |
· Size Measurement | · 计算工作量 | 5 | 5 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 20 | 20 |
合计 | 935 | 1400 |