forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextquery.cpp
89 lines (74 loc) · 1.71 KB
/
textquery.cpp
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
#include "textquery.h"
#include<fstream>
using std::ifstream;
#include <sstream>
using std::istringstream;
#include<memory>
using std::shared_ptr; using std::make_shared;
#include<vector>
using std::vector;
#include<string>
using std::string;
#include<map>
using std::map;
#include<set>
using std::set;
#include "queryresult.h"
TextQuery::TextQuery(ifstream& is) :file(new vector<string>)
{
using std::getline;
string text;
while(getline(is,text))
{
file->push_back(text);
int n = file->size() - 1;
istringstream line(text);
string word;
while(line >> word)
{
auto p = handlePunct(word);
for (auto w : *p)
{
auto &lines = wm[w];
if (!lines)
lines.reset(new set<line_no>);
lines->insert(n);
}
}
}
}
QueryResult TextQuery::query(const string &sought) const
{
static shared_ptr<set<line_no>> nodata(new set<line_no>);
//! fetch the iterator to the matching element in the map<word, lines>.
//std::map<std::string, std::shared_ptr<std::set<line_no>>>::const_iterator
auto loc = wm.find(sought);
if(loc == wm.end())
return QueryResult(sought, nodata, file);
else
return QueryResult(sought, loc->second, file);
}
shared_ptr<vector<string>> TextQuery::handlePunct(const string &s)
{
shared_ptr<vector<string>> p =
make_shared<vector<string>>();
size_t first = 0, index = 0;
while(index != s.size())
{
if (ispunct(s[index]))
{
string word = s.substr(first, index - first);
if (!word.empty())
p->push_back(word);
p->push_back(s.substr(index, 1));
++index;
first = index;
}
else
++index;
}
string trail = s.substr(first);
if (!trail.empty())
p->push_back(trail);
return p;
}