标题:怎么做带逻辑符的文本搜索呢?求指教
取消只看楼主
hwz91
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2010-5-24
结帖率:100%
 问题点数:0 回复次数:0 
怎么做带逻辑符的文本搜索呢?求指教
如要实现
   输入 hello || hi ,则显示有hello或者hi的行
   输入!hello则显示没有hello的行

在程序哪里改呢,新手求教,谢谢

#pragma warning(disable: 4786)

#include <string>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Text {

public:
    typedef  string::size_type str_size;
    typedef  vector<string>::size_type line_no;
   
    void read_file(ifstream &is)
    {
        store_file(is);
        build_map();
    }
    set<line_no> run_(const string&) const;
    string text_line(line_no) const;
    str_size size() const { return lines_of_text.size(); }
    void show_map();
   
private:
    void store_file(ifstream&);
    void build_map();
    vector<string> lines_of_text;
    map< string, set<line_no> > word_map;   
    static string cleanup_str(const string&);
};

string Text::text_line(line_no line) const
{
    if (line < lines_of_text.size())
        return lines_of_text[line];
    throw out_of_range("line number out of range");
}

void Text::store_file(ifstream &is)
{
    string textline;
    while (getline(is, textline))
        lines_of_text.push_back(textline);
}

void Text::build_map()
{
    for (line_no line_num = 0;
    line_num != lines_of_text.size();
    ++line_num)
    {
        istringstream line(lines_of_text[line_num]);
        string word;
        while (line >> word)
            word_map[cleanup_str(word)].insert(line_num);
    }
}

set<Text::line_no>

Text::run_(const string &_word) const
{
    map<string, set<line_no> >::const_iterator
        loc = word_map.find(cleanup_str(_word));
    if (loc == word_map.end())
        return set<line_no>();  
    else
        return loc->second;
}

void Text::show_map()
{
    map< string, set<line_no> >::iterator iter = word_map.begin(),
        iter_end = word_map.end();
    for ( ; iter != iter_end; ++iter)
    {
        cout << "word: " << iter->first << " {";
        const set<line_no> &text_locs = iter->second;
        set<line_no>::const_iterator loc_iter = text_locs.begin(),
            loc_iter_end = text_locs.end();
        while (loc_iter != loc_iter_end)
        {
            cout << *loc_iter;
            
            if (++loc_iter != loc_iter_end)
                cout << ", ";
        }
        cout << "}\n";
    }
    cout << endl;
}


string Text::cleanup_str(const string &word)
{
    string ret;
    for (string::const_iterator it = word.begin(); it != word.end(); ++it) {
        if (!ispunct(*it))
            ret += tolower(*it);
    }
    return ret;
}


void print_results(const set<Text::line_no>& locs, const string& sought, const Text &file)
{
    typedef set<Text::line_no> line_nums;
    line_nums::size_type size = locs.size();
    cout << "\n" << sought << " 出现了 "
        << size << " "
        <<  "次" << endl;
    line_nums::const_iterator it = locs.begin();
    for ( ; it != locs.end(); ++it) {
        cout << "\t(在第 "
            << (*it) + 1 << " 行) "
            << file.text_line(*it) << endl;
    }
}


ifstream& open_file(ifstream &in, const string &file)
{
    in.close();
    in.clear();
    in.open(file.c_str());
    return in;
}


 int main()
{
   
    ifstream in;
    string s;
    //cout << "请输入文本路径:\n";
    //cin >> s;
    if (!open_file(in, "c:\\1.txt"))
    {
        cout << "打开失败,检查重试!" << endl;
        system("pause");
        return 0;
    }
   
    Text tq;
    tq.read_file(in);
   
    while (true)
    {
        cout << "\t你要找的是\n   ( exit! 退出程序 ): ";
        cin >> s;
        if (!cin || s == "exit!") break;
        set<Text::line_no> locs = tq.run_(s);
        print_results(locs, s, tq);
        cout << "\n";
        system("pause");
        system("cls");
    }
    return 1;
}
搜索更多相关主题的帖子: hello 
2011-05-17 11:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-340146-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015642 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved