-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryHandler.cpp
285 lines (208 loc) · 7.95 KB
/
QueryHandler.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "QueryHandler.h"
#include "porter2_stemmer.h"
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
/*
Public interface method for query handling
@param query - the list of strings in the user's query input (includes AND, OR, NOT)
@param indInput - a pointer to the index to search
*/
void QueryHandler::handleQuery(vector<string> query, IndexInterface* indInput){
ind = indInput;
doSearch(query);
}
/*
Searches the index for a series of words in 'words'
@param words - the list of words input by the user in the query (includes AND, OR, NOT)
*/
void QueryHandler::doSearch(vector<string> words){
//Initialize temp vars for documents which are 'allowed' and 'disallowed'
vector<string> allow;
vector<string> disallow;
//Boolean to check if current token is first token
bool first = true;
//Index of the words vector which is currently being checked
int argIndex = 1;
if(words.at(0) == "AND")
//Case 1: AND operator
{
//For each operand which is not 'NOT'
while(argIndex < words.size() && words.at(argIndex) != "NOT"){
//Save all the documents which contain the new word being checked
vector<string> otherSet = ind->getDocumentsForWord(words.at(argIndex));
//If this is the first argument, don't perform set intersection because first set is empty
if(first){
first = false;
allow = otherSet;
argIndex++;
continue;
} else {
//Map of key-value pairs keyed by sum of indeces in the original vectors returned by the index in TF/IDF order
//This allows for equal treatment of both parts of the AND clause
map<int, string> finalSet;
//Perform a set intersection, allowing only documents which contain both words
for(int i = 0; i < allow.size(); i++){
string currentDocName = allow.at(i);
vector<string>::iterator docInSecondList = find(otherSet.begin(), otherSet.end(), currentDocName);
if(docInSecondList != otherSet.end()){
//Key for the map is the sum of the two doc's indeces in their vectors
int indexSum = i + (docInSecondList - otherSet.begin());
pair<int, string> newMapItem;
newMapItem.first = indexSum;
newMapItem.second = currentDocName;
finalSet.insert(newMapItem);
}
}
//Empty the allow vector
allow.clear();
//Re-fill the allow vector by ascending order of sum of original vector indeces, so the
//documents with the highest average TF/IDF value for the two sets come first
for(map<int, string>::iterator it = finalSet.begin(); it != finalSet.end(); ++it)
allow.push_back(it->second);
}
argIndex++;
}
} else if(words.at(0) == "OR")
//Case 2: OR operator
{
//For each operand which is not 'NOT'
while(argIndex < words.size() && words.at(argIndex) != "NOT"){
//Save all the documents which contain the new word being checked
vector<string> otherSet = ind->getDocumentsForWord(words.at(argIndex));
//If this is the first argument, don't perform set union because first set is empty
if(first){
first = false;
allow = otherSet;
argIndex++;
continue;
} else {
//Map of key-value pairs keyed by sum of indeces in the original vectors returned by the index in TF/IDF order
//This allows for equal treatment of both parts of the OR clause
map<int, string> finalSet;
//Perform a set union, allowing only documents which contain both words
for(int i = 0; i < allow.size(); i++){
string currentDocName = allow.at(i);
pair<int, string> newMapItem;
newMapItem.first = i;
newMapItem.second = currentDocName;
finalSet.insert(newMapItem);
}
for(int i = 0; i < otherSet.size(); i++){
string currentDocName = otherSet.at(i);
pair<int, string> newMapItem;
newMapItem.first = i;
newMapItem.second = currentDocName;
finalSet.insert(newMapItem);
}
//Empty the allow vector
allow.clear();
//Re-fill the allow vector by ascending order of sum of original vector indeces, so the
//documents with the highest average TF/IDF value for the two sets come first
for(map<int, string>::iterator it = finalSet.begin(); it != finalSet.end(); ++it)
allow.push_back(it->second);
}
argIndex++;
}
} else
//Case 3: No operator
{
//Allow any document which matches the first word given
allow = ind->getDocumentsForWord(words.at(0));
}
argIndex++;
first = true;
//For the remaining items, perform the 'NOT' (set difference) operation
while(argIndex < words.size()){
//Find all the documents which contain the disallowed word
vector<string> otherSet = ind->getDocumentsForWord(words.at(argIndex));
//If first, don't do set union because set 1 is empty
if(first){
first = false;
disallow = otherSet;
argIndex++;
continue;
} else {
//THE REASON I'M BOTHERING TO RANK THE DISALLOWED ARTICLES IS THAT MAYBE IN SOME FORM, THE SEARCH
//ENGINE COULD WANT TO AVOID THESE WORDS IF POSSIBLE WITHOUT ENTIRELLY RULING THEM OUT OF THE SEARCH
//Map of key-value pairs keyed by sum of indeces in the original vectors returned by the index in TF/IDF order
//This allows for equal treatment of both parts of the OR clause
map<int, string> finalSet;
//Perform the set union
for(int i = 0; i < allow.size(); i++){
string currentDocName = allow.at(i);
pair<int, string> newMapItem;
newMapItem.first = i;
newMapItem.second = currentDocName;
finalSet.insert(newMapItem);
}
for(int i = 0; i < otherSet.size(); i++){
string currentDocName = otherSet.at(i);
pair<int, string> newMapItem;
newMapItem.first = i;
newMapItem.second = currentDocName;
finalSet.insert(newMapItem);
}
//Empty the disallow vector
disallow.clear();
//Re-fill the allow vector by ascending order of sum of original vector indeces, so the
//documents with the highest average TF/IDF value for the two sets come first
for(map<int, string>::iterator it = finalSet.begin(); it != finalSet.end(); ++it)
disallow.push_back(it->second);
}
argIndex++;
}
//The output from the search
vector<string> outputFromSearch;
//Take all of the allowed documents which are not in disallowed documents and put them in the output
for(int i = 0; i < allow.size(); i++){
string docTitle = allow.at(i);
if(find(disallow.begin(), disallow.end(), docTitle) == disallow.end()){
outputFromSearch.push_back(docTitle);
}
}
//For the first 15 documents to be output
for(int i = 0; i < 15 && i < outputFromSearch.size(); i++){
//Output document number, title, author and timestamp info for the doc
cout << "Document #" << i << " " << outputFromSearch.at(i) << endl;
pair<string, string> authTime = ind->getAuthorAndTimeForDocNamed(outputFromSearch.at(i));
cout << "Author: " << authTime.first << ", Timestamp: " << authTime.second << "\n";
}
cout << "If you'd like to see a doc, type in its number, or else type -1\n";
int pageNum;
//Loop until user says to break by typing -1
while(true){
//Catch invalid entries
if(!(cin >> pageNum)){
cout << "Invalid input. Please try again.\n";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
//Break if the user says so
if(pageNum < 0) break;
//Print out all of the document
else {
//Calculate the document's file name
string fileName = "SearchDocs/";
string docName = outputFromSearch.at(pageNum);
replace(docName.begin(), docName.end(), '/', '.');
replace(docName.begin(), docName.end(), ' ', '_');
fileName += docName;;
cout << fileName << endl;
//Open an IFStream for the document and print it
ifstream in(fileName);
while(in.good() && !in.eof()){
string word;
in >> word;
cout << word << ' ';
}
}
cout << "Type Another Number!\n";
}
}