-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextQuery.cpp
More file actions
87 lines (79 loc) · 2.82 KB
/
TextQuery.cpp
File metadata and controls
87 lines (79 loc) · 2.82 KB
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
// Arbel Nathan 308366749
#include "TextQuery.h"
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#include <regex>
#include <iterator>
using namespace std;
// read the input file and build the map of lines to line numbers
TextQuery::TextQuery(ifstream &is) : file(new vector<string>)
{
string text;
// regex words_regex("[\\w']+");
while (getline(is, text))
{ // for each line in the file
file->push_back(text); // remember this line of text
int n = file->size() - 1; // the current line number
//////////////////////////////////////////////////////////////////////////
text= regex_replace(text, regex("([^'\\w])"), " ");
istringstream line(text); // separate the line into words
string word;
while (line >> word)
{ // for each word in that line
// cout<< word<<endl;
//////////////////////////////////////////////////////////////////////////
// if word isn't already in wm, subscripting adds a new entry
auto &lines = wm[word]; // lines is a shared_ptr
if (!lines) // that pointer is null the first time we see word
lines.reset(new set<line_no>); // allocate a new set
lines->insert(n); // insert this line number
}
}
}
QueryResult TextQuery::query(const string &sought) const
{
// we'll return a pointer to this set if we don't find sought
static shared_ptr<set<line_no>> nodata(new set<line_no>);
// we use find instead of subscript, to avoid adding words to wm
auto loc = wm.find(sought);
if (loc == wm.end())
return QueryResult(sought, nodata, file); // not found
else
return QueryResult(sought, loc->second, file);
}
// debugging routine
void TextQuery::display_map()
{
auto iter = wm.cbegin(), iter_end = wm.cend();
for ( ; iter != iter_end; ++iter) {
cout << iter->first << ": {";
// fetch location vector as a const reference to avoid copying it
auto text_locs = iter->second;
auto loc_iter = text_locs->cbegin(),
loc_iter_end = text_locs->cend();
// print all line numbers for this word
while (loc_iter != loc_iter_end)
{
cout << *loc_iter + 1;
if (++loc_iter != loc_iter_end)
cout << ", ";
}
cout << "}\n"; // end list of output this word
}
cout << endl; // finished printing entire map
}
std::ostream &print(std::ostream &os, const QueryResult &qr)
{
os << "\"" << qr.sought << "\"" << " occours " <<
qr.lines->size() << " times:" <<std::endl;
for (auto num : *qr.lines)
{
os << "\t(line " << num + 1 << ") "
<< *(qr.file->begin() + num) << std::endl;
}
return os;
}