菜鸟学习c++—map使用练习 实现词频统计程序
来源:互联网 发布:京东淘宝比价软件 编辑:程序博客网 时间:2024/06/08 17:58
c++练习,使用map实现词频统计程序。
程序实现两个功能:(1)统计文本中各个单词的出现次数 (2)查找指定输入单词在文本中是否出现
需注意:练习比较随意,多使用全局变量,正式使用形参实参传递参数比较好。
#include <iostream>
#include <fstream>#include <vector>
#include <map>
using namespace std;
ifstream is;
vector<string> text;
map<string, size_t> word_count;
int main()
{
string line;
char choice;
void create_vector_map(string line); /* 完成分词工作,将文本变为单个单词存入map函数 */
int search(string word); /* 查询功能实现函数 */
void open_file(ifstream &is);
void print_vector_map();
void result_1();
open_file(is); /* 打开文本 */
create_vector_map(line);
cout<<" 1.show passage"<<endl;
cout<<" ----------------"<<endl;
cout<<" 2.search word "<<endl;
cout<<endl;
cout<<"enter your option:"<<endl;
cin>>choice;
if(choice!='1' && choice!='2')
cout<<"error!"<<endl;
else
switch (choice) {
case '1':
print_vector_map();
break;
case '2':
result_1();
break;
}
}
void create_vector_map(string line)
{
while(is)
{
string unit;
while(getline(is,line))
{
auto beg=line.begin();
auto end=line.end();
auto star=beg;
text.push_back(line);
while(beg!=end+1)
{
while(*beg>='a'&&*beg<='z')
{
++beg;
}
unit.assign(star,beg);
++beg;
star=beg;
++word_count[unit];
}
}
}
cout<<"finish create task"<<endl;
}
int search(string word)
{
int clu;
auto beg=word_count.begin();
auto end=word_count.end();
for (const auto &w : word_count)
{
if (w.first!=word && beg!=end)
{
beg++;
}
}
if(beg==end)
clu=0;
else
clu=1;
return clu;
}
void open_file(ifstream &is)
{
is.open("output.txt"); /* 在project文件夹中建立output.txt存放分析文本*/
if(is)
cout<<"file open success!"<<endl;
else
cout<<"file open faild!"<<endl;
}
void print_vector_map()
{
auto beg=text.begin();
auto end=text.end();
while(beg!=end)
{
cout<<*beg++<<endl;
}
for (const auto &w : word_count)
cout<<w.first<<" occurs "<<w.second<<((w.second>1)?" times":" time")<<endl;
}
void result_1()
{
string word;
int clu;
cout<<"please enter word you want to find(press p to exit):"<<endl;
while(1)
{
cin>>word;
if(word=="p")
break;
clu=search(word);
if(clu==1)
cout<<word<<" exit in output.txt"<<endl;
else
cout<<word<<" not exit in output.txt"<<endl;
}
cout<<"exit success"<<endl;
}
0 0
- 菜鸟学习c++—map使用练习 实现词频统计程序
- 词频统计的C++实现(使用stl--map)
- java实现词频统计程序
- C语言实现 词频统计
- 词频统计程序(C语言/C++)
- Java实现的一个词频统计程序
- Java实现的一个词频统计程序
- C语言实现英文文本词频统计
- 单词词频统计程序(map set 应用)
- 使用Python+NLTK实现英文单词词频统计
- 使用Python+NLTK实现英文单词词频统计
- C++STL--map和set词频统计和单词转换
- 使用python的map和reduce统计词频
- c语言 词频统计
- 词频统计Map-Reduce过程
- Golang实现词频统计
- MapReduce实现词频统计
- Golang实现词频统计
- SolrCloud分布式检索时忽略宕机的Shard
- Android通过软引用实现图片缓存,防止内存溢出
- VmWare安装时出错
- 回归 regression
- VAssistX使用
- 菜鸟学习c++—map使用练习 实现词频统计程序
- Android Listview(列表显示)性能优化的相关专利方法及系统
- Android:复制eclipse下的Android项目,成为一个新项目
- VS集成Qt环境搭建
- struct、union、enum and sizeof
- 霓歌即时通讯中的相关专利整理(二)
- dwr整合ssh的xml配置方式
- boost::property_tree::write_xml写中文乱码问题
- 检测http中的REFERER信息,实现 防盗链