source: gsdl/trunk/runtime-src/src/recpt/querytools.cpp@ 16645

Last change on this file since 16645 was 16645, checked in by kjdon, 16 years ago

upgraded segmentation stuff in format_querystring (using unicode 4.0) to include more Chinese characters and Japanese and Korean characters

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 24.2 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// sets the ct, qt, qto arguments
31void set_query_type_args(ColInfoResponse_t *cinfo, cgiargsclass &args) {
32
33 if (args["ct"].empty()) {
34 text_t build_type = cinfo->buildType;
35 if (build_type == "mgpp") {
36 args["ct"] = "1";
37 } else if (build_type == "lucene") {
38 args["ct"] = "2";
39 } else {
40 args["ct"] = "0";
41 }
42 }
43 text_t arg_ct = args["ct"];
44 if (arg_ct == "0") {
45 // mg
46 args["qt"] = "0";
47 args["qto"] = "0";
48 return;
49 }
50
51 if (!args["qt"].empty() && !args["qto"].empty()) {
52 return;
53 }
54
55 text_tmap::iterator check = cinfo->format.find("SearchTypes");
56 text_t search_types;
57 if(check != cinfo->format.end() && !(*check).second.empty()){
58 search_types = (*check).second;
59 } else {
60 // assume plain,form
61 if (args["qto"].empty()) args["qto"] = "3";
62 if (args["qt"].empty()) {
63 int arg_qto = args.getintarg("qto");
64 if (arg_qto == 2) {
65 args["qt"] = "1";
66 } else {
67 args["qt"] = "0";
68 }
69 }
70 return;
71 }
72
73
74 if (args["qto"].empty()) {
75 unsigned int type = 0;
76 if (findword(search_types.begin(), search_types.end(), "form") != search_types.end()) {
77 type |= 2;
78 }
79 if (findword(search_types.begin(), search_types.end(), "plain") != search_types.end()) {
80 type |= 1;
81 }
82 args.setintarg("qto", type);
83 }
84
85 if (args["qt"].empty()) {
86 int arg_qto = args.getintarg("qto");
87 if (arg_qto == 2 || (arg_qto == 3 && starts_with(search_types, "form"))) {
88 args["qt"] = "1";
89 } else {
90 args["qt"] = "0";
91 }
92 }
93}
94
95// sets the ks, ss, afs (casesupport, stemsupport, accentfoldsupport) args
96void set_stem_index_args(ColInfoResponse_t *cinfo, cgiargsclass &args) {
97 int stemIndexes = cinfo->stemIndexes;
98
99 if (stemIndexes & SIcasefold) {
100 args["ks"] = 1;
101 }
102 if (stemIndexes & SIstem) {
103 args["ss"] = 1;
104 }
105 if (stemIndexes & SIaccentfold) {
106 args["afs"] = 1;
107 }
108
109}
110
111// request.filterResultOptions and request.fields (if required) should
112// be set from the calling code
113void set_queryfilter_options (FilterRequest_t &request,
114 const text_t &querystring,
115 cgiargsclass &args) {
116
117 request.filterName = "QueryFilter";
118
119 OptionValue_t option;
120
121 option.name = "Term";
122 option.value = querystring;
123 request.filterOptions.push_back (option);
124
125 option.name = "QueryType";
126 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
127 request.filterOptions.push_back (option);
128
129 option.name = "MatchMode";
130 // mgpp in advanced mode, always use some query
131 if (args.getintarg("ct") == 1 && args.getintarg("b") == 1) {
132 option.value = "some";
133 } else {
134 option.value = (args.getintarg("t")) ? "some" : "all";
135 }
136 request.filterOptions.push_back (option);
137
138 option.name = "Casefold";
139 option.value = (args.getintarg("k")) ? "true" : "false";
140 request.filterOptions.push_back (option);
141
142 option.name = "Stem";
143 option.value = (args.getintarg("s")) ? "true" : "false";
144 request.filterOptions.push_back (option);
145
146 option.name = "AccentFold";
147 option.value = (args.getintarg("af")) ? "true" : "false";
148 request.filterOptions.push_back (option);
149
150 if (!args["h"].empty()) {
151 option.name = "Index";
152 option.value = args["h"];
153 request.filterOptions.push_back (option);
154 }
155
156 if (!args["j"].empty()) {
157 option.name = "Subcollection";
158 option.value = args["j"];
159 request.filterOptions.push_back (option);
160 }
161
162 if (!args["n"].empty()) {
163 option.name = "Language";
164 option.value = args["n"];
165 request.filterOptions.push_back (option);
166 }
167
168 if (!args["g"].empty()) { // granularity for mgpp
169 option.name = "Level";
170 option.value = args["g"];
171 request.filterOptions.push_back (option);
172 }
173
174 if (!args["fs"].empty()) { // filter string for lucene
175 option.name = "FilterString";
176 option.value = args["fs"];
177 request.filterOptions.push_back (option);
178 }
179
180 if (!args["sf"].empty()) { // sort field for lucene
181 option.name = "SortField";
182 option.value = args["sf"];
183 request.filterOptions.push_back (option);
184 }
185
186 if (!args["fuzziness"].empty() && args["fuzziness"] != "100") { // fuzziness value for lucene
187 option.name = "Fuzziness";
188 option.value = (text_t) "0." + args["fuzziness"];
189 request.filterOptions.push_back (option);
190 }
191
192 set_more_queryfilter_options (request, args);
193}
194
195void set_queryfilter_options (FilterRequest_t &request,
196 const text_t &querystring1,
197 const text_t &querystring2, cgiargsclass &args) {
198
199 set_queryfilter_options (request, querystring1, args);
200
201 // fill in the second query if needed
202 if (!args["cq2"].empty()) {
203 OptionValue_t option;
204
205 option.name = "CombineQuery";
206 option.value = args["cq2"];
207 request.filterOptions.push_back (option);
208
209 option.name = "Term";
210 option.value = querystring2;
211 request.filterOptions.push_back (option);
212
213 option.name = "QueryType";
214 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
215 request.filterOptions.push_back (option);
216
217 option.name = "Casefold";
218 option.value = (args.getintarg("k")) ? "true" : "false";
219 request.filterOptions.push_back (option);
220
221 option.name = "Stem";
222 option.value = (args.getintarg("s")) ? "true" : "false";
223 request.filterOptions.push_back (option);
224
225 option.name = "AccentFold";
226 option.value = (args.getintarg("af")) ? "true" : "false";
227 request.filterOptions.push_back (option);
228
229 if (!args["h2"].empty()) {
230 option.name = "Index";
231 option.value = args["h2"];
232 request.filterOptions.push_back (option);
233 }
234
235 if (!args["j2"].empty()) {
236 option.name = "Subcollection";
237 option.value = args["j2"];
238 request.filterOptions.push_back (option);
239 }
240
241 if (!args["n2"].empty()) {
242 option.name = "Language";
243 option.value = args["n2"];
244 request.filterOptions.push_back (option);
245 }
246 }
247 set_more_queryfilter_options (request, args);
248}
249
250void set_more_queryfilter_options (FilterRequest_t &request,
251 cgiargsclass &args) {
252
253 OptionValue_t option;
254 int arg_m = args.getintarg("m");
255
256 option.name = "Maxdocs";
257 option.value = arg_m;
258 request.filterOptions.push_back (option);
259
260 // option.name = "StartResults";
261 // option.value = args["r"];
262 // request.filterOptions.push_back (option);
263
264 // option.name = "EndResults";
265 // int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
266 // if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
267 // option.value = endresults;
268 // request.filterOptions.push_back (option);
269}
270
271bool is_special_character(int indexer_type, unsigned short character) {
272 // mgpp
273 if (indexer_type == 1) {
274 return (character == '#' || character == '/' || character == '*');
275 }
276 // lucene
277 else if (indexer_type == 2) {
278 return (character == '?' || character == '*' || character == '~' ||
279 character == '^');
280 }
281 return false;
282}
283
284// This function removes boolean operators from simple searches, and segments
285// chinese characters if segment=true
286void format_querystring (text_t &querystring, int querymode, bool segment) {
287 text_t formattedstring;
288
289 // advanced search, no segmenting, don't need to do anything
290 if (querymode == 1 && !segment) return;
291
292 text_t::const_iterator here = querystring.begin();
293 text_t::const_iterator end = querystring.end();
294
295 // space is used to insert spaces between Chinese
296 // characters. No space is needed before the first
297 // Chinese character.
298 bool space = false;
299
300 // want to remove ()|!& from querystring so boolean queries are just
301 // "all the words" queries (unless querymode is advanced)
302 while (here != end) {
303 if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
304 *here == '!' || *here == '&')) {
305 formattedstring.push_back(' ');
306 } else if (segment) {
307 if ((*here >= 0x2e80 && *here <= 0xfa6a) ||
308 (*here >= 0x20000 && *here <= 0x2a6d6) ||
309 (*here >= 0x2f800 && *here <= 0x2fa1d)) {
310
311 // CJK character
312 if (!space) formattedstring.push_back (0x200b); // zero width space
313 formattedstring.push_back (*here);
314 formattedstring.push_back (0x200b);
315 space = true;
316 } else {
317
318 // non-Chinese character
319 formattedstring.push_back (*here);
320 space = false;
321
322 }
323
324 } else {
325 formattedstring.push_back (*here);
326 }
327 ++here;
328 }
329 querystring = formattedstring;
330}
331
332
333
334
335// search history tool
336// also used for form query macros
337text_t escape_quotes(const text_t &querystring) {
338
339 text_t::const_iterator here = querystring.begin();
340 text_t::const_iterator end = querystring.end();
341
342 text_t escquery = "";
343 while (here != end) {
344 if (*here != '\'' && *here != '\"' && *here != '\n' && *here != '\r') escquery.push_back(*here);
345 else if (*here == '\n' || *here == '\r') {
346 escquery.push_back(' ');
347 } else {
348 escquery +="\\\\";
349 escquery.push_back(*here);
350 }
351
352 ++here;
353 }
354 return escquery;
355
356}
357
358// Parses the terms into words, and adds #si if necessary
359text_t addstemcase(const text_t &terms, const text_t &stem, const text_t &fold,
360 const int indexer_type) {
361
362 // the default stem and case are set to 0 if this is being used, so we are only adding on qualifiers if stem or fold is 1.
363 if (stem == "0" && fold == "0") {
364 return terms;
365 }
366 // this is only for mgpp collections, shouldn't be called for anything else
367 if (indexer_type != 1) {
368 return terms;
369 }
370
371 text_t outtext;
372 text_t word;
373
374 text_t::const_iterator here = terms.begin();
375 text_t::const_iterator end = terms.end();
376
377 while (here !=end) {
378
379 if (is_unicode_letdig(*here) || is_special_character(indexer_type, *here)) {
380 // not word boundary
381 word.push_back(*here);
382 ++here;
383 }
384 else {
385 // found word boundary
386 if (!word.empty() ) {
387 if (starts_with(word, "NEAR") || starts_with(word, "WITHIN")) {
388 outtext += word;
389 word.clear();
390 }
391 else {
392 word += "#";
393 if (stem == "1") word += "s";
394 if (fold == "1") word += "i";
395 outtext += word;
396 word.clear();
397 }
398 }
399 // this only used in advanced form, so we leave in boolean operators
400 if (*here == '\"' || *here == '&' || *here == '|' || *here == '!' ||
401 *here == '(' || *here == ')' || is_unicode_space(*here)) {
402 outtext.push_back(*here);
403 }
404 ++here;
405 }
406 }
407
408 // get last word
409 if (!word.empty()) {
410 word += "#";
411 if (stem == "1") word += "s";
412 if (fold == "1") word += "i";
413 word += " ";
414 outtext += word;
415 }
416 return outtext;
417}
418
419
420// some query form parsing functions for use with mgpp & lucene
421
422void parse_reg_query_form(text_t &querystring, cgiargsclass &args, bool segment)
423{
424 querystring.clear();
425
426 int argct = args.getintarg("ct");
427 int argt = args.getintarg("t");// t=0 -and, t=1 - or
428 int argb = args.getintarg("b");
429
430 text_t combine;
431
432 // lucene uses global combine, so only need this for mgpp
433 if (argct==1) {
434 if (argt == 0) combine = "&";
435 else combine = "|";
436 }
437
438 text_t field = args["fqf"];
439 if (field.empty()) return; // no query
440 text_tarray fields;
441 splitchar(field.begin(), field.end(), ',', fields);
442
443 text_t value = args["fqv"];
444 if (value.empty()) return; // somethings wrong
445 text_tarray values;
446 splitchar(value.begin(), value.end(), ',', values);
447
448
449 for (int i=0; i< values.size(); ++i) {
450 if (!values[i].empty()) {
451 text_t this_value = values[i];
452 // remove operators for simple search, segments text if necessary
453 format_querystring(this_value, argb, segment);
454 // add tag info for this field (and other processing)
455 format_field_info(this_value, fields[i], argct, argt, argb);
456 // add into query string
457 if (argct == 2) {
458 // lucene
459 // we don't worry about AND/OR, cos this is done by defaultcombineoperator
460 querystring += this_value+" ";
461 } else {
462 // mgpp
463 if (!querystring.empty()) {
464 querystring += " "+ combine+ " ";
465 }
466 querystring += this_value;
467 }
468 }
469 }
470}
471
472
473void parse_adv_query_form(text_t &querystring, cgiargsclass &args, bool segment){
474 querystring.clear();
475
476 const int argct = args.getintarg("ct");
477 int argt = 0;// arg t is either not used (lucene) or used for natural/ranked (mgpp), so we set it to 0 = AND, by default
478 int argb = args.getintarg("b");
479 text_t combine;
480 if (argct==1) {
481 combine = "&";
482 }
483 else { // lucene
484 combine = "AND";
485 }
486
487 text_t field = args["fqf"];
488 if (field.empty()) return; // no query
489 text_tarray fields;
490 splitchar(field.begin(), field.end(), ',', fields);
491
492 text_t value = args["fqv"];
493 if (value.empty()) return; // somethings wrong
494 text_tarray values;
495 splitchar(value.begin(), value.end(), ',', values);
496
497 text_t comb = args["fqc"];
498 if (comb.empty()) return; //somethings wrong
499 text_tarray combs;
500 splitchar(comb.begin(), comb.end(), ',', combs);
501
502 text_tarray stems;
503 text_tarray folds;
504 if (argct == 1) {// mgpp - lucene doesn't do stem/case
505 text_t stem = args["fqs"];
506 if (stem.empty()) return; // somethings wrong
507 splitchar(stem.begin(), stem.end(), ',', stems);
508
509 text_t fold = args["fqk"];
510 if (fold.empty()) return; // somethings wrong
511 splitchar(fold.begin(), fold.end(), ',', folds);
512 }
513
514 for(int i=0; i< values.size(); ++i) {
515 if (!values[i].empty()) {
516 if (i!=0) {
517 if (argct==1) {
518 if (combs[i-1]=="and") combine = "&";
519 else if (combs[i-1]=="or")combine = "|";
520 else if (combs[i-1]=="not")combine = "!";
521 }
522 else { // lucene
523 if (combs[i-1]=="and") combine = "AND";
524 else if (combs[i-1]=="or")combine = "OR";
525 else if (combs[i-1]=="not")combine = "NOT";
526 }
527 }
528 text_t this_value = values[i];
529 // remove operators for simple search, segments text if necessary
530 format_querystring(this_value, argb, segment);
531 if (argct == 1) { // mgpp only
532 this_value = addstemcase(this_value, stems[i], folds[i], argct);
533 }
534 // add tag info for this field (and other processing)
535 format_field_info(this_value, fields[i], argct, argt, argb);
536 // add into query string
537 if (!querystring.empty()) {
538 querystring += " "+ combine+ " ";
539 }
540 querystring += this_value;
541
542 }
543 }
544}
545
546// Extended addqueryelem for Human Info project
547void addqueryelem_ex(text_t &querystring, const text_t &tag,
548 const text_t &terms, const text_t &stem,
549 const text_t &fold,
550 const text_t& combine, const text_t& word_combine) {
551
552 if (!querystring.empty()) { // have to put and/or
553 querystring += " " + combine + " ";
554 }
555 text_t outtext; outtext.reserve(512);
556 text_t word; word.reserve(100);
557 //unsigned short c;
558 text_t::const_iterator here = terms.begin();
559 text_t::const_iterator end = terms.end();
560 bool inquote = false, firstword = true;
561
562 text_t word2; word2.reserve(256);
563
564 while (here !=end) {
565 if (is_unicode_space(*here)) {
566 if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
567 else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
568 else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
569 else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
570 else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
571 if (inquote) {
572 word2.push_back(*here);
573 }
574 word.append(word2); word2.clear();
575
576 if (!inquote && !word.empty() ) {
577 // found word boundary
578
579 if (stem == "1" || fold =="1") {
580 word += "#";
581 if (stem == "1") word += "s";
582 //else word += "u";
583
584 if (fold == "1") word += "i";
585 //else word += "c";
586 }
587 if (firstword) {
588 firstword = false;
589 } else {
590 outtext += " " + word_combine + " ";
591 }
592 outtext += "[" + word + "]:"+tag;
593 word.clear();
594 }
595 ++here;
596 } else if (*here == '\"') {
597 word2.push_back(*here);
598 inquote = !inquote;
599 ++here;
600 } else {
601 // not word boundary
602 word2.push_back(*here);
603 ++here;
604 }
605 }
606
607 // get last word
608 if (!word2.empty()) {
609 if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
610 else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
611 else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
612 else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
613 else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
614 word.append(word2); word2.clear();
615
616 if (stem == "1"|| fold == "1") {
617 word += "#";
618 if (stem == "1") word += "s";
619 //else word += "u";
620
621 if (fold == "1") word += "i";
622 //else word += "c";
623 }
624 if (!outtext.empty()) outtext += " " + word_combine + " ";
625 outtext += "[" + word + "]:"+tag;
626 }
627 querystring += "(" + outtext + ")";
628}
629
630void add_field_info(text_t &querystring, const text_t &tag, int type) {
631
632 if (tag == "" || tag == "ZZ") return; // do nothing
633 if (type == 1) { //mgpp
634 querystring = "["+querystring+"]:"+tag;
635 } else if (type == 2) { // lucene
636 querystring = tag+":("+querystring+")";
637 }
638
639}
640
641
642void format_field_info_lucene(text_t &querystring, text_t tag, int argt, int argb) {
643 if (tag == "ZZ") tag = ""; // ZZ is a special tag meaning no tag (all fields)
644 int type = 2; //lucene
645
646 if (argb==0) { // simple
647 // there will be no & or | as they should have already been removed
648 // just tag the entire thing
649 if (tag != "") {
650 add_field_info(querystring, tag, type);
651 }
652 return;
653 }
654
655 // need to replace & with &&, | with ||
656 text_t::const_iterator here = querystring.begin();
657 text_t::const_iterator end = querystring.end();
658
659 text_t finalquery = "";
660 while (here != end) {
661 if (*here == '&') {
662 finalquery.push_back('&');
663 finalquery.push_back('&');
664 while (*(here+1) == '&') {
665 ++here;
666 }
667 }
668 else if (*here == '|') {
669 finalquery.push_back('|');
670 finalquery.push_back('|');
671 while (*(here+1) == '|') {
672 ++here;
673 }
674 }
675 else {
676 finalquery.push_back(*here);
677 }
678 ++here;
679 }
680 querystring = finalquery;
681 add_field_info(querystring, tag, type);
682}
683
684
685void format_field_info_mgpp(text_t &querystring, text_t tag, int argt, int argb) {
686
687 if (tag == "ZZ") tag = ""; // ZZ is a special tag meaning no tag (all fields)
688 if (tag == "" && argb == 1) {
689 return; // no field specifier, advanced mode, the query stays as written
690 }
691
692 int type = 1; // mgpp
693
694 bool simple_and = (argb==0 && argt==0);
695 text_t finalquery = "";
696 text_t fieldpart ="";
697 text_t queryelem = "";
698 bool in_phrase = false;
699 bool in_field = false;
700
701 text_t::const_iterator here = querystring.begin();
702 text_t::const_iterator end = querystring.end();
703 while (here != end) {
704 if (is_unicode_letdig(*here) || *here == '&' || is_special_character(type, *here)) {
705 queryelem.push_back(*here);
706 }
707 else if (*here == '|') {
708 in_field = false;
709 }
710 else if (*here == '!' || *here == '(' || *here == ')') {
711 if (!in_phrase) { // ignore these if in_phrase
712 // output field, then output operator
713 in_field = false;
714 if (!queryelem.empty()) {
715 if (!simple_and && !fieldpart.empty()) {
716 add_field_info(fieldpart, tag, type);
717 finalquery += fieldpart;
718 finalquery.push_back(' ');
719 fieldpart.clear();
720 }
721 fieldpart += queryelem;
722 }
723 if (!fieldpart.empty()) {
724 add_field_info(fieldpart, tag, type);
725 finalquery += fieldpart;
726 finalquery.push_back(' ');
727 }
728 fieldpart.clear();
729 queryelem.clear();
730 finalquery.push_back(*here);
731 finalquery.push_back(' ');
732 }
733 }
734 else if (*here == '"') {
735 queryelem.push_back(*here);
736 if (in_phrase == false) in_phrase = true;
737 else {
738 in_phrase = false;
739 }
740 }
741
742 // Found word boundary, in a phrase
743 else if (in_phrase) {
744 queryelem.push_back(*here);
745 }
746 // Found a word boundary
747 else {
748 if (!queryelem.empty()) {
749 if (queryelem == "&") {
750 in_field = true;
751 queryelem.clear();
752 }
753 else if (starts_with(queryelem, "NEAR") || starts_with(queryelem, "WITHIN")) {
754
755 if (argb==1) {
756 // simple search, these not allowed
757 in_field = true;
758 fieldpart += queryelem;
759 fieldpart.push_back(' ');
760 }
761 queryelem.clear();
762
763 }
764 else {
765 if (!simple_and && !in_field) {
766 if (!fieldpart.empty()) {
767 add_field_info(fieldpart, tag, type);
768 finalquery += fieldpart;
769 finalquery.push_back(' ');
770 fieldpart.clear();
771 }
772 }
773
774 fieldpart += queryelem;
775 fieldpart.push_back(' ');
776 queryelem.clear();
777 }
778 }
779 }
780 ++here;
781 }
782 // at the end
783 if (!queryelem.empty()) {
784 if (!simple_and && !in_field && !fieldpart.empty()) {
785 add_field_info(fieldpart, tag, type);
786 finalquery += fieldpart;
787 finalquery.push_back(' ');
788 fieldpart.clear();
789 }
790 fieldpart += queryelem;
791 }
792 if (!fieldpart.empty()) {
793 add_field_info(fieldpart, tag, type);
794 finalquery += fieldpart;
795 fieldpart.clear();
796 finalquery.push_back(' ');
797 }
798
799 querystring = finalquery;
800}
801
802
803void format_field_info(text_t &querystring, text_t tag, int argct, int argt, int argb) {
804 if (argct == 1) {
805 format_field_info_mgpp(querystring, tag, argt, argb);
806 } else if (argct == 2) {
807 format_field_info_lucene(querystring, tag, argt, argb);
808 }
809}
810
811void mgpp_adddateelem(text_t& querystring, const int date)
812{
813 querystring.appendcstr(" [");
814 if(date<0) {
815 querystring.appendcstr("bc");
816 querystring.appendint((date*-1));
817 }
818 else {
819 querystring.appendint(date);
820 }
821 querystring.appendcstr("]:CV");
822}
823
824void lucene_adddateelem(text_t& querystring, const int date)
825{
826 querystring.appendcstr(" CV:(");
827 if(date<0) {
828 querystring.appendcstr("bc");
829 querystring.appendint((date*-1));
830 }
831 else {
832 querystring.appendint(date);
833 }
834 querystring.appendcstr(")");
835}
836
837
838void add_dates(text_t &querystring, int startdate, int enddate,
839 int startbc, int endbc, int ct)
840{
841 if(startdate)
842 {
843 int querystringis = 0;
844 text_t::const_iterator here = querystring.begin();
845 text_t::const_iterator end = querystring.end();
846 while(here!=end)
847 {
848 if(!(isspace((*here)))){
849 here = end;
850 querystringis = 1;
851 }
852 else
853 ++here;
854 }
855 //converting BCE dates
856 if(startbc && startdate > 0)
857 {
858 startdate *= -1;
859 }
860 if(endbc && enddate > 0)
861 {
862 enddate *= -1;
863 }
864 if(enddate != 0 && enddate<startdate)
865 {
866 cout<<"enddate too small"<<endl;
867 return;
868 }
869 if(querystringis)
870 querystring.appendcstr(" AND");
871 if(!enddate)
872 {
873 if (ct==1) {
874 mgpp_adddateelem(querystring,startdate);
875 }
876 else { // lucene
877 lucene_adddateelem(querystring,startdate);
878 }
879 }
880 else{
881 int nextdate = startdate;
882 querystring.appendcstr(" (");
883 while(nextdate<=enddate)
884 {
885 if(nextdate!=0) {
886 if (ct==1) {
887 mgpp_adddateelem(querystring,nextdate);
888 }
889 else { // lucene
890 lucene_adddateelem(querystring,nextdate);
891 }
892 }
893 ++nextdate;
894 }
895 querystring.appendcstr(" )");
896 }
897 }
898
899}
Note: See TracBrowser for help on using the repository browser.