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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| #ifndef TEXT_H #define TEXT_H
#include <iostream> #include <string> #include <vector> #include <map> #include <set> #include <iterator> #include <memory> #include <fstream> #include <sstream> #include <algorithm>
class QueryResult { friend std::ostream& print(std::ostream&,const QueryResult&); public: using line_no = std::vector<std::string>::size_type; using line_it = std::set<line_no>::const_iterator; QueryResult(std::string s, std::shared_ptr<std::set<line_no>> p, std::shared_ptr<std::vector<std::string>> f): sought(s),lines(p),file(f) {} std::set<line_no>::size_type size() const {return lines->size();} line_it begin() const { return lines->begin(); } line_it end() const { return lines->end(); }
std::shared_ptr<std::vector<std::string>> get_file() { return file; } private: std::string sought; std::shared_ptr<std::set<line_no>> lines; std::shared_ptr<std::vector<std::string>> file; };
class TextQuery { public: using line_no = std::vector<std::string>::size_type; TextQuery(std::ifstream&); QueryResult query(const std::string&) const; private: std::shared_ptr<std::vector<std::string>> file; std::map<std::string,std::shared_ptr<std::set<line_no>>> wm; };
TextQuery::TextQuery(std::ifstream& is):file(new std::vector<std::string>) { std::string text; while (getline(is,text)) { file->push_back(text); int n = file->size() - 1; std::istringstream line(text); std::string word; while (line >> word) { auto& lines = wm[word]; if (!lines) lines.reset(new std::set<line_no>); lines->insert(n); } } }
QueryResult TextQuery::query(const std::string& sought) const { static std::shared_ptr<std::set<line_no>> nodata = std::make_shared<std::set<line_no>>(); auto loc = wm.find(sought); if (loc == wm.end()) return QueryResult(sought,nodata,file); else return QueryResult(sought,loc->second,file); }
std::ostream& print(std::ostream& os,const QueryResult& res) { os << res.sought << " occurs " << res.lines->size() << " \n"; for (auto i: *res.lines) os << " line" << i+1 << ": " << *(res.file->begin() + i) << "\n"; return os; }
class Query_base { friend class Query; public: using line_no = TextQuery::line_no; virtual ~Query_base() {} private: virtual QueryResult eval(const TextQuery&) const =0; virtual std::string rep() const =0; };
class Query { friend Query operator~(const Query&); friend Query operator&(const Query&,const Query&); friend Query operator|(const Query&,const Query&); public: Query(const std::string&); QueryResult eval(const TextQuery& t) const { return q->eval(t); } std::string rep() const { return q->rep(); } private: Query(std::shared_ptr<Query_base> query):q(query) {} std::shared_ptr<Query_base> q; };
std::ostream& operator<<(std::ostream& os,const Query& query) { return os<<query.rep(); }
class WordQuery:public Query_base { friend class Query; WordQuery(const std::string& s):query_word(s) {}
QueryResult eval(const TextQuery& t) const { return t.query(query_word); } std::string rep() const { return query_word; }
std::string query_word; };
inline Query::Query(const std::string& s):q(new WordQuery(s)) {}
class NotQuery:public Query_base { friend Query operator~(const Query&); NotQuery(const Query& q):query(q) {}
QueryResult eval(const TextQuery& t) const; std::string rep() const { return "~("+query.rep()+")";} Query query; }; inline Query operator~(const Query& operand) { return std::shared_ptr<Query_base>(new NotQuery(operand)); }
class BinaryQuery:public Query_base { protected: BinaryQuery(const Query& l,const Query& r,std::string s): lhs(l),rhs(r),opSym(s) {} std::string rep() const { return "("+lhs.rep()+" "+opSym+" "+rhs.rep()+")"; } Query lhs,rhs; std::string opSym; };
class AndQuery:public BinaryQuery { friend Query operator&(const Query&,const Query&); AndQuery(const Query& left,const Query& right):BinaryQuery(left,right,"&") {} QueryResult eval(const TextQuery&) const; }; inline Query operator&(const Query& left,const Query& right) { return std::shared_ptr<Query_base>(new AndQuery(left,right)); } class OrQuery:public BinaryQuery { friend Query operator|(const Query&,const Query&); OrQuery(const Query& left,const Query& right):BinaryQuery(left,right,"|") {} QueryResult eval(const TextQuery&) const; }; inline Query operator|(const Query& left,const Query& right) { return std::shared_ptr<Query_base>(new OrQuery(left,right)); }
QueryResult OrQuery::eval(const TextQuery& text) const { auto right = rhs.eval(text),left = lhs.eval(text); auto ret_lines = std::make_shared<std::set<line_no>> (left.begin(),left.end()); ret_lines->insert(right.begin(),right.end()); return QueryResult(rep(),ret_lines,left.get_file()); } QueryResult AndQuery::eval(const TextQuery& text) const { auto left=lhs.eval(text),right=rhs.eval(text); auto ret_lines = std::make_shared<std::set<line_no>>(); set_intersection(left.begin(),left.end(),right.begin(),right.end(),inserter(*ret_lines,ret_lines->begin())); return QueryResult(rep(),ret_lines,left.get_file()); } QueryResult NotQuery::eval(const TextQuery& text) const { auto result = query.eval(text); auto ret_lines = std::make_shared<std::set<line_no>>(); auto beg = result.begin(),end=result.end(); auto sz = result.get_file()->size(); for (size_t n=0; n!=sz; ++n) { if (beg==end || *beg!=n) ret_lines->insert(n); else ++beg; } return QueryResult(rep(),ret_lines,result.get_file()); } #endif
|