source: trunk/mgpp/text/GSDLQueryParser.cpp@ 3782

Last change on this file since 3782 was 3782, checked in by kjdon, 21 years ago

made the default value for the NEAR operator into a constant defined in the .h file

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/**************************************************************************
2 *
3 * QueryParser.cpp -- Query parser for a simple query language
4 * Copyright (C) 2000 Rodger McNab
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 **************************************************************************/
21
22#include "GSDLQueryParser.h"
23#include "GSDLQueryLex.h"
24
25
26static QueryNode *ParseExpression (UCArray::const_iterator &here,
27 UCArray::const_iterator end,
28 int defaultBoolCOmbine,
29 int defaultStemMethod);
30
31static QueryNode *AndAdd (QueryNode *t1, QueryNode *t2) {
32 if (t1 == NULL) return t2;
33 if (t2 == NULL) return t1;
34
35 AndQueryNode *andNode = new AndQueryNode;
36 andNode->leftNode = t1;
37 andNode->rightNode = t2;
38 return andNode;
39}
40
41static QueryNode *OrAdd (QueryNode *t1, QueryNode *t2) {
42 if (t1 == NULL) return t2;
43 if (t2 == NULL) return t1;
44
45 OrQueryNode *orNode = new OrQueryNode;
46 orNode->leftNode = t1;
47 orNode->rightNode = t2;
48 return orNode;
49}
50
51static QueryNode *NotAdd (QueryNode *t1, QueryNode *t2) {
52 if (t1 == NULL) return t2;
53 if (t2 == NULL) return t1;
54
55 NotQueryNode *notNode = new NotQueryNode;
56 notNode->queryNode = t1;
57 notNode->notNode = t2;
58 return notNode;
59}
60
61// expects the opening bracket to have already been parsed
62// and discarded
63static QueryNode *ParseBracketExpression (UCArray::const_iterator &here,
64 UCArray::const_iterator end,
65 int defaultBoolCombine,
66 int defaultStemMethod) {
67 // get everything in the expression
68 QueryNode *curTree = ParseExpression (here, end, defaultBoolCombine,
69 defaultStemMethod);
70
71 // gobble up tokens until a closing bracket is found
72 // or the end of the string
73 LexEl el;
74 while (ParseLexEl (here, end, el)) {
75 if (el.lexType == CloseBracketE) break;
76 }
77
78 return curTree;
79}
80
81static int ParseInt (UCArray::const_iterator &here,
82 UCArray::const_iterator end) {
83 LexEl el;
84 UCArray::const_iterator oldHere = here;
85 if (ParseLexEl (here, end, el) && el.lexType == IntegerE)
86 return el.num;
87
88 here = oldHere; // not an integer
89 return 0;
90}
91
92// default is within 20 words
93static void SetRangeValues (TermNode &termNode,
94 UCArray &nearby) {
95 UCArray NEARBY; SetCStr(NEARBY, "NEAR");
96 if (nearby == NEARBY) { // no modifier
97 termNode.startRange = (NEAR_DEFAULT+1)*-1;
98 termNode.endRange = NEAR_DEFAULT;
99
100 }
101 else { // extract number
102 UCArray::const_iterator here = nearby.begin()+4;
103 UCArray::const_iterator end = nearby.end();
104 int size=0;
105 while (here != end) {
106 size = size*10 + (*here-'0');
107 here++;
108 }
109 termNode.startRange = -1 * (size+1);
110 termNode.endRange = size;
111 }
112}
113
114static unsigned long GetStemMethod(LexEl &el, int defaultStemMethod) {
115 // here expect el to contain some of c,s,i,u
116 // stem method 0 = c,u 00
117 // stem method 1 = i,u 01 - default for DL
118 // stem method 2 = c, s 10
119 // stem method 3 = i,s 11
120
121 unsigned long stem = (unsigned long)defaultStemMethod;
122
123 UCArray::const_iterator here = el.text.begin();
124 UCArray::const_iterator end = el.text.end();
125
126 unsigned char c1 = *here;
127 if (!(c1 == 'c'| c1 == 'i' | c1 == 'u' | c1 == 's'))
128 return 4; // incorrect format
129
130 here++;
131 unsigned char c2 = 'a';
132 if (here !=end) {
133 c2 = *here;
134 if (!(c2 == 'c'| c2 == 'i' | c2 == 'u' | c2 == 's'))
135 return 4; // incorrect format
136 }
137
138 if (c1 == 'i'|| c2=='i') stem |= 1; // set bit 0 to 1
139 if (c1 == 'c' || c2 == 'c') stem &=0xe; //set bit 0 to 0
140 if (c1 == 's'|| c2 == 's') stem |= 2; // set bit 1 to 1
141 if (c1 == 'u' || c2 =='u') stem &=0xd; // set bit 1 to 0
142 return stem;
143}
144
145
146static void ParseTermModifiers (UCArray::const_iterator &here,
147 UCArray::const_iterator end,
148 TermNode &termNode,
149 int defaultStemMethod) {
150
151 termNode.stemMethod = defaultStemMethod;
152
153 LexEl el;
154 UCArray::const_iterator oldHere = here;
155 while (ParseLexEl (here, end, el)) {
156 if (el.lexType == TermWeightE) {
157 termNode.termWeight = ParseInt (here, end);
158
159 } else if (el.lexType == StemMethodE) {
160 oldHere = here;
161 LexEl stem;
162 if (ParseLexEl (here, end, stem) && stem.lexType == TermE) {
163 termNode.stemMethod = GetStemMethod(stem, defaultStemMethod);
164 if (termNode.stemMethod == 4) { // error so backtrack
165 here = oldHere;
166 termNode.stemMethod = (unsigned long)defaultStemMethod;
167 }
168 }else here = oldHere; //ignore - wrong syntax
169
170 } else if (el.lexType == RangeE) {
171 termNode.startRange = ParseInt (here, end);
172 termNode.endRange = ParseInt (here, end);
173
174 } else if (el.lexType == AtE) {
175 termNode.startRange = termNode.endRange = ParseInt (here, end);
176
177 } else {
178 // no term modifiers
179 here = oldHere;
180 break;
181 }
182
183 oldHere = here;
184 }
185}
186
187static void ParseProxModifiers (UCArray::const_iterator &here,
188 UCArray::const_iterator end,
189 ProxMatchQueryNode *proxNode) {
190 // so far only have one - the tag stuff
191 LexEl el;
192 UCArray::const_iterator oldHere = here;
193 while (ParseLexEl (here, end, el)) {
194 if (el.lexType == TagE) {
195 oldHere = here; // don't backtrack past here
196 if (ParseLexEl (here, end, el) && el.lexType == TermE) {
197 proxNode->tagNodePtr = new TagNode;
198 proxNode->tagNodePtr->tagName = el.text;
199
200 }
201 else { // error in tag
202 here = oldHere;
203 }
204 } // TagE
205 // add in other cases here
206 else {
207 // no modifiers
208 here = oldHere;
209 break;
210 }
211 oldHere = here;
212 }//while
213
214
215}
216
217// expects starting brackets to have been parsed
218static void ParseSquareBrackets(UCArray::const_iterator &here,
219 UCArray::const_iterator end,
220 ProxMatchQueryNode *proxNode,
221 int defaultStemMethod) {
222
223 LexEl el;
224 bool phrase=false;
225 bool first=true;
226 while (ParseLexEl (here, end, el)) {
227 if (el.lexType == TermE || el.lexType == IntegerE) {
228 TermNode termNode;
229 termNode.term = el.text;
230 ParseTermModifiers (here, end, termNode, defaultStemMethod);
231 if (phrase) {
232 if (first) first=false;
233 else {
234 termNode.startRange = -2;
235 termNode.endRange = -1;
236 }
237 }
238 proxNode->terms.push_back(termNode);
239 }
240 else if (el.lexType == CloseSquareBracketE) {
241 break;
242 }
243 else if (el.lexType == AndOpE) {
244 // ignore, the words are AND'ed anyway
245 cerr << "and inside []\n";
246 }
247 else if (el.lexType == OrOpE) {
248 cerr << "or inside []\n";
249 }
250 else if (el.lexType == QuoteE) {
251 // phrase inside square brackets
252 if (phrase) phrase=false;
253 else phrase=true;
254 }
255 else {
256 //error
257 // just ignore for now
258 cerr <<"bad syntax inside []\n";
259 }
260 } // while
261
262}
263// expects the starting quote to have been parsed
264// and discarded
265// now phrases use the case and stem preference options
266// ie can search for a phrase ignoring case
267static void ParsePhrase (UCArray::const_iterator &here,
268 UCArray::const_iterator end,
269 ProxMatchQueryNode &proxNode,
270 int defaultStemMethod) {
271 LexEl el;
272 bool first = true;
273 while (ParseLexEl (here, end, el)) {
274 if (el.lexType == TermE || el.lexType == IntegerE) {
275 TermNode termNode;
276 termNode.term = el.text;
277 //termNode.stemMethod = defaultStemMethod;
278 ParseTermModifiers (here, end, termNode, defaultStemMethod);
279 if (first) {
280 first = false;
281 }
282 else {
283 termNode.startRange = -2;
284 termNode.endRange = -1;
285 }
286 proxNode.terms.push_back (termNode);
287
288 } else if (el.lexType == QuoteE) {
289 break;
290
291 } else {
292 // error
293 break;
294 }
295 }
296}
297
298static QueryNode *ParseTerm (UCArray::const_iterator &here,
299 UCArray::const_iterator end,
300 int defaultBoolCombine,
301 int defaultStemMethod) {
302 LexEl el;
303
304 UCArray::const_iterator oldHere = here;
305 if (!ParseLexEl (here, end, el)) return NULL;
306
307 if (el.lexType == OpenBracketE)
308 return ParseBracketExpression (here, end, defaultBoolCombine,
309 defaultStemMethod);
310
311 ProxMatchQueryNode *proxNode = new ProxMatchQueryNode;
312
313 if (el.lexType == TermE || el.lexType == IntegerE) {
314 TermNode termNode;
315 termNode.term = el.text;
316 ParseTermModifiers (here, end, termNode, defaultStemMethod);
317 oldHere = here; // dont backtrack past here
318 if (ParseLexEl(here, end, el) && el.lexType == NearOpE) {
319 delete proxNode;
320 proxNode = (ProxMatchQueryNode *)ParseTerm(here, end, defaultBoolCombine,
321 defaultStemMethod);
322 SetRangeValues(termNode, el.text);
323 proxNode->terms.push_back (termNode);
324 return proxNode;
325 }
326 else {
327 here = oldHere; // backtrack
328 proxNode->terms.push_back (termNode);
329 ParseProxModifiers(here, end, proxNode);
330 return proxNode;
331 }
332 } else if (el.lexType == QuoteE) {
333 ParsePhrase (here, end, *proxNode, defaultStemMethod);
334 return proxNode;
335 }
336 else if (el.lexType == OpenSquareBracketE) {
337 ParseSquareBrackets (here, end, proxNode, defaultStemMethod);
338 ParseProxModifiers (here, end, proxNode);
339 return proxNode;
340 }
341
342 // not a term
343 here = oldHere;
344 delete proxNode;
345 return NULL;
346}
347
348
349static QueryNode *ParseExpression (UCArray::const_iterator &here,
350 UCArray::const_iterator end,
351 int defaultBoolCombine,
352 int defaultStemMethod) {
353 LexEl el;
354 QueryNode *curTree = NULL;
355
356 UCArray::const_iterator oldHere = here;
357 while (ParseLexEl (here, end, el)) {
358 if (el.lexType == OpenSquareBracketE ||
359 el.lexType == OpenBracketE ||
360 el.lexType == TermE ||
361 el.lexType == QuoteE ||
362 el.lexType == IntegerE ) {
363 // el.lexType == TagE) { //tag at end of term now
364 // some type of term, back track and parse it
365 here = oldHere;
366 // if default==1, AND, else if==0, OR
367 if (defaultBoolCombine) {
368 curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
369 defaultStemMethod));
370 }
371 else {
372 curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
373 defaultStemMethod));
374 }
375
376 } else if (el.lexType == AndOpE) {
377 curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
378 defaultStemMethod));
379
380 } else if (el.lexType == OrOpE) {
381 curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
382 defaultStemMethod));
383
384 } else if (el.lexType == NotOpE) {
385 curTree = NotAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
386 defaultStemMethod));
387
388 } else if (el.lexType == CloseBracketE) {
389 // parsebracketexpression is waiting for the last bracket, so put it back
390 here = oldHere;
391 break;
392
393 } else break;
394
395 oldHere = here;
396 }
397
398 return curTree;
399}
400
401QueryNode *ParseQuery (const UCArray &queryStr, int defaultBoolCombine,
402 int defaultStemMethod) {
403 UCArray::const_iterator here = queryStr.begin();
404 UCArray::const_iterator end = queryStr.end();
405 return ParseExpression (here, end, defaultBoolCombine, defaultStemMethod);
406}
Note: See TracBrowser for help on using the repository browser.