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

Last change on this file since 1861 was 1774, checked in by kjm18, 23 years ago

added MatchMode in set_queryfilter_options - used in mgppqueryfilter. for
now just the same as QueryType.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 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
29// request.filterResultOptions and request.fields (if required) should
30// be set from the calling code
31void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring,
32 cgiargsclass &args) {
33
34 request.filterName = "QueryFilter";
35
36 OptionValue_t option;
37
38 option.name = "Term";
39 option.value = querystring;
40 request.filterOptions.push_back (option);
41
42 option.name = "QueryType";
43 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
44 request.filterOptions.push_back (option);
45
46 option.name = "MatchMode";
47 option.value = (args.getintarg("t")) ? "some" : "all";
48 request.filterOptions.push_back (option);
49
50 option.name = "Casefold";
51 option.value = (args.getintarg("k")) ? "true" : "false";
52 request.filterOptions.push_back (option);
53
54 option.name = "Stem";
55 option.value = (args.getintarg("s")) ? "true" : "false";
56 request.filterOptions.push_back (option);
57
58 if (!args["h"].empty()) {
59 option.name = "Index";
60 option.value = args["h"];
61 request.filterOptions.push_back (option);
62 }
63
64 if (!args["j"].empty()) {
65 option.name = "Subcollection";
66 option.value = args["j"];
67 request.filterOptions.push_back (option);
68 }
69
70 if (!args["n"].empty()) {
71 option.name = "Language";
72 option.value = args["n"];
73 request.filterOptions.push_back (option);
74 }
75
76 if (!args["g"].empty()) { // granularity for mgpp
77 option.name = "Level";
78 option.value = args["g"];
79 request.filterOptions.push_back (option);
80 }
81
82 set_more_queryfilter_options (request, args);
83}
84
85void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring1,
86 const text_t &querystring2, cgiargsclass &args) {
87
88 set_queryfilter_options (request, querystring1, args);
89
90 // fill in the second query if needed
91 if (!args["cq2"].empty()) {
92 OptionValue_t option;
93
94 option.name = "CombineQuery";
95 option.value = args["cq2"];
96 request.filterOptions.push_back (option);
97
98 option.name = "Term";
99 option.value = querystring2;
100 request.filterOptions.push_back (option);
101
102 option.name = "QueryType";
103 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
104 request.filterOptions.push_back (option);
105
106 option.name = "Casefold";
107 option.value = (args.getintarg("k")) ? "true" : "false";
108 request.filterOptions.push_back (option);
109
110 option.name = "Stem";
111 option.value = (args.getintarg("s")) ? "true" : "false";
112 request.filterOptions.push_back (option);
113
114 if (!args["h2"].empty()) {
115 option.name = "Index";
116 option.value = args["h2"];
117 request.filterOptions.push_back (option);
118 }
119
120 if (!args["j2"].empty()) {
121 option.name = "Subcollection";
122 option.value = args["j2"];
123 request.filterOptions.push_back (option);
124 }
125
126 if (!args["n2"].empty()) {
127 option.name = "Language";
128 option.value = args["n2"];
129 request.filterOptions.push_back (option);
130 }
131 }
132 set_more_queryfilter_options (request, args);
133}
134
135void set_more_queryfilter_options (FilterRequest_t &request, cgiargsclass &args) {
136
137 OptionValue_t option;
138 int arg_m = args.getintarg("m");
139
140 option.name = "Maxdocs";
141 option.value = arg_m;
142 request.filterOptions.push_back (option);
143
144 // option.name = "StartResults";
145 // option.value = args["r"];
146 // request.filterOptions.push_back (option);
147
148 // option.name = "EndResults";
149 // int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
150 // if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
151 // option.value = endresults;
152 // request.filterOptions.push_back (option);
153}
154
155void format_querystring (text_t &querystring, int querymode) {
156 text_t formattedstring;
157
158 text_t::const_iterator here = querystring.begin();
159 text_t::const_iterator end = querystring.end();
160
161 // space is used to insert spaces between Chinese
162 // characters. No space is needed before the first
163 // Chinese character.
164 bool space = false;
165
166 // want to remove ()|!& from querystring so boolean queries are just
167 // "all the words" queries (unless querymode is advanced)
168 while (here != end) {
169 if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
170 *here == '!' || *here == '&')) {
171 formattedstring.push_back(' ');
172 } else {
173 if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
174 (*here >= 0xf900 && *here <= 0xfa2d)) {
175 // Chinese character
176 if (space) formattedstring.push_back (0x200b);
177 formattedstring.push_back (*here);
178 formattedstring.push_back (0x200b);
179 space = true;
180 } else {
181 // non-Chinese character
182 formattedstring.push_back (*here);
183 space = false;
184 }
185 }
186 here ++;
187 }
188 querystring = formattedstring;
189}
190
191void add_ands(text_t& querystring, int querytype)
192{
193
194 if(querytype==0)
195 {
196 text_t formattedstring;
197
198 bool spacelast = false;
199
200 text_t::const_iterator here = querystring.begin();
201 text_t::const_iterator end = querystring.end();
202
203
204 querystring = formattedstring;
205
206 while(here!=end && isspace((*here)))
207 here++;
208
209 if(here==end)
210 return;
211
212 while(here!=end)
213 {
214 if(isspace((*here)))
215 {
216 while(here!=end && isspace((*here)))
217 here++;
218 if(here!=end)
219 formattedstring.appendcstr(" AND ");
220 }
221 if(!isspace((*here)))
222 {
223 formattedstring.push_back((*here));
224 }
225 here++;
226 }
227 querystring = formattedstring;
228 }
229}
230
231
232
233void add_dates(text_t &querystring, int startdate, int enddate,
234 int startbc, int endbc)
235{
236 if(startdate)
237 {
238 int querystringis = 0;
239 text_t::const_iterator here = querystring.begin();
240 text_t::const_iterator end = querystring.end();
241 while(here!=end)
242 {
243 if(!(isspace((*here)))){
244 here = end;
245 querystringis = 1;
246 }
247 else
248 here++;
249 }
250 //converting BCE dates
251 if(startbc && startdate > 0)
252 {
253 startdate *= -1;
254 }
255 if(endbc && enddate > 0)
256 {
257 enddate *= -1;
258 }
259 if(enddate != 0 && enddate<startdate)
260 {
261 cout<<"enddate too small"<<endl;
262 return;
263 }
264 if(querystringis)
265 querystring.appendcstr(" AND");
266 if(!enddate)
267 {
268 querystring.appendcstr(" [");
269 if(startdate<0)
270 {
271 querystring.appendcstr("bc");
272 querystring.appendint((startdate*-1));
273 }
274 else
275 querystring.appendint(startdate);
276 querystring.appendcstr("]:Coverage");
277 }
278 else{
279 int nextdate = startdate;
280 querystring.appendcstr(" (");
281 while(nextdate<=enddate)
282 {
283 if(nextdate!=0)
284 {
285 querystring.appendcstr(" [");
286 if(nextdate<0)
287 {
288 querystring.appendcstr("bc");
289 querystring.appendint((nextdate*-1));
290 }
291 else
292 querystring.appendint(nextdate);
293 querystring.appendcstr("]:Coverage");
294 }
295 nextdate++;
296 }
297 querystring.appendcstr(" )");
298 }
299 }
300
301}
302
303void get_phrases (const text_t &querystring, text_tarray &phrases) {
304
305 phrases.erase (phrases.begin(), phrases.end());
306 if (!querystring.empty()) {
307
308 text_t::const_iterator end = querystring.end();
309 text_t::const_iterator here = findchar (querystring.begin(), end, '"');
310 if (here != end) {
311 text_t tmptext;
312 bool foundquote = false;
313 while (here != end) {
314 if (*here == '"') {
315 if (foundquote) {
316 if (!tmptext.empty()) {
317 phrases.push_back(tmptext);
318 tmptext.clear();
319 }
320 foundquote = false;
321 } else foundquote = true;
322 } else {
323 if (foundquote) tmptext.push_back (*here);
324 }
325 here ++;
326 }
327 }
328 }
329}
Note: See TracBrowser for help on using the repository browser.