source: trunk/gsdl/src/recpt/querytools.cpp@ 7432

Last change on this file since 7432 was 7432, checked in by mdewsnip, 20 years ago

(Human Info) Added language parameter to all calls of get_info, get_children, has_children, and get_contents. Also, removed some of the modifications so it compiles again.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/**********************************************************************
2 *
3 * querytools.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "querytools.h"
27#include <ctype.h>
28#include "unitool.h" // for is_unicode_letdig
29
30// request.filterResultOptions and request.fields (if required) should
31// be set from the calling code
32void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring,
33 cgiargsclass &args) {
34
35 request.filterName = "QueryFilter";
36
37 OptionValue_t option;
38
39 option.name = "Term";
40 option.value = querystring;
41 request.filterOptions.push_back (option);
42
43 option.name = "QueryType";
44 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
45 request.filterOptions.push_back (option);
46
47 option.name = "MatchMode";
48 option.value = (args.getintarg("t")) ? "some" : "all";
49 request.filterOptions.push_back (option);
50
51 option.name = "Casefold";
52 option.value = (args.getintarg("k")) ? "true" : "false";
53 request.filterOptions.push_back (option);
54
55 option.name = "Stem";
56 option.value = (args.getintarg("s")) ? "true" : "false";
57 request.filterOptions.push_back (option);
58
59 if (!args["h"].empty()) {
60 option.name = "Index";
61 option.value = args["h"];
62 request.filterOptions.push_back (option);
63 }
64
65 if (!args["j"].empty()) {
66 option.name = "Subcollection";
67 option.value = args["j"];
68 request.filterOptions.push_back (option);
69 }
70
71 if (!args["n"].empty()) {
72 option.name = "Language";
73 option.value = args["n"];
74 request.filterOptions.push_back (option);
75 }
76
77 if (!args["g"].empty()) { // granularity for mgpp
78 option.name = "Level";
79 option.value = args["g"];
80 request.filterOptions.push_back (option);
81 }
82
83 set_more_queryfilter_options (request, args);
84}
85
86void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring1,
87 const text_t &querystring2, cgiargsclass &args) {
88
89 set_queryfilter_options (request, querystring1, args);
90
91 // fill in the second query if needed
92 if (!args["cq2"].empty()) {
93 OptionValue_t option;
94
95 option.name = "CombineQuery";
96 option.value = args["cq2"];
97 request.filterOptions.push_back (option);
98
99 option.name = "Term";
100 option.value = querystring2;
101 request.filterOptions.push_back (option);
102
103 option.name = "QueryType";
104 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
105 request.filterOptions.push_back (option);
106
107 option.name = "Casefold";
108 option.value = (args.getintarg("k")) ? "true" : "false";
109 request.filterOptions.push_back (option);
110
111 option.name = "Stem";
112 option.value = (args.getintarg("s")) ? "true" : "false";
113 request.filterOptions.push_back (option);
114
115 if (!args["h2"].empty()) {
116 option.name = "Index";
117 option.value = args["h2"];
118 request.filterOptions.push_back (option);
119 }
120
121 if (!args["j2"].empty()) {
122 option.name = "Subcollection";
123 option.value = args["j2"];
124 request.filterOptions.push_back (option);
125 }
126
127 if (!args["n2"].empty()) {
128 option.name = "Language";
129 option.value = args["n2"];
130 request.filterOptions.push_back (option);
131 }
132 }
133 set_more_queryfilter_options (request, args);
134}
135
136void set_more_queryfilter_options (FilterRequest_t &request, cgiargsclass &args) {
137
138 OptionValue_t option;
139 int arg_m = args.getintarg("m");
140
141 option.name = "Maxdocs";
142 option.value = arg_m;
143 request.filterOptions.push_back (option);
144
145 // option.name = "StartResults";
146 // option.value = args["r"];
147 // request.filterOptions.push_back (option);
148
149 // option.name = "EndResults";
150 // int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
151 // if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
152 // option.value = endresults;
153 // request.filterOptions.push_back (option);
154}
155
156void format_querystring (text_t &querystring, int querymode, bool segment) {
157 text_t formattedstring;
158
159 if (querymode == 1 && !segment) return;
160
161 text_t::const_iterator here = querystring.begin();
162 text_t::const_iterator end = querystring.end();
163
164 // space is used to insert spaces between Chinese
165 // characters. No space is needed before the first
166 // Chinese character.
167 bool space = false;
168
169 // want to remove ()|!& from querystring so boolean queries are just
170 // "all the words" queries (unless querymode is advanced)
171 while (here != end) {
172 if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
173 *here == '!' || *here == '&')) {
174 formattedstring.push_back(' ');
175 } else if (segment) {
176 if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
177 (*here >= 0xf900 && *here <= 0xfa2d)) {
178 // Chinese character
179 if (space) formattedstring.push_back (0x200b);
180 formattedstring.push_back (*here);
181 formattedstring.push_back (0x200b);
182 space = true;
183 } else {
184 // non-Chinese character
185 formattedstring.push_back (*here);
186 space = false;
187 }
188
189 } else {
190 formattedstring.push_back (*here);
191 }
192 here ++;
193 }
194 querystring = formattedstring;
195}
196
197
198
199void add_dates(text_t &querystring, int startdate, int enddate,
200 int startbc, int endbc)
201{
202 if(startdate)
203 {
204 int querystringis = 0;
205 text_t::const_iterator here = querystring.begin();
206 text_t::const_iterator end = querystring.end();
207 while(here!=end)
208 {
209 if(!(isspace((*here)))){
210 here = end;
211 querystringis = 1;
212 }
213 else
214 here++;
215 }
216 //converting BCE dates
217 if(startbc && startdate > 0)
218 {
219 startdate *= -1;
220 }
221 if(endbc && enddate > 0)
222 {
223 enddate *= -1;
224 }
225 if(enddate != 0 && enddate<startdate)
226 {
227 cout<<"enddate too small"<<endl;
228 return;
229 }
230 if(querystringis)
231 querystring.appendcstr(" AND");
232 if(!enddate)
233 {
234 querystring.appendcstr(" [");
235 if(startdate<0)
236 {
237 querystring.appendcstr("bc");
238 querystring.appendint((startdate*-1));
239 }
240 else
241 querystring.appendint(startdate);
242 querystring.appendcstr("]:CV");
243 }
244 else{
245 int nextdate = startdate;
246 querystring.appendcstr(" (");
247 while(nextdate<=enddate)
248 {
249 if(nextdate!=0)
250 {
251 querystring.appendcstr(" [");
252 if(nextdate<0)
253 {
254 querystring.appendcstr("bc");
255 querystring.appendint((nextdate*-1));
256 }
257 else
258 querystring.appendint(nextdate);
259 querystring.appendcstr("]:CV");
260 }
261 nextdate++;
262 }
263 querystring.appendcstr(" )");
264 }
265 }
266
267}
268
269void get_phrases (const text_t &querystring, text_tarray &phrases) {
270
271 phrases.erase (phrases.begin(), phrases.end());
272 if (!querystring.empty()) {
273
274 text_t::const_iterator end = querystring.end();
275 text_t::const_iterator here = findchar (querystring.begin(), end, '"');
276 if (here != end) {
277 text_t tmptext;
278 bool foundquote = false;
279 while (here != end) {
280 if (*here == '"') {
281 if (foundquote) {
282 if (!tmptext.empty()) {
283 phrases.push_back(tmptext);
284 tmptext.clear();
285 }
286 foundquote = false;
287 } else foundquote = true;
288 } else {
289 if (foundquote) tmptext.push_back (*here);
290 }
291 here ++;
292 }
293 }
294 }
295}
296
297// search history tool
298// also used for form query macros
299text_t escape_quotes(const text_t &querystring) {
300
301 text_t::const_iterator here = querystring.begin();
302 text_t::const_iterator end = querystring.end();
303
304 text_t escquery = "";
305 while (here != end) {
306 if (*here != '\'' && *here != '\"' && *here != '\n' && *here != '\r') escquery.push_back(*here);
307 else if (*here == '\n' || *here == '\r') {
308 escquery.push_back(' ');
309 } else {
310 escquery +="\\\\";
311 escquery.push_back(*here);
312 }
313
314 here++;
315 }
316 return escquery;
317
318}
319
320// some query form parsing functions for use with mgpp
321
322void parse_reg_query_form(text_t &querystring, cgiargsclass &args){
323
324 querystring.clear();
325 text_t combine;
326 int argt = args.getintarg("t");// t=0 -and, t=1 - or
327 if (argt == 0) combine = "&";
328 else combine = "|";
329
330 text_t field = args["fqf"];
331 if (field.empty()) return; // no query
332 text_tarray fields;
333 splitchar(field.begin(), field.end(), ',', fields);
334
335 text_t value = args["fqv"];
336 if (value.empty()) return; // somethings wrong
337 text_tarray values;
338 splitchar(value.begin(), value.end(), ',', values);
339
340 for (int i=0; i< values.size(); i++) {
341 if (!values[i].empty()) {
342 addqueryelem(querystring, fields[i], values[i], combine);
343 }
344 }
345
346}
347
348
349void parse_adv_query_form(text_t &querystring, cgiargsclass &args){
350
351 querystring.clear();
352 text_t combine = "&";
353
354 text_t field = args["fqf"];
355 if (field.empty()) return; // no query
356 text_tarray fields;
357 splitchar(field.begin(), field.end(), ',', fields);
358
359 text_t value = args["fqv"];
360 if (value.empty()) return; // somethings wrong
361 text_tarray values;
362 splitchar(value.begin(), value.end(), ',', values);
363
364 text_t stem = args["fqs"];
365 if (stem.empty()) return; // somethings wrong
366 text_tarray stems;
367 splitchar(stem.begin(), stem.end(), ',', stems);
368
369 text_t fold = args["fqk"];
370 if (fold.empty()) return; // somethings wrong
371 text_tarray folds;
372 splitchar(fold.begin(), fold.end(), ',', folds);
373
374 text_t comb = args["fqc"];
375 if (comb.empty()) return; //somethings wrong
376 text_tarray combs;
377 splitchar(comb.begin(), comb.end(), ',', combs);
378
379 for(int i=0; i< values.size(); i++) {
380 if (!values[i].empty()) {
381 if (i!=0) {
382 if (combs[i-1]=="and") combine = "&";
383 else if (combs[i-1]=="or")combine = "|";
384 else if (combs[i-1]=="not")combine = "!";
385 }
386 text_t term = addstemcase(values[i], stems[i], folds[i]);
387 addqueryelem(querystring, fields[i], term, combine);
388 }
389
390 }
391}
392
393text_t addstemcase(const text_t &terms, const text_t &stem, const text_t &fold) {
394
395 text_t outtext;
396 text_t word;
397 //unsigned short c;
398 text_t::const_iterator here = terms.begin();
399 text_t::const_iterator end = terms.end();
400
401 while (here !=end) {
402
403 if (is_unicode_letdig(*here)) {
404 // not word boundary
405 word.push_back(*here);
406 here++;
407 }
408 else {
409 // found word boundary
410 if (!word.empty() ) {
411 if (stem == "1" || fold =="1") {
412 word += "#";
413 if (stem == "1") word += "s";
414 //else word += "u";
415
416 if (fold == "1") word += "i";
417 //else word += "c";
418 }
419
420 word += " ";
421 outtext += word;
422 word.clear();
423 }
424 if (*here == '\"') {
425 outtext.push_back(*here);
426 }
427 here++;
428 }
429 }
430
431 // get last word
432 if (!word.empty()) {
433 if (stem == "1"|| fold == "1") {
434 word += "#";
435 if (stem == "1") word += "s";
436 //else word += "u";
437
438 if (fold == "1") word += "i";
439 //else word += "c";
440 }
441 word += " ";
442 outtext += word;
443 }
444 return outtext;
445}
446
447
448void addqueryelem(text_t &querystring, text_t &tag,
449 text_t &query, text_t &combine) {
450 if (!querystring.empty()) { // have to put and/or
451 querystring += " "+combine + " ";
452
453 }
454 if (tag=="ZZ" || tag=="") { // just add onto querystring
455 querystring += query;
456 }
457 else {
458 querystring += "["+query+"]:"+tag;
459 }
460
461}
462
463
464void addqueryelem_ex(text_t &querystring, const text_t &tag,
465 const text_t &terms, const text_t &stem, const text_t &fold,
466 const text_t& combine, const text_t& word_combine) {
467 if (!querystring.empty()) { // have to put and/or
468 querystring += " " + combine + " ";
469 }
470 text_t outtext; outtext.reserve(512);
471 text_t word; word.reserve(100);
472 //unsigned short c;
473 text_t::const_iterator here = terms.begin();
474 text_t::const_iterator end = terms.end();
475 bool inquote = false, firstword = true;
476
477 text_t word2; word2.reserve(256);
478
479 while (here !=end) {
480 if (is_unicode_space(*here)) {
481 if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
482 else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
483 else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
484 else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
485 else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
486 if (inquote) {
487 word2.push_back(*here);
488 }
489 word.append(word2); word2.clear();
490
491 if (!inquote && !word.empty() ) {
492 // found word boundary
493
494 if (stem == "1" || fold =="1") {
495 word += "#";
496 if (stem == "1") word += "s";
497 //else word += "u";
498
499 if (fold == "1") word += "i";
500 //else word += "c";
501 }
502 if (firstword) {
503 firstword = false;
504 } else {
505 outtext += " " + word_combine + " ";
506 }
507 outtext += "[" + word + "]:"+tag;
508 word.clear();
509 }
510 ++here;
511 } else if (*here == '\"') {
512 word2.push_back(*here);
513 inquote = !inquote;
514 ++here;
515 } else {
516 // not word boundary
517 word2.push_back(*here);
518 ++here;
519 }
520 }
521
522 // get last word
523 if (!word2.empty()) {
524 if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
525 else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
526 else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
527 else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
528 else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
529 word.append(word2); word2.clear();
530
531 if (stem == "1"|| fold == "1") {
532 word += "#";
533 if (stem == "1") word += "s";
534 //else word += "u";
535
536 if (fold == "1") word += "i";
537 //else word += "c";
538 }
539 if (!outtext.empty()) outtext += " " + word_combine + " ";
540 outtext += "[" + word + "]:"+tag;
541 }
542 querystring += "(" + outtext + ")";
543}
544
545
546void format_field_info(text_t & querystring, const text_t &tag) {
547
548 if (tag == "ZZ" || tag == "") {
549 return; // do nothing
550 }
551
552 querystring = "["+querystring+"]:"+tag;
553}
Note: See TracBrowser for help on using the repository browser.