source: trunk/gsdl/src/mgpp/text/GSDLQueryParser.cpp@ 3161

Last change on this file since 3161 was 3161, checked in by kjdon, 22 years ago

syntax now handles phrases insode a field search, and term modifiers(#is)
inside a phrase.

  • 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 = -21;
98 termNode.endRange = 20;
99 }
100 else { // extract number
101 UCArray::const_iterator here = nearby.begin()+4;
102 UCArray::const_iterator end = nearby.end();
103 int size=0;
104 while (here != end) {
105 size = size*10 + (*here-'0');
106 here++;
107 }
108 cerr <<"size= "<<size<<endl;
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 cerr << "stem is "<<stem<<endl;
143 return stem;
144}
145
146
147static void ParseTermModifiers (UCArray::const_iterator &here,
148 UCArray::const_iterator end,
149 TermNode &termNode,
150 int defaultStemMethod) {
151
152 termNode.stemMethod = defaultStemMethod;
153
154 LexEl el;
155 UCArray::const_iterator oldHere = here;
156 while (ParseLexEl (here, end, el)) {
157 if (el.lexType == TermWeightE) {
158 termNode.termWeight = ParseInt (here, end);
159
160 } else if (el.lexType == StemMethodE) {
161 oldHere = here;
162 LexEl stem;
163 if (ParseLexEl (here, end, stem) && stem.lexType == TermE) {
164 termNode.stemMethod = GetStemMethod(stem, defaultStemMethod);
165 if (termNode.stemMethod == 4) { // error so backtrack
166 here = oldHere;
167 termNode.stemMethod = (unsigned long)defaultStemMethod;
168 }
169 }else here = oldHere; //ignore - wrong syntax
170
171 } else if (el.lexType == RangeE) {
172 termNode.startRange = ParseInt (here, end);
173 termNode.endRange = ParseInt (here, end);
174
175 } else if (el.lexType == AtE) {
176 termNode.startRange = termNode.endRange = ParseInt (here, end);
177
178 } else {
179 // no term modifiers
180 here = oldHere;
181 break;
182 }
183
184 oldHere = here;
185 }
186}
187
188static void ParseProxModifiers (UCArray::const_iterator &here,
189 UCArray::const_iterator end,
190 ProxMatchQueryNode *proxNode) {
191 // so far only have one - the tag stuff
192 LexEl el;
193 UCArray::const_iterator oldHere = here;
194 while (ParseLexEl (here, end, el)) {
195 if (el.lexType == TagE) {
196 oldHere = here; // don't backtrack past here
197 if (ParseLexEl (here, end, el) && el.lexType == TermE) {
198 proxNode->tagNodePtr = new TagNode;
199 proxNode->tagNodePtr->tagName = el.text;
200
201 }
202 else { // error in tag
203 here = oldHere;
204 }
205 } // TagE
206 // add in other cases here
207 else {
208 // no modifiers
209 here = oldHere;
210 break;
211 }
212 oldHere = here;
213 }//while
214
215
216}
217
218// expects starting brackets to have been parsed
219static void ParseSquareBrackets(UCArray::const_iterator &here,
220 UCArray::const_iterator end,
221 ProxMatchQueryNode *proxNode,
222 int defaultStemMethod) {
223
224 LexEl el;
225 bool phrase=false;
226 bool first=true;
227 while (ParseLexEl (here, end, el)) {
228 if (el.lexType == TermE || el.lexType == IntegerE) {
229 TermNode termNode;
230 termNode.term = el.text;
231 ParseTermModifiers (here, end, termNode, defaultStemMethod);
232 if (phrase) {
233 if (first) first=false;
234 else {
235 termNode.startRange = -2;
236 termNode.endRange = -1;
237 }
238 }
239 proxNode->terms.push_back(termNode);
240 }
241 else if (el.lexType == CloseSquareBracketE) {
242 break;
243 }
244 else if (el.lexType == AndOpE) {
245 // ignore, the words are AND'ed anyway
246 cerr << "and inside []\n";
247 }
248 else if (el.lexType == OrOpE) {
249 cerr << "or inside []\n";
250 }
251 else if (el.lexType == QuoteE) {
252 // phrase inside square brackets
253 if (phrase) phrase=false;
254 else phrase=true;
255 }
256 else {
257 //error
258 // just ignore for now
259 cerr <<"bad syntax inside []\n";
260 }
261 } // while
262
263}
264// expects the starting quote to have been parsed
265// and discarded
266// now phrases use the case and stem preference options
267// ie can search for a phrase ignoring case
268static void ParsePhrase (UCArray::const_iterator &here,
269 UCArray::const_iterator end,
270 ProxMatchQueryNode &proxNode,
271 int defaultStemMethod) {
272 LexEl el;
273 bool first = true;
274 while (ParseLexEl (here, end, el)) {
275 if (el.lexType == TermE || el.lexType == IntegerE) {
276 TermNode termNode;
277 termNode.term = el.text;
278 //termNode.stemMethod = defaultStemMethod;
279 ParseTermModifiers (here, end, termNode, defaultStemMethod);
280 if (first) {
281 first = false;
282 }
283 else {
284 termNode.startRange = -2;
285 termNode.endRange = -1;
286 }
287 proxNode.terms.push_back (termNode);
288
289 } else if (el.lexType == QuoteE) {
290 break;
291
292 } else {
293 // error
294 break;
295 }
296 }
297}
298
299static QueryNode *ParseTerm (UCArray::const_iterator &here,
300 UCArray::const_iterator end,
301 int defaultBoolCombine,
302 int defaultStemMethod) {
303 LexEl el;
304
305 UCArray::const_iterator oldHere = here;
306 if (!ParseLexEl (here, end, el)) return NULL;
307
308 if (el.lexType == OpenBracketE)
309 return ParseBracketExpression (here, end, defaultBoolCombine,
310 defaultStemMethod);
311
312 ProxMatchQueryNode *proxNode = new ProxMatchQueryNode;
313
314 if (el.lexType == TermE || el.lexType == IntegerE) {
315 TermNode termNode;
316 termNode.term = el.text;
317 ParseTermModifiers (here, end, termNode, defaultStemMethod);
318 oldHere = here; // dont backtrack past here
319 if (ParseLexEl(here, end, el) && el.lexType == NearOpE) {
320 delete proxNode;
321 proxNode = (ProxMatchQueryNode *)ParseTerm(here, end, defaultBoolCombine,
322 defaultStemMethod);
323 SetRangeValues(termNode, el.text);
324 proxNode->terms.push_back (termNode);
325 return proxNode;
326 }
327 else {
328 here = oldHere; // backtrack
329 proxNode->terms.push_back (termNode);
330 ParseProxModifiers(here, end, proxNode);
331 return proxNode;
332 }
333 } else if (el.lexType == QuoteE) {
334 ParsePhrase (here, end, *proxNode, defaultStemMethod);
335 return proxNode;
336 }
337 else if (el.lexType == OpenSquareBracketE) {
338 ParseSquareBrackets (here, end, proxNode, defaultStemMethod);
339 ParseProxModifiers (here, end, proxNode);
340 return proxNode;
341 }
342
343 // not a term
344 here = oldHere;
345 delete proxNode;
346 return NULL;
347}
348
349
350static QueryNode *ParseExpression (UCArray::const_iterator &here,
351 UCArray::const_iterator end,
352 int defaultBoolCombine,
353 int defaultStemMethod) {
354 LexEl el;
355 QueryNode *curTree = NULL;
356
357 UCArray::const_iterator oldHere = here;
358 while (ParseLexEl (here, end, el)) {
359 if (el.lexType == OpenSquareBracketE ||
360 el.lexType == OpenBracketE ||
361 el.lexType == TermE ||
362 el.lexType == QuoteE ||
363 el.lexType == IntegerE ) {
364 // el.lexType == TagE) { //tag at end of term now
365 // some type of term, back track and parse it
366 here = oldHere;
367 // if default==1, AND, else if==0, OR
368 if (defaultBoolCombine) {
369 curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
370 defaultStemMethod));
371 }
372 else {
373 curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
374 defaultStemMethod));
375 }
376
377 } else if (el.lexType == AndOpE) {
378 curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
379 defaultStemMethod));
380
381 } else if (el.lexType == OrOpE) {
382 curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
383 defaultStemMethod));
384
385 } else if (el.lexType == NotOpE) {
386 curTree = NotAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
387 defaultStemMethod));
388
389 } else if (el.lexType == CloseBracketE) {
390 // parsebracketexpression is waiting for the last bracket, so put it back
391 here = oldHere;
392 break;
393
394 } else break;
395
396 oldHere = here;
397 }
398
399 return curTree;
400}
401
402QueryNode *ParseQuery (const UCArray &queryStr, int defaultBoolCombine,
403 int defaultStemMethod) {
404 UCArray::const_iterator here = queryStr.begin();
405 UCArray::const_iterator end = queryStr.end();
406 return ParseExpression (here, end, defaultBoolCombine, defaultStemMethod);
407}
Note: See TracBrowser for help on using the repository browser.