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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#ifndef MYSEARCH_H #define MYSEARCH_H
#include <memory> #include <string> #include <map> #include <vector> #include <set> #include <fstream> #include <iostream> #include <sstream>
#include "mysearch.h"
class QueryResult; class TextQuery { friend class QueryResult; public: TextQuery(std::ifstream &infile); QueryResult query(std::string s) const; private: std::shared_ptr<std::vector<std::string>> ifile; //指向存放每行文本的vector std::map<std::string, std::shared_ptr<std::set<int>>> wm; //单词到存放行数的set的映射 };
class QueryResult { friend std::ostream &operator<<(std::ostream &os, const QueryResult &qr); public: QueryResult(std::string s, std::shared_ptr<std::set<int>> lp, std::shared_ptr<std::vector<std::string>> fp) : world(s), linep(lp), file(fp){} std::set<int>::iterator begin() { return linep -> begin(); } std::set<int>::iterator end() { return linep -> end(); } std::shared_ptr<std::vector<std::string>> &get_file() { return file; } private: std::string world; std::shared_ptr<std::set<int>> linep; std::shared_ptr<std::vector<std::string>> file; };
//构造函数,在构造函数里储存将单词到行号的隐射 TextQuery::TextQuery(std::ifstream &infile) : ifile(new std::vector<std::string>) //初始化infile { std::string s,world; int num(0); while(getline(infile, s)) { ++num; ifile -> push_back(s); std::istringstream line(s); while(line >> world) { if(!wm[world]) //如果set还没有创建 wm[world].reset(new std::set<int>); //创建一个set wm[world] -> insert(num); //行号插入到set } } } //查找 //返回一个QueryResult类 QueryResult TextQuery::query(std::string s) const { std::shared_ptr<std::set<int>> nodata(new std::set<int>); //一个空set auto res = wm.find(s); //查找函数的本体,查找map对应字符串的行号set if (res == wm.end()) //无内容返回空set return QueryResult(s, nodata, ifile); else //有内容返回set,访问map 的 second成员 return QueryResult(s, res -> second, ifile); }
//用<<操作符打印QueryResult的内容 std::ostream &operator<<(std::ostream &os, const QueryResult &qr) { os << qr.world << ": n"; for (auto num:*qr.linep) //对每个指针解引用然后遍历 { os << num << " "; //行号 os << *(qr.file -> begin() + num - 1) << "n"; //用迭代器输出vector的行文本 } return os; }
//一个抽象基类,继承查询类型 class Query_base { friend class Query; protected: virtual ~Query_base() = default; private: virtual QueryResult eval(const TextQuery &t) const = 0; virtual std::string rep() const = 0; };
class Query { friend Query operator~(const Query &q); friend Query operator&(const Query &q1, const Query &q2); friend Query operator|(const Query &q1, const Query &q2); public: //构造一个WordQuery Query(const std::string &s); QueryResult eval(const TextQuery &t) const { return q -> eval(t); //虚调用 } std::string rep() const { return q -> rep(); //虚调用 } private: Query(std::shared_ptr<Query_base> sq) : q(sq) {} std::shared_ptr<Query_base> q; };
//继承查询基类 class WordQuery : public Query_base { //默认都是私有成员,由友元Query(用户借口类)访问 friend class Query; //保存要查询的词 WordQuery(const std::string &s) : query_world(s) {} QueryResult eval(const TextQuery &t) const override { return t.query(query_world); } std::string rep() const override {return query_world;} std::string query_world; //查询的词 };
inline Query::Query(const std::string &s) : q(new WordQuery(s)) {}
class NotQuery : public Query_base { friend Query operator~(const Query &q); NotQuery(const Query &q) : query(q) {}; std::string rep() const override { //输出要查询的词,附带~符号和括号 return "~(" + query.rep() + ")"; } QueryResult eval(const TextQuery &t) const override; Query query; //保存传递的query参数 };
inline Query operator~(const Query &q) {//返回一个shared_ptr return std::shared_ptr<Query_base> (new NotQuery(q)); }
class BinaryQuery : public Query_base { protected: BinaryQuery(const Query &l, const Query &r, std::string s) : lq(l), rq(r), ops(s) {} std::string rep() const override { //输出查找的词和符号 return "(" + lq.rep() + " " + ops + " " + rq.rep() + ")"; } //没有覆盖eval(),仍是抽象基类 Query lq, rq; //左右操作数 std::string ops; //运算符 };
//&运算和|运算相似 class AndQuery : public BinaryQuery { friend Query operator&(const Query &q1, const Query &q2); AndQuery(const Query &q1, const Query &a2) : BinaryQuery(q1, a2, "&") {} QueryResult eval(const TextQuery &t) const; };
inline Query operator&(const Query &q1,const Query &q2) { return std::shared_ptr<Query_base> (new AndQuery(q1, q2)); }
class OrQuery : public BinaryQuery { friend Query operator|(const Query &q1, const Query &q2); OrQuery(const Query &q1, const Query &a2) : BinaryQuery(q1, a2, "|") {} QueryResult eval(const TextQuery &t) const; };
inline Query operator|(const Query &q1,const Query &q2) { return std::shared_ptr<Query_base> (new OrQuery(q1, q2)); }
// '|' 操作 QueryResult OrQuery::eval(const TextQuery &t) const { QueryResult lqr = lq.eval(t), rqr = rq.eval(t); //左QueryResult的行号or上右QueryResult行号 auto ret_lines = std::make_shared<std::set<int>>(lqr.begin(), lqr.end()); ret_lines -> insert(rqr.begin(), rqr.end()); //初始化一个新QueryResult return QueryResult(rep(), ret_lines, lqr.get_file()); }
// '&'操作 QueryResult AndQuery::eval(const TextQuery &t) const { QueryResult lqr = lq.eval(t), rqr = rq.eval(t); //一个指向空set的指针 auto ret_lines = std::make_shared<std::set<int>>(); ret_lines -> insert(rqr.begin(), rqr.end()); //使用标准库算法求并集 set_intersection(lqr.begin(), lqr.end(), rqr.begin(), rqr.end(), inserter(*ret_lines, ret_lines -> begin())); return QueryResult(rep(), ret_lines, lqr.get_file()); }
// '~'操作 QueryResult NotQuery::eval(const TextQuery &t) const { QueryResult qr = query.eval(t); //一个指向空set的指针 auto ret_lines = std::make_shared<std::set<int>>(); auto qrb = qr.begin(), qre = qr.end(); auto sz = qr.get_file() -> size(); for (auto n = 0; n != sz; ++n) { if (*qrb != n + 1) //如果行号不在查询结果里面,插入到set中 ret_lines -> insert(n+1); else if(qrb != qre) ++qrb; } return QueryResult(rep(), ret_lines, qr.get_file()); }
#endif
//主函数调用 int main () { ifstream infile("text.txt"); TextQuery tq(infile); auto res = Query("harry") & Query("my") | ~Query("haha"); cout << res.eval(tq); /* ((harry & my) | ~(haha)): ... ... .. */ }
|
近期评论