1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * Queryer.cpp -- simple interactive query program
|
---|
4 | * Copyright (C) 1999 Rodger McNab
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | **************************************************************************/
|
---|
21 |
|
---|
22 | #define _XOPEN_SOURCE 1
|
---|
23 | #define _XOPEN_SOURCE_EXTENDED 1
|
---|
24 |
|
---|
25 | /* getopt is in posix.2, so cygwin should have it in unistd, but doesn't */
|
---|
26 | #if defined (__WIN32__) || defined (__CYGWIN__)
|
---|
27 | # include "getopt_old.h"
|
---|
28 | #else
|
---|
29 | # include <unistd.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include "MGQuery.h"
|
---|
33 | #include "TextGet.h"
|
---|
34 |
|
---|
35 | #include "messages.h"
|
---|
36 | #include "mg_files.h"
|
---|
37 |
|
---|
38 | #include "GSDLQueryParser.h"
|
---|
39 |
|
---|
40 | void printHelp() {
|
---|
41 |
|
---|
42 | cout << "commands available are:\n"
|
---|
43 | << "\t.q\t\tquit\n"
|
---|
44 | << "\t.h\t\tprint the help message\n"
|
---|
45 | << "\t.i\t\tchange the search level (enter the new level at the prompt)\n"
|
---|
46 | << "\t.l\t\tchange the result level ( \"\" \"\" )\n"
|
---|
47 | << "\t.b\t\tfull text browse (enter a word or fragment at the prompt)\n"
|
---|
48 | << "\t.r0/.r1\t\tranking off/on\n"
|
---|
49 | << "\t.t0/.t1\t\tquery type some/all\n"
|
---|
50 | << "\t.c0/.c1\t\tcasefolding off/on\n"
|
---|
51 | << "\t.s0/.s1\t\tstemming off/on\n"
|
---|
52 | << "\t.o0/.o1\t\tshort output off/on\n"
|
---|
53 | << "\t.m\t\tset maxnumeric (enter the number at the prompt)\n\n"
|
---|
54 | << "\t.p\t\tprint a document (enter the docnum at the prompt)\n"
|
---|
55 | << "otherwise just enter a query\n\n";
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | int main (int argc, char **argv) {
|
---|
60 | int ch;
|
---|
61 | char *textfilename = "";
|
---|
62 | char *indexfilename = "";
|
---|
63 | char *basePath = "";
|
---|
64 |
|
---|
65 | opterr = 0;
|
---|
66 | msg_prefix = argv[0];
|
---|
67 |
|
---|
68 | // process the command line arguments
|
---|
69 | while ((ch = getopt (argc, argv, "f:t:d:h")) != -1) {
|
---|
70 | switch (ch) {
|
---|
71 | case 'f': /* input file */
|
---|
72 | indexfilename = optarg;
|
---|
73 | break;
|
---|
74 | case 't':
|
---|
75 | textfilename = optarg;
|
---|
76 | break;
|
---|
77 | case 'd':
|
---|
78 | basePath = optarg;
|
---|
79 | set_basepath (basePath);
|
---|
80 | break;
|
---|
81 | case 'h':
|
---|
82 | case '?':
|
---|
83 | fprintf (stderr, "usage: %s [-h] [-d directory] -f indexname -t textname\n", argv[0]);
|
---|
84 | exit (1);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (textfilename[0] == '\0' || indexfilename[0] == '\0') {
|
---|
89 | FatalError (1, "Index and text file names must be specified with -f and -t \n");
|
---|
90 | }
|
---|
91 |
|
---|
92 | // init the text system
|
---|
93 | TextData textData;
|
---|
94 | if (!textData.LoadData (basePath, textfilename)) {
|
---|
95 | FatalError (1, "Couldn't load text information for \"%s\"", textfilename);
|
---|
96 | }
|
---|
97 |
|
---|
98 | // init the query system
|
---|
99 | IndexData indexData;
|
---|
100 | if (!indexData.LoadData (basePath, indexfilename)) {
|
---|
101 | FatalError (1, "Couldn't load index information for \"%s\"", indexfilename);
|
---|
102 | }
|
---|
103 |
|
---|
104 | int maxnumeric = 4;
|
---|
105 |
|
---|
106 | // debug output
|
---|
107 | cerr << "num docs: "<<indexData.bdh.num_docs
|
---|
108 | << "\nnum frags: "<<indexData.bdh.num_frags
|
---|
109 | << "\nnum words: "<<indexData.bdh.num_words
|
---|
110 | << "\ntotal bytes: "<<indexData.bdh.total_bytes
|
---|
111 | << "\nindex string bytes: "<<indexData.bdh.index_string_bytes
|
---|
112 | << "\nnum levels: "<<indexData.bdh.num_levels<<endl;
|
---|
113 |
|
---|
114 | // do querying
|
---|
115 | QueryInfo queryInfo;
|
---|
116 | SetCStr (queryInfo.docLevel, "Doc", 3);
|
---|
117 | queryInfo.maxDocs = 50;
|
---|
118 | queryInfo.sortByRank = true;
|
---|
119 | queryInfo.exactWeights = false;
|
---|
120 | queryInfo.needRankInfo = true;
|
---|
121 | queryInfo.needTermFreqs = true;
|
---|
122 |
|
---|
123 | ExtQueryResult queryResult;
|
---|
124 | char query[2048];
|
---|
125 | UCArray queryArray;
|
---|
126 | QueryNode *queryTree = NULL;
|
---|
127 |
|
---|
128 |
|
---|
129 | UCArray docLevel;
|
---|
130 | SetCStr(docLevel, "Doc", 3);
|
---|
131 |
|
---|
132 | UCArray level;
|
---|
133 | UCArrayClear(level);
|
---|
134 | //SetCStr(level, "");
|
---|
135 |
|
---|
136 | int defaultStemMethod = 0; // uncasefolded, unstemmed
|
---|
137 | int defaultBoolCombine = 0; // OR
|
---|
138 | bool shortOutput = false;
|
---|
139 | BrowseQueryNode browseNode;
|
---|
140 | browseNode.startPosition = -10;
|
---|
141 | browseNode.numTerms = 40;
|
---|
142 |
|
---|
143 | BrowseQueryResult browseResult;
|
---|
144 |
|
---|
145 | while (true) {
|
---|
146 | cout << "> ";
|
---|
147 | cin.getline(query, 2048, '\n');
|
---|
148 | SetCStr (queryArray, query, strlen(query));
|
---|
149 |
|
---|
150 | // check for commands
|
---|
151 | if (queryArray.size() >= 2 && queryArray[0] == '.') {
|
---|
152 | if (queryArray[1] == 'q') break; // quit
|
---|
153 |
|
---|
154 | if (queryArray[1] == 'h') { // help
|
---|
155 | printHelp();
|
---|
156 | } else if (queryArray[1] == 'i') {
|
---|
157 | cout << "current index="<< queryInfo.docLevel << "\nchange to index:";
|
---|
158 | cin >> query;
|
---|
159 | UCArrayClear(queryInfo.docLevel);
|
---|
160 | SetCStr(queryInfo.docLevel, query, strlen(query));
|
---|
161 | cout << "index set to " << queryInfo.docLevel <<"\n";
|
---|
162 | cin.getline(query, 2048, '\n');
|
---|
163 | } else if (queryArray[1] == 'l') {
|
---|
164 | cout << "current level="<< level << "\nchange to level:";
|
---|
165 | cin >> query;
|
---|
166 | UCArrayClear(level);
|
---|
167 | SetCStr(level, query, strlen(query));
|
---|
168 | cout << "level set to " << level <<"\n";
|
---|
169 | cin.getline(query, 2048, '\n');
|
---|
170 | }
|
---|
171 |
|
---|
172 | else if (queryArray[1] == 'm') {
|
---|
173 | // maxnumeric
|
---|
174 | int m = 0;
|
---|
175 | cin >> m;
|
---|
176 | cin.getline(query, 2048, '\n'); // eat up return
|
---|
177 | if (4 < m < 512) {
|
---|
178 | maxnumeric = m;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | else if (queryArray[1] == 'p') {
|
---|
182 | // print
|
---|
183 | UCArray docText;
|
---|
184 | unsigned long docNum = 0;
|
---|
185 | cin >> docNum;
|
---|
186 | cin.getline(query, 2048, '\n'); // eat up return
|
---|
187 |
|
---|
188 | if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) {
|
---|
189 | FatalError (1, "Error while trying to get document %u", docNum);
|
---|
190 | }
|
---|
191 |
|
---|
192 | cout << docText << "\n";
|
---|
193 | }
|
---|
194 | else if (queryArray[1] == 't') { // query type - all/some
|
---|
195 | if (queryArray[2] == '1') defaultBoolCombine = 1;
|
---|
196 | else if (queryArray[2] == '0') defaultBoolCombine = 0;
|
---|
197 | else {
|
---|
198 | cout << "Error: please enter .t0 (some) or .t1 (all)\n";
|
---|
199 | }
|
---|
200 | }
|
---|
201 | else if (queryArray[1] == 'r') { // ranking - on/off
|
---|
202 | if (queryArray[2] == '1') queryInfo.sortByRank = true;
|
---|
203 | else if (queryArray[2] == '0') queryInfo.sortByRank = false;
|
---|
204 | else {
|
---|
205 | cout << "Error: please enter .r0 (non-ranked) or .r1 (ranked)\n";
|
---|
206 | }
|
---|
207 | }
|
---|
208 | else if (queryArray[1] == 'c') { // casefolding - on/off
|
---|
209 | if (queryArray[2] == '1') defaultStemMethod |= 1;
|
---|
210 | else if (queryArray[2] == '0') defaultStemMethod &= 0xe;
|
---|
211 | else {
|
---|
212 | cout << "Error: please enter .c0 (case sensitive) or .c1 (casefolded)\n";
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else if (queryArray[1] == 's') { // stemming - on/off
|
---|
216 | if (queryArray[2] == '1') defaultStemMethod |=2;
|
---|
217 | else if (queryArray[2] == '0') defaultStemMethod &=0xd;
|
---|
218 | else {
|
---|
219 | cout << "Error: please enter .s0 (unstemmed) or .s1 (stemmed)\n";
|
---|
220 | }
|
---|
221 | }
|
---|
222 | else if (queryArray[1] == 'o') { // output - short/long
|
---|
223 | if (queryArray[2] == '1') shortOutput = true;
|
---|
224 | else if (queryArray[2] == '0') shortOutput = false;
|
---|
225 | else {
|
---|
226 | cout << "Error: please enter .o0 (long output) or .o1 (short output)\n";
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else if (queryArray[1] == 'b') {
|
---|
230 | // full text browse
|
---|
231 | cout<<"enter a few letters to start browsing from:";
|
---|
232 | cin>>query;
|
---|
233 | UCArrayClear(browseNode.term);
|
---|
234 | SetCStr(browseNode.term, query, strlen(query));
|
---|
235 | cin.getline(query, 2048, '\n'); // get rest of line
|
---|
236 |
|
---|
237 | // print the query
|
---|
238 | PrintNode (cout, &browseNode);
|
---|
239 |
|
---|
240 | MGBrowseQuery(indexData, docLevel, browseNode, browseResult);
|
---|
241 | cout << browseResult;
|
---|
242 | cout << "\n";
|
---|
243 |
|
---|
244 | }
|
---|
245 | else { // bad option
|
---|
246 | cout << "bad command\n\n";
|
---|
247 | printHelp();
|
---|
248 | }
|
---|
249 | } // if a .x query
|
---|
250 | else {
|
---|
251 | // regular query
|
---|
252 | queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod, maxnumeric);
|
---|
253 | if (queryTree == NULL) {
|
---|
254 | cout << "invalid syntax\n";
|
---|
255 | } else {
|
---|
256 | // print the query
|
---|
257 | PrintNode (cout, queryTree);
|
---|
258 |
|
---|
259 | MGQuery (indexData, queryInfo, queryTree, queryResult, level);
|
---|
260 | if (shortOutput) {
|
---|
261 | queryResult.printShort(cout);
|
---|
262 | cout << "\n";
|
---|
263 | } else {
|
---|
264 | cout << queryResult;
|
---|
265 | cout << "\n";
|
---|
266 | }
|
---|
267 | // delete the query
|
---|
268 | delete queryTree;
|
---|
269 | queryTree = NULL;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | // clean up, everybody clean up
|
---|
276 | textData.UnloadData ();
|
---|
277 | indexData.UnloadData ();
|
---|
278 |
|
---|
279 | return (0);
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|