文本查询TextQuery类文件

话不多说,直接上马

TextQuery.h

#pragma once
 
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
 
 
 
class TextQuery
{
public:
    TextQuery(void);
    ~TextQuery(void);
 
    typedef std::vector <std::string>::size_type line_no;
 
    void read_file(std::ifstream &is)
    {
        store_file(is);
        build_map();
 
    }
 
    std::string text_line(line_no) const;
 
    std::set<TextQuery::line_no> run_query(const std::string &query_word) const;
 
private:
    void store_file(std::ifstream &is);
 
    void build_map();
 
    std::vector <std::string> lines_of_text;  //line string of txt
 
    std::map < std::string, std::set<line_no> > word_map;
};

TextQuery.cpp

#include "TextQuery.h"
 
 
TextQuery::TextQuery(void)
{
}
 
 
TextQuery::~TextQuery(void)
{
}
 
 
 
void TextQuery::store_file(std::ifstream &is)
{
    std::string textline;
 
    while(getline(is, textline))
    {
        lines_of_text.push_back(textline);
    }
}
 
 
void TextQuery::build_map()
{
    for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num)
    {
        std::istringstream line(lines_of_text[line_num]);
 
        std::string word;
 
        while(line >> word)
        {
            word_map[word].insert(line_num);
        }
    }
}
 
std::set<TextQuery::line_no> TextQuery::run_query(const std::string &query_word) const
{
    std::map<std::string, std::set<line_no>>::const_iterator loc = word_map.find(query_word);
 
    if(loc == word_map.end())
    {
        return std::set<line_no>();
    }else
    {
        return loc->second;
    }
}
 
std::string TextQuery::text_line(line_no line) const
{
    if(line < lines_of_text.size())
    {
        return lines_of_text[line];
    }else
    {
        throw::std::out_of_range("line number out of range");
    }
}

  

main.cpp

#include <iostream>
#include <algorithm>
 
#include "TextQuery.h"
 
#include <iterator>
 
//using namespace::std;
 
std::string make_plural(std::set<TextQuery::line_no>::size_type sizetemp, std::string str1, std::string str2)
{
    if(sizetemp <= 1)
    {
        return str1;
    }else
    {
        return str1 + str2;
    }
}
 
void print_results(const std::set<TextQuery::line_no> &locs,
                                const std::string &sought, const TextQuery &file)
{
    typedef std::set<TextQuery::line_no> line_nums;
 
    line_nums::size_type size = locs.size();
 
    std::cout << "\n" << sought << " occurs " << size << " " << make_plural(size, "time", "s") << std::endl;
 
    line_nums::const_iterator it = locs.begin();
 
    for(; it != locs.end(); ++it)
    {
        std::cout << "\t(line " << (*it) +1 << ") " << file.text_line(*it) << std::endl;
    }
}
 
std::ifstream& open_file(std::ifstream &in, const std::string &file)
{
    in.close();
    in.clear();
    in.open(file.c_str());
    return in;
}
 
 
 
 
int main(int argc, char* argv[])
{
 
    std::ifstream infile;
 
    if(/*argc < 2 ||*/ !open_file(infile, "D:\\TEXT.txt"))
    {
        std::cerr << "No input file!" << std::endl;
 
        return EXIT_FAILURE;
    }
 
    TextQuery tq;
 
    tq.read_file(infile);
 
    while(true)
    {
        std::cout << "enter word to look for, or q to quit: ";
        std::string s;
        std::cin >> s;
 
        if(!std::cin || s == "q") break;
 
        std::set<TextQuery::line_no> locs = tq.run_query(s);
 
        print_results(locs, s, tq);
    }
     
 
    return 0;
}

  

 

posted on   wu.g.q  阅读(63)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示