C++ 对一段英文进行词频统计
2012-05-14 10:05
891 查看
[cpp] view
plaincopy
/**
* 对一段英文的词频统计
* @Author xxingup@gmail.com
* date 2010/06/17
*/
#include <iostream>
#include <string>
using namespace std;
/**
* 单词对象
*/
struct Word
{
Word() : Str(""), Count(0)
{}
string Str;
int Count;
/**
* 交换单词(用于排序)
* @param word 交换的单词对象
*/
void exchange(Word &word)
{
string tStr = word.Str;
int tCount = word.Count;
word.Str = Str;
word.Count = Count;
Str = tStr;
Count = tCount;
}
};
/**
* 统计词频
* @param words 单词数组
* @param newWord 单词内容
* @param size 单词总数
*/
void CalcCount(Word * words, string &newWord, int size)
{
int i = 0;
for(; i < size; i++)
{
if(words[i].Str == newWord)
{
words[i].Count++;
return;
}
else if(words[i].Str == "")
break;
}
words[i].Str = newWord;
words[i].Count = 1;
}
/**
* 以单词出现频率降序排列单词
* @param words 单词数组
* @param size 单词数量
*/
void SortWordDown(Word * words, int size)
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size-1; j++)
{
if(words[j].Count < words[j+1].Count)
{
words[j].exchange(words[j+1]);
}
}
}
}
int main()
{
Word * words;
string content;
cout << "输入一段英文:";
getline(cin, content);
//计算单词总数
int wCount = 1;
for(unsigned int i = 0; i < content.length(); i++)
{
if(content[i] == ' ')
wCount++;
}
words = new Word[wCount];
string::size_type offset = content.find(' ');//单词都是以空格隔开
while(offset != string::npos)
{
string wStr = content.substr(0, offset);
content.erase(0, offset+1);
CalcCount(words, wStr, wCount);
offset = content.find(' ');
}
CalcCount(words, content, wCount);//计算最后一个单词
SortWordDown(words, wCount);
int printCount = wCount < 5 ? wCount : 5;
cout << "出现频率最高的前" << printCount << "个单词是:" << endl;
for(int i = 0; i < printCount; i++)
{
cout << words[i].Str << "/t频率:" << words[i].Count << "次" << endl;
}
delete [] words;
return 0;
}
效果:
plaincopy
/**
* 对一段英文的词频统计
* @Author xxingup@gmail.com
* date 2010/06/17
*/
#include <iostream>
#include <string>
using namespace std;
/**
* 单词对象
*/
struct Word
{
Word() : Str(""), Count(0)
{}
string Str;
int Count;
/**
* 交换单词(用于排序)
* @param word 交换的单词对象
*/
void exchange(Word &word)
{
string tStr = word.Str;
int tCount = word.Count;
word.Str = Str;
word.Count = Count;
Str = tStr;
Count = tCount;
}
};
/**
* 统计词频
* @param words 单词数组
* @param newWord 单词内容
* @param size 单词总数
*/
void CalcCount(Word * words, string &newWord, int size)
{
int i = 0;
for(; i < size; i++)
{
if(words[i].Str == newWord)
{
words[i].Count++;
return;
}
else if(words[i].Str == "")
break;
}
words[i].Str = newWord;
words[i].Count = 1;
}
/**
* 以单词出现频率降序排列单词
* @param words 单词数组
* @param size 单词数量
*/
void SortWordDown(Word * words, int size)
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size-1; j++)
{
if(words[j].Count < words[j+1].Count)
{
words[j].exchange(words[j+1]);
}
}
}
}
int main()
{
Word * words;
string content;
cout << "输入一段英文:";
getline(cin, content);
//计算单词总数
int wCount = 1;
for(unsigned int i = 0; i < content.length(); i++)
{
if(content[i] == ' ')
wCount++;
}
words = new Word[wCount];
string::size_type offset = content.find(' ');//单词都是以空格隔开
while(offset != string::npos)
{
string wStr = content.substr(0, offset);
content.erase(0, offset+1);
CalcCount(words, wStr, wCount);
offset = content.find(' ');
}
CalcCount(words, content, wCount);//计算最后一个单词
SortWordDown(words, wCount);
int printCount = wCount < 5 ? wCount : 5;
cout << "出现频率最高的前" << printCount << "个单词是:" << endl;
for(int i = 0; i < printCount; i++)
{
cout << words[i].Str << "/t频率:" << words[i].Count << "次" << endl;
}
delete [] words;
return 0;
}
效果:
相关文章推荐
- C++ 对一段英文进行词频统计
- 请编写相关函数来完成对一段英文字符串进行字母统计功能,出现最多那个字母的出现次数,不区分大小写。
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 实验二-2 Eclipse&Hadoop 做英文词频统计进行集群测试
- 用python对一段下载的语料库进行词频统计
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 【CC】Hadoop2.7.1 mapreduce in c++ 词频统计
- 利用Hadoop平台的map-reduce进行词频统计
- python进行中文分词、词性标注、词频统计
- 语料中筛选出英文单词并统计词频,正则切割匹配
- JavaScript中模拟java的map键值统计一段英文中各个单词出现的次数
- [c++]输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。(写的代码有疑惑,求解求教)
- 利用python对一段英文文本进行分词,分句
- 给出一段英文,统计单词出现的次数
- 统计一段文字中数组、中文、英文字母、空格以及其他特殊字符出现的次数
- 英文单词词频统计代码
- 使用ES对中文文章进行分词,并进行词频统计排序
- 【自然语言处理入门】01:利用jieba对数据集进行分词,并统计词频
- 组合数据类型和英文词频统计实例
- 用Python和Shell结合进行词频统计