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
|
---|
31 | void 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
|
---|
96 | void 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
|
---|
113 | void 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 |
|
---|
195 | void 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 |
|
---|
250 | void 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 |
|
---|
271 | bool 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
|
---|
286 | void 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 >= 0x4e00 && *here <= 0x9fa5) ||
|
---|
308 | (*here >= 0xf900 && *here <= 0xfa2d)) {
|
---|
309 | // Chinese character
|
---|
310 | if (!space) formattedstring.push_back (0x200b); // zero width space
|
---|
311 | formattedstring.push_back (*here);
|
---|
312 | formattedstring.push_back (0x200b);
|
---|
313 | space = true;
|
---|
314 | } else {
|
---|
315 |
|
---|
316 | // non-Chinese character
|
---|
317 | formattedstring.push_back (*here);
|
---|
318 | space = false;
|
---|
319 |
|
---|
320 | }
|
---|
321 |
|
---|
322 | } else {
|
---|
323 | formattedstring.push_back (*here);
|
---|
324 | }
|
---|
325 | ++here;
|
---|
326 | }
|
---|
327 | querystring = formattedstring;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 |
|
---|
332 |
|
---|
333 | // search history tool
|
---|
334 | // also used for form query macros
|
---|
335 | text_t escape_quotes(const text_t &querystring) {
|
---|
336 |
|
---|
337 | text_t::const_iterator here = querystring.begin();
|
---|
338 | text_t::const_iterator end = querystring.end();
|
---|
339 |
|
---|
340 | text_t escquery = "";
|
---|
341 | while (here != end) {
|
---|
342 | if (*here != '\'' && *here != '\"' && *here != '\n' && *here != '\r') escquery.push_back(*here);
|
---|
343 | else if (*here == '\n' || *here == '\r') {
|
---|
344 | escquery.push_back(' ');
|
---|
345 | } else {
|
---|
346 | escquery +="\\\\";
|
---|
347 | escquery.push_back(*here);
|
---|
348 | }
|
---|
349 |
|
---|
350 | ++here;
|
---|
351 | }
|
---|
352 | return escquery;
|
---|
353 |
|
---|
354 | }
|
---|
355 |
|
---|
356 | // Parses the terms into words, and adds #si if necessary
|
---|
357 | text_t addstemcase(const text_t &terms, const text_t &stem, const text_t &fold,
|
---|
358 | const int indexer_type) {
|
---|
359 |
|
---|
360 | // 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.
|
---|
361 | if (stem == "0" && fold == "0") {
|
---|
362 | return terms;
|
---|
363 | }
|
---|
364 | // this is only for mgpp collections, shouldn't be called for anything else
|
---|
365 | if (indexer_type != 1) {
|
---|
366 | return terms;
|
---|
367 | }
|
---|
368 |
|
---|
369 | text_t outtext;
|
---|
370 | text_t word;
|
---|
371 |
|
---|
372 | text_t::const_iterator here = terms.begin();
|
---|
373 | text_t::const_iterator end = terms.end();
|
---|
374 |
|
---|
375 | while (here !=end) {
|
---|
376 |
|
---|
377 | if (is_unicode_letdig(*here) || is_special_character(indexer_type, *here)) {
|
---|
378 | // not word boundary
|
---|
379 | word.push_back(*here);
|
---|
380 | ++here;
|
---|
381 | }
|
---|
382 | else {
|
---|
383 | // found word boundary
|
---|
384 | if (!word.empty() ) {
|
---|
385 | if (starts_with(word, "NEAR") || starts_with(word, "WITHIN")) {
|
---|
386 | outtext += word;
|
---|
387 | word.clear();
|
---|
388 | }
|
---|
389 | else {
|
---|
390 | word += "#";
|
---|
391 | if (stem == "1") word += "s";
|
---|
392 | if (fold == "1") word += "i";
|
---|
393 | outtext += word;
|
---|
394 | word.clear();
|
---|
395 | }
|
---|
396 | }
|
---|
397 | // this only used in advanced form, so we leave in boolean operators
|
---|
398 | if (*here == '\"' || *here == '&' || *here == '|' || *here == '!' ||
|
---|
399 | *here == '(' || *here == ')' || is_unicode_space(*here)) {
|
---|
400 | outtext.push_back(*here);
|
---|
401 | }
|
---|
402 | ++here;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | // get last word
|
---|
407 | if (!word.empty()) {
|
---|
408 | word += "#";
|
---|
409 | if (stem == "1") word += "s";
|
---|
410 | if (fold == "1") word += "i";
|
---|
411 | word += " ";
|
---|
412 | outtext += word;
|
---|
413 | }
|
---|
414 | return outtext;
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | // some query form parsing functions for use with mgpp & lucene
|
---|
419 |
|
---|
420 | void parse_reg_query_form(text_t &querystring, cgiargsclass &args, bool segment)
|
---|
421 | {
|
---|
422 | querystring.clear();
|
---|
423 |
|
---|
424 | int argct = args.getintarg("ct");
|
---|
425 | int argt = args.getintarg("t");// t=0 -and, t=1 - or
|
---|
426 | int argb = args.getintarg("b");
|
---|
427 |
|
---|
428 | text_t combine;
|
---|
429 |
|
---|
430 | // lucene uses global combine, so only need this for mgpp
|
---|
431 | if (argct==1) {
|
---|
432 | if (argt == 0) combine = "&";
|
---|
433 | else combine = "|";
|
---|
434 | }
|
---|
435 |
|
---|
436 | text_t field = args["fqf"];
|
---|
437 | if (field.empty()) return; // no query
|
---|
438 | text_tarray fields;
|
---|
439 | splitchar(field.begin(), field.end(), ',', fields);
|
---|
440 |
|
---|
441 | text_t value = args["fqv"];
|
---|
442 | if (value.empty()) return; // somethings wrong
|
---|
443 | text_tarray values;
|
---|
444 | splitchar(value.begin(), value.end(), ',', values);
|
---|
445 |
|
---|
446 |
|
---|
447 | for (int i=0; i< values.size(); ++i) {
|
---|
448 | if (!values[i].empty()) {
|
---|
449 | text_t this_value = values[i];
|
---|
450 | // remove operators for simple search, segments text if necessary
|
---|
451 | format_querystring(this_value, argb, segment);
|
---|
452 | // add tag info for this field (and other processing)
|
---|
453 | format_field_info(this_value, fields[i], argct, argt, argb);
|
---|
454 | // add into query string
|
---|
455 | if (argct == 2) {
|
---|
456 | // lucene
|
---|
457 | // we don't worry about AND/OR, cos this is done by defaultcombineoperator
|
---|
458 | querystring += this_value+" ";
|
---|
459 | } else {
|
---|
460 | // mgpp
|
---|
461 | if (!querystring.empty()) {
|
---|
462 | querystring += " "+ combine+ " ";
|
---|
463 | }
|
---|
464 | querystring += this_value;
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | void parse_adv_query_form(text_t &querystring, cgiargsclass &args, bool segment){
|
---|
472 | querystring.clear();
|
---|
473 |
|
---|
474 | const int argct = args.getintarg("ct");
|
---|
475 | 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
|
---|
476 | int argb = args.getintarg("b");
|
---|
477 | text_t combine;
|
---|
478 | if (argct==1) {
|
---|
479 | combine = "&";
|
---|
480 | }
|
---|
481 | else { // lucene
|
---|
482 | combine = "AND";
|
---|
483 | }
|
---|
484 |
|
---|
485 | text_t field = args["fqf"];
|
---|
486 | if (field.empty()) return; // no query
|
---|
487 | text_tarray fields;
|
---|
488 | splitchar(field.begin(), field.end(), ',', fields);
|
---|
489 |
|
---|
490 | text_t value = args["fqv"];
|
---|
491 | if (value.empty()) return; // somethings wrong
|
---|
492 | text_tarray values;
|
---|
493 | splitchar(value.begin(), value.end(), ',', values);
|
---|
494 |
|
---|
495 | text_t comb = args["fqc"];
|
---|
496 | if (comb.empty()) return; //somethings wrong
|
---|
497 | text_tarray combs;
|
---|
498 | splitchar(comb.begin(), comb.end(), ',', combs);
|
---|
499 |
|
---|
500 | text_tarray stems;
|
---|
501 | text_tarray folds;
|
---|
502 | if (argct == 1) {// mgpp - lucene doesn't do stem/case
|
---|
503 | text_t stem = args["fqs"];
|
---|
504 | if (stem.empty()) return; // somethings wrong
|
---|
505 | splitchar(stem.begin(), stem.end(), ',', stems);
|
---|
506 |
|
---|
507 | text_t fold = args["fqk"];
|
---|
508 | if (fold.empty()) return; // somethings wrong
|
---|
509 | splitchar(fold.begin(), fold.end(), ',', folds);
|
---|
510 | }
|
---|
511 |
|
---|
512 | for(int i=0; i< values.size(); ++i) {
|
---|
513 | if (!values[i].empty()) {
|
---|
514 | if (i!=0) {
|
---|
515 | if (argct==1) {
|
---|
516 | if (combs[i-1]=="and") combine = "&";
|
---|
517 | else if (combs[i-1]=="or")combine = "|";
|
---|
518 | else if (combs[i-1]=="not")combine = "!";
|
---|
519 | }
|
---|
520 | else { // lucene
|
---|
521 | if (combs[i-1]=="and") combine = "AND";
|
---|
522 | else if (combs[i-1]=="or")combine = "OR";
|
---|
523 | else if (combs[i-1]=="not")combine = "NOT";
|
---|
524 | }
|
---|
525 | }
|
---|
526 | text_t this_value = values[i];
|
---|
527 | // remove operators for simple search, segments text if necessary
|
---|
528 | format_querystring(this_value, argb, segment);
|
---|
529 | if (argct == 1) { // mgpp only
|
---|
530 | this_value = addstemcase(this_value, stems[i], folds[i], argct);
|
---|
531 | }
|
---|
532 | // add tag info for this field (and other processing)
|
---|
533 | format_field_info(this_value, fields[i], argct, argt, argb);
|
---|
534 | // add into query string
|
---|
535 | if (!querystring.empty()) {
|
---|
536 | querystring += " "+ combine+ " ";
|
---|
537 | }
|
---|
538 | querystring += this_value;
|
---|
539 |
|
---|
540 | }
|
---|
541 | }
|
---|
542 | }
|
---|
543 |
|
---|
544 | // Extended addqueryelem for Human Info project
|
---|
545 | void addqueryelem_ex(text_t &querystring, const text_t &tag,
|
---|
546 | const text_t &terms, const text_t &stem,
|
---|
547 | const text_t &fold,
|
---|
548 | const text_t& combine, const text_t& word_combine) {
|
---|
549 |
|
---|
550 | if (!querystring.empty()) { // have to put and/or
|
---|
551 | querystring += " " + combine + " ";
|
---|
552 | }
|
---|
553 | text_t outtext; outtext.reserve(512);
|
---|
554 | text_t word; word.reserve(100);
|
---|
555 | //unsigned short c;
|
---|
556 | text_t::const_iterator here = terms.begin();
|
---|
557 | text_t::const_iterator end = terms.end();
|
---|
558 | bool inquote = false, firstword = true;
|
---|
559 |
|
---|
560 | text_t word2; word2.reserve(256);
|
---|
561 |
|
---|
562 | while (here !=end) {
|
---|
563 | if (is_unicode_space(*here)) {
|
---|
564 | if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
|
---|
565 | else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
|
---|
566 | else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
|
---|
567 | else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
|
---|
568 | else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
|
---|
569 | if (inquote) {
|
---|
570 | word2.push_back(*here);
|
---|
571 | }
|
---|
572 | word.append(word2); word2.clear();
|
---|
573 |
|
---|
574 | if (!inquote && !word.empty() ) {
|
---|
575 | // found word boundary
|
---|
576 |
|
---|
577 | if (stem == "1" || fold =="1") {
|
---|
578 | word += "#";
|
---|
579 | if (stem == "1") word += "s";
|
---|
580 | //else word += "u";
|
---|
581 |
|
---|
582 | if (fold == "1") word += "i";
|
---|
583 | //else word += "c";
|
---|
584 | }
|
---|
585 | if (firstword) {
|
---|
586 | firstword = false;
|
---|
587 | } else {
|
---|
588 | outtext += " " + word_combine + " ";
|
---|
589 | }
|
---|
590 | outtext += "[" + word + "]:"+tag;
|
---|
591 | word.clear();
|
---|
592 | }
|
---|
593 | ++here;
|
---|
594 | } else if (*here == '\"') {
|
---|
595 | word2.push_back(*here);
|
---|
596 | inquote = !inquote;
|
---|
597 | ++here;
|
---|
598 | } else {
|
---|
599 | // not word boundary
|
---|
600 | word2.push_back(*here);
|
---|
601 | ++here;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | // get last word
|
---|
606 | if (!word2.empty()) {
|
---|
607 | if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
|
---|
608 | else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
|
---|
609 | else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
|
---|
610 | else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
|
---|
611 | else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
|
---|
612 | word.append(word2); word2.clear();
|
---|
613 |
|
---|
614 | if (stem == "1"|| fold == "1") {
|
---|
615 | word += "#";
|
---|
616 | if (stem == "1") word += "s";
|
---|
617 | //else word += "u";
|
---|
618 |
|
---|
619 | if (fold == "1") word += "i";
|
---|
620 | //else word += "c";
|
---|
621 | }
|
---|
622 | if (!outtext.empty()) outtext += " " + word_combine + " ";
|
---|
623 | outtext += "[" + word + "]:"+tag;
|
---|
624 | }
|
---|
625 | querystring += "(" + outtext + ")";
|
---|
626 | }
|
---|
627 |
|
---|
628 | void add_field_info(text_t &querystring, const text_t &tag, int type) {
|
---|
629 |
|
---|
630 | if (tag == "" || tag == "ZZ") return; // do nothing
|
---|
631 | if (type == 1) { //mgpp
|
---|
632 | querystring = "["+querystring+"]:"+tag;
|
---|
633 | } else if (type == 2) { // lucene
|
---|
634 | querystring = tag+":("+querystring+")";
|
---|
635 | }
|
---|
636 |
|
---|
637 | }
|
---|
638 |
|
---|
639 |
|
---|
640 | void format_field_info_lucene(text_t &querystring, text_t tag, int argt, int argb) {
|
---|
641 | if (tag == "ZZ") tag = ""; // ZZ is a special tag meaning no tag (all fields)
|
---|
642 | int type = 2; //lucene
|
---|
643 |
|
---|
644 | if (argb==0) { // simple
|
---|
645 | // there will be no & or | as they should have already been removed
|
---|
646 | // just tag the entire thing
|
---|
647 | if (tag != "") {
|
---|
648 | add_field_info(querystring, tag, type);
|
---|
649 | }
|
---|
650 | return;
|
---|
651 | }
|
---|
652 |
|
---|
653 | // need to replace & with &&, | with ||
|
---|
654 | text_t::const_iterator here = querystring.begin();
|
---|
655 | text_t::const_iterator end = querystring.end();
|
---|
656 |
|
---|
657 | text_t finalquery = "";
|
---|
658 | while (here != end) {
|
---|
659 | if (*here == '&') {
|
---|
660 | finalquery.push_back('&');
|
---|
661 | finalquery.push_back('&');
|
---|
662 | while (*(here+1) == '&') {
|
---|
663 | ++here;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | else if (*here == '|') {
|
---|
667 | finalquery.push_back('|');
|
---|
668 | finalquery.push_back('|');
|
---|
669 | while (*(here+1) == '|') {
|
---|
670 | ++here;
|
---|
671 | }
|
---|
672 | }
|
---|
673 | else {
|
---|
674 | finalquery.push_back(*here);
|
---|
675 | }
|
---|
676 | ++here;
|
---|
677 | }
|
---|
678 | querystring = finalquery;
|
---|
679 | add_field_info(querystring, tag, type);
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | void format_field_info_mgpp(text_t &querystring, text_t tag, int argt, int argb) {
|
---|
684 |
|
---|
685 | if (tag == "ZZ") tag = ""; // ZZ is a special tag meaning no tag (all fields)
|
---|
686 | if (tag == "" && argb == 1) {
|
---|
687 | return; // no field specifier, advanced mode, the query stays as written
|
---|
688 | }
|
---|
689 |
|
---|
690 | int type = 1; // mgpp
|
---|
691 |
|
---|
692 | bool simple_and = (argb==0 && argt==0);
|
---|
693 | text_t finalquery = "";
|
---|
694 | text_t fieldpart ="";
|
---|
695 | text_t queryelem = "";
|
---|
696 | bool in_phrase = false;
|
---|
697 | bool in_field = false;
|
---|
698 |
|
---|
699 | text_t::const_iterator here = querystring.begin();
|
---|
700 | text_t::const_iterator end = querystring.end();
|
---|
701 | while (here != end) {
|
---|
702 | if (is_unicode_letdig(*here) || *here == '&' || is_special_character(type, *here)) {
|
---|
703 | queryelem.push_back(*here);
|
---|
704 | }
|
---|
705 | else if (*here == '|') {
|
---|
706 | in_field = false;
|
---|
707 | }
|
---|
708 | else if (*here == '!' || *here == '(' || *here == ')') {
|
---|
709 | if (!in_phrase) { // ignore these if in_phrase
|
---|
710 | // output field, then output operator
|
---|
711 | in_field = false;
|
---|
712 | if (!queryelem.empty()) {
|
---|
713 | if (!simple_and && !fieldpart.empty()) {
|
---|
714 | add_field_info(fieldpart, tag, type);
|
---|
715 | finalquery += fieldpart;
|
---|
716 | finalquery.push_back(' ');
|
---|
717 | fieldpart.clear();
|
---|
718 | }
|
---|
719 | fieldpart += queryelem;
|
---|
720 | }
|
---|
721 | if (!fieldpart.empty()) {
|
---|
722 | add_field_info(fieldpart, tag, type);
|
---|
723 | finalquery += fieldpart;
|
---|
724 | finalquery.push_back(' ');
|
---|
725 | }
|
---|
726 | fieldpart.clear();
|
---|
727 | queryelem.clear();
|
---|
728 | finalquery.push_back(*here);
|
---|
729 | finalquery.push_back(' ');
|
---|
730 | }
|
---|
731 | }
|
---|
732 | else if (*here == '"') {
|
---|
733 | queryelem.push_back(*here);
|
---|
734 | if (in_phrase == false) in_phrase = true;
|
---|
735 | else {
|
---|
736 | in_phrase = false;
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | // Found word boundary, in a phrase
|
---|
741 | else if (in_phrase) {
|
---|
742 | queryelem.push_back(*here);
|
---|
743 | }
|
---|
744 | // Found a word boundary
|
---|
745 | else {
|
---|
746 | if (!queryelem.empty()) {
|
---|
747 | if (queryelem == "&") {
|
---|
748 | in_field = true;
|
---|
749 | queryelem.clear();
|
---|
750 | }
|
---|
751 | else if (starts_with(queryelem, "NEAR") || starts_with(queryelem, "WITHIN")) {
|
---|
752 |
|
---|
753 | if (argb==1) {
|
---|
754 | // simple search, these not allowed
|
---|
755 | in_field = true;
|
---|
756 | fieldpart += queryelem;
|
---|
757 | fieldpart.push_back(' ');
|
---|
758 | }
|
---|
759 | queryelem.clear();
|
---|
760 |
|
---|
761 | }
|
---|
762 | else {
|
---|
763 | if (!simple_and && !in_field) {
|
---|
764 | if (!fieldpart.empty()) {
|
---|
765 | add_field_info(fieldpart, tag, type);
|
---|
766 | finalquery += fieldpart;
|
---|
767 | finalquery.push_back(' ');
|
---|
768 | fieldpart.clear();
|
---|
769 | }
|
---|
770 | }
|
---|
771 |
|
---|
772 | fieldpart += queryelem;
|
---|
773 | fieldpart.push_back(' ');
|
---|
774 | queryelem.clear();
|
---|
775 | }
|
---|
776 | }
|
---|
777 | }
|
---|
778 | ++here;
|
---|
779 | }
|
---|
780 | // at the end
|
---|
781 | if (!queryelem.empty()) {
|
---|
782 | if (!simple_and && !in_field && !fieldpart.empty()) {
|
---|
783 | add_field_info(fieldpart, tag, type);
|
---|
784 | finalquery += fieldpart;
|
---|
785 | finalquery.push_back(' ');
|
---|
786 | fieldpart.clear();
|
---|
787 | }
|
---|
788 | fieldpart += queryelem;
|
---|
789 | }
|
---|
790 | if (!fieldpart.empty()) {
|
---|
791 | add_field_info(fieldpart, tag, type);
|
---|
792 | finalquery += fieldpart;
|
---|
793 | fieldpart.clear();
|
---|
794 | finalquery.push_back(' ');
|
---|
795 | }
|
---|
796 |
|
---|
797 | querystring = finalquery;
|
---|
798 | }
|
---|
799 |
|
---|
800 |
|
---|
801 | void format_field_info(text_t &querystring, text_t tag, int argct, int argt, int argb) {
|
---|
802 | if (argct == 1) {
|
---|
803 | format_field_info_mgpp(querystring, tag, argt, argb);
|
---|
804 | } else if (argct == 2) {
|
---|
805 | format_field_info_lucene(querystring, tag, argt, argb);
|
---|
806 | }
|
---|
807 | }
|
---|
808 |
|
---|
809 | void mgpp_adddateelem(text_t& querystring, const int date)
|
---|
810 | {
|
---|
811 | querystring.appendcstr(" [");
|
---|
812 | if(date<0) {
|
---|
813 | querystring.appendcstr("bc");
|
---|
814 | querystring.appendint((date*-1));
|
---|
815 | }
|
---|
816 | else {
|
---|
817 | querystring.appendint(date);
|
---|
818 | }
|
---|
819 | querystring.appendcstr("]:CV");
|
---|
820 | }
|
---|
821 |
|
---|
822 | void lucene_adddateelem(text_t& querystring, const int date)
|
---|
823 | {
|
---|
824 | querystring.appendcstr(" CV:(");
|
---|
825 | if(date<0) {
|
---|
826 | querystring.appendcstr("bc");
|
---|
827 | querystring.appendint((date*-1));
|
---|
828 | }
|
---|
829 | else {
|
---|
830 | querystring.appendint(date);
|
---|
831 | }
|
---|
832 | querystring.appendcstr(")");
|
---|
833 | }
|
---|
834 |
|
---|
835 |
|
---|
836 | void add_dates(text_t &querystring, int startdate, int enddate,
|
---|
837 | int startbc, int endbc, int ct)
|
---|
838 | {
|
---|
839 | if(startdate)
|
---|
840 | {
|
---|
841 | int querystringis = 0;
|
---|
842 | text_t::const_iterator here = querystring.begin();
|
---|
843 | text_t::const_iterator end = querystring.end();
|
---|
844 | while(here!=end)
|
---|
845 | {
|
---|
846 | if(!(isspace((*here)))){
|
---|
847 | here = end;
|
---|
848 | querystringis = 1;
|
---|
849 | }
|
---|
850 | else
|
---|
851 | ++here;
|
---|
852 | }
|
---|
853 | //converting BCE dates
|
---|
854 | if(startbc && startdate > 0)
|
---|
855 | {
|
---|
856 | startdate *= -1;
|
---|
857 | }
|
---|
858 | if(endbc && enddate > 0)
|
---|
859 | {
|
---|
860 | enddate *= -1;
|
---|
861 | }
|
---|
862 | if(enddate != 0 && enddate<startdate)
|
---|
863 | {
|
---|
864 | cout<<"enddate too small"<<endl;
|
---|
865 | return;
|
---|
866 | }
|
---|
867 | if(querystringis)
|
---|
868 | querystring.appendcstr(" AND");
|
---|
869 | if(!enddate)
|
---|
870 | {
|
---|
871 | if (ct==1) {
|
---|
872 | mgpp_adddateelem(querystring,startdate);
|
---|
873 | }
|
---|
874 | else { // lucene
|
---|
875 | lucene_adddateelem(querystring,startdate);
|
---|
876 | }
|
---|
877 | }
|
---|
878 | else{
|
---|
879 | int nextdate = startdate;
|
---|
880 | querystring.appendcstr(" (");
|
---|
881 | while(nextdate<=enddate)
|
---|
882 | {
|
---|
883 | if(nextdate!=0) {
|
---|
884 | if (ct==1) {
|
---|
885 | mgpp_adddateelem(querystring,nextdate);
|
---|
886 | }
|
---|
887 | else { // lucene
|
---|
888 | lucene_adddateelem(querystring,nextdate);
|
---|
889 | }
|
---|
890 | }
|
---|
891 | ++nextdate;
|
---|
892 | }
|
---|
893 | querystring.appendcstr(" )");
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | }
|
---|