-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
306 lines (285 loc) · 9.19 KB
/
Copy pathcompiler.cpp
File metadata and controls
306 lines (285 loc) · 9.19 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "compiler.h"
#include "../isr/isrHandler.h"
#include "../index/stemmer/stemmer.h"
#include <stdexcept>
//#include <iostream>
bool QueryParser::IsBaseTerm()
{
return tokenStream.ReadTokenType() == T_WORD || tokenStream.ReadTokenType() == T_OPEN_PAREN
|| tokenStream.ReadTokenType() == T_QUOTE;
}
//compiles an OR constraint
//<OrC> ::= <AndC> { <OrOp> <AndC> }
ISR* QueryParser::OrC()
{
vector<ISR*> terms;
ISR* andTerm = AndC();
if (andTerm == nullptr) {
if (!tokenStream.match(T_OR))
{
// std::cout << "OrC returned nullptr" << std::endl;
return nullptr;
}
andTerm = AndC();
if (andTerm == nullptr)
return nullptr;
}
terms.push_back(andTerm);
while (tokenStream.match(T_OR)) {
ISR* andTerm = AndC();
if (andTerm == nullptr)
throw std::runtime_error("Error: Expected term after OR");
terms.push_back(andTerm);
}
if (terms.size() == 1)
return terms[0];
//allocate a dynamic array for terms
ISR** termsArray = new ISR*[terms.size()];
for (size_t i = 0; i < terms.size(); i++)
termsArray[i] = terms[i];
return handler.OpenISROr(termsArray, terms.size());
}
//compiles an AND constraint
//<AndC> ::= <BaseC> { <BaseC> }
ISR* QueryParser::AndC()
{
vector<ISR*> terms;
ISR* baseTerm = BaseC();
terms.push_back(baseTerm);
//clears AND token
tokenStream.match(T_AND);
//recursively compiles further AND terms
while ( IsBaseTerm() )
{
baseTerm = BaseC();
//clears AND token
tokenStream.match(T_AND);
terms.push_back(baseTerm);
}
//if there is only one term, return it
if (terms.size() == 1 && terms[0] == nullptr)
return nullptr;
else if (terms.size() == 1)
return terms[0];
if (terms.size() == 1)
return terms[0];
//allocate a dynamic array for terms
ISR** termsArray = new ISR*[terms.size()];
for (size_t i = 0; i < terms.size(); i++) {
termsArray[i] = terms[i];
if (terms[i] == nullptr)
{
delete[] termsArray;
return nullptr;
}
}
//otherwise, compile the AND constraint
return handler.OpenISRAnd(termsArray, terms.size());
}
//finds base constraint (parentheses, quotes, words, NOT)
ISR* QueryParser::BaseC()
{
if (tokenStream.match(T_WORD))
{
string w = stemWord(tokenStream.CurrentTokenString());
ISRWord* word = handler.OpenISRWord(w.c_str());
string titleStr(string("@") + w);
ISRWord* title = handler.OpenISRWord(titleStr.c_str());
// string w = tokenStream.CurrentTokenString();
// w = standardize(w);
// std::cout << "w: " << w.c_str() << std::endl;
// if (StopWords::isStopword(w))
// return nullptr;
// w = stemWord(w);
// ISRWord* word = handler.OpenISRWord(w.c_str());
// string titleStr(string("@") + w);
// ISRWord* title = handler.OpenISRWord(titleStr.c_str());
if (word != nullptr) {
// Only store basic ISRWord, not composite structures
flattenedWords.push_back(word);
}
if (title != nullptr) {
flattenedTitles.push_back(title);
}
if (type == 'b') {
return word;
}
else if (type == 't') {
return title;
}
return nullptr;
}
else if ( tokenStream.ReadTokenType() == T_OPEN_PAREN )
{
//std::cout << "see open parenthesis" << std::endl;
return ParenC();
}
else if ( tokenStream.ReadTokenType() == T_QUOTE )
{
//std::cout << "see quote" << std::endl;
return QuoteC();
}
else if ( tokenStream.ReadTokenType() == T_NOT )
{
//std::cout << "hit NOT" << std::endl;
return nullptr;
}
else
return nullptr;
}
//compiles a constraint in parentheses
//<ParenC> ::= ( <OrC> )
ISR* QueryParser::ParenC()
{
if (tokenStream.match(T_OPEN_PAREN))
{
//std::cout << "Gobbled up open parenthesis" << std::endl;
ISR* constraint = OrC();
if (constraint == nullptr)
throw std::runtime_error("Error: Expected constraint after open parenthesis");
if (tokenStream.match(T_CLOSE_PAREN))
return constraint;
else
throw std::runtime_error("Error: Expected close parenthesis after constraint");
}
else
return nullptr;
}
//compiles a constraint in quotes
//<QuoteC> ::= " <wordC> "
// TODO: shoude directly call phrase, or how do you recognize a phrase or a normal word?
ISR* QueryParser::QuoteC()
{
if (tokenStream.match(T_QUOTE))
{
//std::cout << "Gobbled up quote" << std::endl;
ISR* constraint = wordC();
if (tokenStream.match(T_QUOTE))
return constraint;
else
throw std::runtime_error("Error: Expected close quote after constraint");
}
else
return nullptr;
}
//compiles a phrase constraint of one or more words.
//<wordC> ::= <word> { <word> }
ISR* QueryParser::wordC()
{
if (tokenStream.match(T_WORD))
{
vector<ISR*> terms;
//std::cout << "Gobbled up word in quote: " << tokenStream.CurrentTokenString() << std::endl;
// string w = standardize(tokenStream.CurrentTokenString());
// if (StopWords::isStopword(w))
// return nullptr;
// w = stemWord(w);
// ISRWord* word = handler.OpenISRWord(w.c_str());
// string titleStr(string("@") + w);
// ISRWord* title = handler.OpenISRWord(titleStr.c_str());
string w = stemWord( tokenStream.CurrentTokenString() );
ISRWord* word = handler.OpenISRWord(w.c_str());
string titleStr(string("@") + w);
ISRWord* title = handler.OpenISRWord(titleStr.c_str());
if (word != nullptr)
{
flattenedWords.push_back(word);
if (type == 'b')
terms.push_back(word);
}
if (title != nullptr)
{
flattenedTitles.push_back(title);
if (type == 't')
terms.push_back(title);
}
while (tokenStream.match(T_WORD))
{
// string w = standardize(tokenStream.CurrentTokenString());
// if (StopWords::isStopword(w))
// return nullptr;
w = stemWord(tokenStream.CurrentTokenString());
ISRWord* word = handler.OpenISRWord(w.c_str());
string titleStr(string("@") + w);
ISRWord* title = handler.OpenISRWord(titleStr.c_str());
if (word != nullptr)
{
flattenedWords.push_back(word);
if (type == 'b')
terms.push_back(word);
}
if (title != nullptr)
{
flattenedTitles.push_back(title);
if (type == 't')
terms.push_back(title);
}
}
if (terms.size() == 0)
return nullptr;
if (terms.size() == 1)
return terms[0];
else
{
//allocate a dynamic array for terms
ISR** termsArray = new ISR*[terms.size()];
for (size_t i = 0; i < terms.size(); i++)
termsArray[i] = terms[i];
return handler.OpenISRPhrase(termsArray, terms.size());
}
}
else
return nullptr;
}
ISR* QueryParser::compile()
{
// vector<ISR*> included;
// vector<ISR*> excluded;
included.clear();
excluded.clear();
flattenedWords.clear(); // Clear any previous words
flattenedTitles.clear();
try
{
while (tokenStream.ReadTokenType() != T_EOF)
{
if (tokenStream.match(T_NOT))
{
ISR* baseTerm = BaseC();
if (baseTerm != nullptr)
excluded.push_back(baseTerm);
}
else
{
ISR* orTerm = OrC();
if (orTerm != nullptr)
included.push_back(orTerm);
}
}
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
return nullptr;
}
if (included.size() == 0)
{
std::cerr << "Error: No included constraints" << std::endl;
return nullptr;
}
else
{
std::cout << "num of included: " << included.size() << ", num of excluded: " << excluded.size() << std::endl;
std::cout << "num of flattened words: " << flattenedWords.size() << std::endl;
std::cout << "num of flattened titles: " << flattenedTitles.size() << std::endl;
std::cout << "--------------------------------" << std::endl;
}
if (excluded.size() == 0)
{
if (included.size() == 1)
return included[0];
else
return handler.OpenISRAnd(included.data(), included.size());
}
return handler.OpenISRContainer(included.data(), excluded.data(), included.size(), excluded.size());
}