您的位置:首页 > 编程语言 > C语言/C++

菜鸟学习c++—map使用练习 实现词频统计程序

2015-07-11 09:51 1741 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ map 词频分析器