source: trunk/gsdl/src/mgpp/text/Queryer.cpp@ 12882

Last change on this file since 12882 was 12882, checked in by kjdon, 18 years ago

added accentfolding option. setting up default stem method now uses defines from mg_files

  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
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
40void 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.a0/.a1\t\taccentfolding off/on\n"
53 << "\t.o0/.o1\t\tshort output off/on\n"
54 << "\t.m\t\tset maxnumeric (enter the number at the prompt)\n\n"
55 << "\t.p\t\tprint a document (enter the docnum at the prompt)\n"
56 << "otherwise just enter a query\n\n";
57
58}
59
60int main (int argc, char **argv) {
61 int ch;
62 char *textfilename = "";
63 char *indexfilename = "";
64 char *basePath = "";
65
66 opterr = 0;
67 msg_prefix = argv[0];
68
69 // process the command line arguments
70 while ((ch = getopt (argc, argv, "f:t:d:h")) != -1) {
71 switch (ch) {
72 case 'f': /* input file */
73 indexfilename = optarg;
74 break;
75 case 't':
76 textfilename = optarg;
77 break;
78 case 'd':
79 basePath = optarg;
80 set_basepath (basePath);
81 break;
82 case 'h':
83 case '?':
84 fprintf (stderr, "usage: %s [-h] [-d directory] -f indexname -t textname\n", argv[0]);
85 exit (1);
86 }
87 }
88
89 if (textfilename[0] == '\0' || indexfilename[0] == '\0') {
90 FatalError (1, "Index and text file names must be specified with -f and -t \n");
91 }
92
93 // init the text system
94 TextData textData;
95 if (!textData.LoadData (basePath, textfilename)) {
96 FatalError (1, "Couldn't load text information for \"%s\"", textfilename);
97 }
98
99 // init the query system
100 IndexData indexData;
101 if (!indexData.LoadData (basePath, indexfilename)) {
102 FatalError (1, "Couldn't load index information for \"%s\"", indexfilename);
103 }
104
105 int maxnumeric = 4;
106
107 // debug output
108 cerr << "num docs: "<<indexData.bdh.num_docs
109 << "\nnum frags: "<<indexData.bdh.num_frags
110 << "\nnum words: "<<indexData.bdh.num_words
111 << "\ntotal bytes: "<<indexData.bdh.total_bytes
112 << "\nindex string bytes: "<<indexData.bdh.index_string_bytes
113 << "\nnum levels: "<<indexData.bdh.num_levels<<endl;
114
115 // do querying
116 QueryInfo queryInfo;
117 SetCStr (queryInfo.docLevel, "Doc", 3);
118 queryInfo.maxDocs = 50;
119 queryInfo.sortByRank = true;
120 queryInfo.exactWeights = false;
121 queryInfo.needRankInfo = true;
122 queryInfo.needTermFreqs = true;
123
124 ExtQueryResult queryResult;
125 char query[2048];
126 UCArray queryArray;
127 QueryNode *queryTree = NULL;
128
129
130 UCArray docLevel;
131 SetCStr(docLevel, "Doc", 3);
132
133 UCArray level;
134 UCArrayClear(level);
135 //SetCStr(level, "");
136
137 int defaultStemMethod = 0; // uncasefolded, unstemmed, unaccentfolded
138 int defaultBoolCombine = 0; // OR
139 bool shortOutput = false;
140 BrowseQueryNode browseNode;
141 browseNode.startPosition = -10;
142 browseNode.numTerms = 40;
143
144 BrowseQueryResult browseResult;
145
146 while (true) {
147 cout << "> ";
148 cin.getline(query, 2048, '\n');
149 SetCStr (queryArray, query, strlen(query));
150
151 // check for commands
152 if (queryArray.size() >= 2 && queryArray[0] == '.') {
153 if (queryArray[1] == 'q') break; // quit
154
155 if (queryArray[1] == 'h') { // help
156 printHelp();
157 } else if (queryArray[1] == 'i') {
158 cout << "current index="<< queryInfo.docLevel << "\nchange to index:";
159 cin >> query;
160 UCArrayClear(queryInfo.docLevel);
161 SetCStr(queryInfo.docLevel, query, strlen(query));
162 cout << "index set to " << queryInfo.docLevel <<"\n";
163 cin.getline(query, 2048, '\n');
164 } else if (queryArray[1] == 'l') {
165 cout << "current level="<< level << "\nchange to level:";
166 cin >> query;
167 UCArrayClear(level);
168 SetCStr(level, query, strlen(query));
169 cout << "level set to " << level <<"\n";
170 cin.getline(query, 2048, '\n');
171 }
172
173 else if (queryArray[1] == 'm') {
174 // maxnumeric
175 int m = 0;
176 cin >> m;
177 cin.getline(query, 2048, '\n'); // eat up return
178 if (4 < m < 512) {
179 maxnumeric = m;
180 }
181 }
182 else if (queryArray[1] == 'p') {
183 // print
184 UCArray docText;
185 unsigned long docNum = 0;
186 cin >> docNum;
187 cin.getline(query, 2048, '\n'); // eat up return
188
189 if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) {
190 FatalError (1, "Error while trying to get document %u", docNum);
191 }
192
193 cout << docText << "\n";
194 }
195 else if (queryArray[1] == 't') { // query type - all/some
196 if (queryArray[2] == '1') defaultBoolCombine = 1;
197 else if (queryArray[2] == '0') defaultBoolCombine = 0;
198 else {
199 cout << "Error: please enter .t0 (some) or .t1 (all)\n";
200 }
201 }
202 else if (queryArray[1] == 'r') { // ranking - on/off
203 if (queryArray[2] == '1') queryInfo.sortByRank = true;
204 else if (queryArray[2] == '0') queryInfo.sortByRank = false;
205 else {
206 cout << "Error: please enter .r0 (non-ranked) or .r1 (ranked)\n";
207 }
208 }
209 else if (queryArray[1] == 'c') { // casefolding - on/off
210 if (queryArray[2] == '1') defaultStemMethod |= STEM_CaseFolding;
211 else if (queryArray[2] == '0') defaultStemMethod &= (~STEM_CaseFolding);
212 else {
213 cout << "Error: please enter .c0 (case sensitive) or .c1 (casefolded)\n";
214 }
215 }
216 else if (queryArray[1] == 's') { // stemming - on/off
217 if (queryArray[2] == '1') defaultStemMethod |= STEM_Stemming;
218 else if (queryArray[2] == '0') defaultStemMethod &= (~STEM_Stemming);
219 else {
220 cout << "Error: please enter .s0 (unstemmed) or .s1 (stemmed)\n";
221 }
222 }
223 else if (queryArray[1] == 'a') { // accentfolding - on/off
224 if (queryArray[2] == '1') defaultStemMethod |= STEM_AccentFolding;
225 else if (queryArray[2] == '0') defaultStemMethod &= (~STEM_AccentFolding);
226 else {
227 cout << "Error: please enter .a0 (accent sensitive) or .a1 (accentfolded)\n";
228 }
229 }
230 else if (queryArray[1] == 'o') { // output - short/long
231 if (queryArray[2] == '1') shortOutput = true;
232 else if (queryArray[2] == '0') shortOutput = false;
233 else {
234 cout << "Error: please enter .o0 (long output) or .o1 (short output)\n";
235 }
236 }
237 else if (queryArray[1] == 'b') {
238 // full text browse
239 cout<<"enter a few letters to start browsing from:";
240 cin>>query;
241 UCArrayClear(browseNode.term);
242 SetCStr(browseNode.term, query, strlen(query));
243 cin.getline(query, 2048, '\n'); // get rest of line
244
245 // print the query
246 PrintNode (cout, &browseNode);
247
248 MGBrowseQuery(indexData, docLevel, browseNode, browseResult);
249 cout << browseResult;
250 cout << "\n";
251
252 }
253 else { // bad option
254 cout << "bad command\n\n";
255 printHelp();
256 }
257 } // if a .x query
258 else {
259 // regular query
260 queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod, maxnumeric);
261 if (queryTree == NULL) {
262 cout << "invalid syntax\n";
263 } else {
264 // print the query
265 PrintNode (cout, queryTree);
266
267 MGQuery (indexData, queryInfo, queryTree, queryResult, level);
268 if (shortOutput) {
269 queryResult.printShort(cout);
270 cout << "\n";
271 } else {
272 cout << queryResult;
273 cout << "\n";
274 }
275 // delete the query
276 delete queryTree;
277 queryTree = NULL;
278 }
279 }
280 }
281
282
283 // clean up, everybody clean up
284 textData.UnloadData ();
285 indexData.UnloadData ();
286
287 return (0);
288}
289
290
Note: See TracBrowser for help on using the repository browser.