菜鸟学习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
原创粉丝点击