1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
//
// qtest.cc
//
// qtest: A program to test the Query classes as replacement for the current
// parsing code
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: qtest.cc,v 1.5 2004/05/28 13:15:25 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "cgi.h"
#include "defaults.h"
#include "WordContext.h"
#ifdef HAVE_STD
#include <iostream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#endif /* HAVE_STD */
#include "QueryParser.h"
#include "Query.h"
#include "ResultList.h"
#include "Exact.h"
#include "Accents.h"
#include "Prefix.h"
#include "WordSearcher.h"
#include "OrFuzzyExpander.h"
#include "ExactWordQuery.h"
#include "OrQueryParser.h"
#include "AndQueryParser.h"
#include "BooleanQueryParser.h"
#include "GParser.h"
// If we have this, we probably want it.
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
void reportError(char *msg);
void usage();
int debug = 0;
void
ParseAndGet(QueryParser &parser, const String &string);
//*****************************************************************************
// int main()
//
int
main(int ac, char **av)
{
int c;
extern char *optarg;
int override_config=0;
String configFile = DEFAULT_CONFIG_FILE;
String logicalWords;
bool doall = true,
doand = false,
door = false,
dobool = false,
dogeoffs = false;
//
// Parse command line arguments
//
while ((c = getopt(ac, av, "c:dvkaobg")) != -1)
{
switch (c)
{
case 'c':
configFile = optarg;
override_config = 1;
break;
case 'v':
debug++;
break;
case 'd':
debug++;
break;
case 'a':
doall = false;
doand = true;
break;
case 'o':
doall = false;
door = true;
break;
case 'b':
doall = false;
dobool = true;
break;
case 'g':
doall = false;
dogeoffs = true;
break;
case '?':
usage();
break;
}
}
//
// Parse the CGI parameters.
//
char none[] = "";
cgi input(optind < ac ? av[optind] : none);
String originalWords = input["words"];
originalWords.chop(" \t\r\n");
HtConfiguration* config= HtConfiguration::config();
// Set up the config
config->Defaults(&defaults[0]);
if (access((char*)configFile, R_OK) < 0)
{
reportError(form("Unable to find configuration file '%s'",
configFile.get()));
}
config->Read(configFile);
// Initialize htword library (key description + wordtype...)
WordContext::Initialize(*config);
OrFuzzyExpander exp;
Exact exact(*config);
exact.setWeight(1.0);
exact.openIndex();
exp.Add(&exact);
Accents accents(*config);
accents.setWeight(0.7);
accents.openIndex();
exp.Add(&accents);
Prefix prefix(*config);
prefix.setWeight(0.7);
prefix.openIndex();
exp.Add(&prefix);
QueryParser::SetFuzzyExpander(&exp);
WordSearcher searcher(config->Find("word_db"));
ExactWordQuery::SetSearcher(&searcher);
// -- put here your prefered cache
//QueryCache *cache = new XXX;
//Query::SetCache(cache);
OrQueryParser o;
BooleanQueryParser b;
GParser g;
AndQueryParser a;
if(doall || doand)
{
cout << "Trying and..." << endl;
ParseAndGet(a, originalWords);
}
if(doall || door)
{
cout << "Trying or..." << endl;
ParseAndGet(o, originalWords);
}
if(doall || dobool)
{
cout << "Trying boolean..." << endl;
ParseAndGet(b, originalWords);
}
if(doall || dogeoffs)
{
cout << "Trying no-precedence-boolean..." << endl;
ParseAndGet(g, originalWords);
}
}
void
ParseAndGet(QueryParser &parser, const String &query)
{
Query *q = parser.Parse(query);
if(q)
{
cout << "Parsed: " << q->GetLogicalWords() << endl;
ResultList *l = q->GetResults();
if(l)
{
cout << "Evaluated with " << l->Count() << " matches" << endl;
if(debug) l->Dump();
}
else
{
cout << "No matches" << endl;;
}
}
else
{
cerr << "syntax error: " << flush << parser.Error() << endl;
}
delete q;
}
//*****************************************************************************
// void usage()
// Display program usage information--assumes we're running from a cmd line
//
void usage()
{
cout << "usage: qtest [-a][-o][-b][-g][-v][-d][-c configfile]\n";
cout << "This program is part of ht://Dig " << VERSION << "\n\n";
cout << "Options:\n";
cout << "\t-v -d\tVerbose mode. This increases the verbosity of the\n";
cout << "\t\tprogram. Using more than 2 is probably only useful\n";
cout << "\t\tfor debugging purposes. The default verbose mode\n";
cout << "\t\tgives a progress on what it is doing and where it is.\n\n";
cout << "\t-c configfile\n";
cout << "\t\tUse the specified configuration file instead on the\n";
cout << "\t\tdefault.\n\n";
cout << "\t-a\tPerform only and/all parsing\n\n";
cout << "\t-o\tPerform only or/any parsing\n\n";
cout << "\t-b\tPerform only boolean parsing\n\n";
cout << "\t-g\tPerform only no-precedence-boolean parsing\n\n";
exit(0);
}
//*****************************************************************************
// Report an error and die
//
void reportError(char *msg)
{
cout << "qtest: " << msg << "\n\n";
exit(1);
}
|