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

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

added help message

  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 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 * $Id: Queryer.cpp 1850 2001-01-22 02:07:35Z kjm18 $
21 *
22 **************************************************************************/
23
24#include "MGQuery.h"
25#include "TextGet.h"
26
27#include "messages.h"
28#include "mg_files.h"
29
30#include "GSDLQueryParser.h"
31
32void printHelp() {
33
34 cout << "commands available are:\n"
35 << "\t.q\t\tquit\n"
36 << "\t.h\t\tprint the help message\n"
37 << "\t.i\t\tchange the search level (enter the new level at the prompt)\n"
38 << "\t.l\t\tchange the result level ( \"\" \"\" )\n"
39 << "\t.b\t\tfull text browse (enter a word or fragment at the prompt)\n"
40 << "\t.r0/.r1\t\tranking off/on\n"
41 << "\t.t0/.t1\t\tquery type some/all\n"
42 << "\t.c0/.c1\t\tcasefolding off/on\n"
43 << "\t.s0/.s1\t\tstemming off/on\n"
44 << "\t.o0/.o1\t\tshort output off/on\n\n";
45
46}
47
48int main (int argc, char **argv) {
49 int ch;
50 char *textfilename = "";
51 char *indexfilename = "";
52 char *basePath = "";
53
54 opterr = 0;
55 msg_prefix = argv[0];
56
57 // process the command line arguments
58 while ((ch = getopt (argc, argv, "f:t:d:h")) != -1) {
59 switch (ch) {
60 case 'f': /* input file */
61 indexfilename = optarg;
62 break;
63 case 't':
64 textfilename = optarg;
65 break;
66 case 'd':
67 basePath = optarg;
68 set_basepath (basePath);
69 break;
70 case 'h':
71 case '?':
72 fprintf (stderr, "usage: %s [-h] [-d directory] -f indexname -t textname\n", argv[0]);
73 exit (1);
74 }
75 }
76
77 if (textfilename[0] == '\0' || indexfilename[0] == '\0') {
78 FatalError (1, "Index and text file names must be specified with -f and -t \n");
79 }
80
81 // init the text system
82 TextData textData;
83 if (!textData.LoadData (basePath, textfilename)) {
84 FatalError (1, "Couldn't load text information for \"%s\"", textfilename);
85 }
86
87 // init the query system
88 IndexData indexData;
89 if (!indexData.LoadData (basePath, indexfilename)) {
90 FatalError (1, "Couldn't load index information for \"%s\"", indexfilename);
91 }
92
93 // do querying
94 QueryInfo queryInfo;
95 SetCStr (queryInfo.docLevel, "Document");
96 queryInfo.maxDocs = 50;
97 queryInfo.sortByRank = true;
98 queryInfo.exactWeights = false;
99 queryInfo.needRankInfo = true;
100 queryInfo.needTermFreqs = true;
101
102 ExtQueryResult queryResult;
103 char query[2048];
104 UCArray queryArray;
105 QueryNode *queryTree = NULL;
106
107
108 UCArray docLevel;
109 SetCStr(docLevel, "Document");
110
111 UCArray level;
112 UCArrayClear(level);
113 //SetCStr(level, "");
114
115 int defaultStemMethod = 0; // uncasefolded, unstemmed
116 int defaultBoolCombine = 0; // OR
117 bool shortOutput = false;
118 BrowseQueryNode browseNode;
119 browseNode.startPosition = -10;
120 browseNode.numTerms = 40;
121
122 BrowseQueryResult browseResult;
123
124 while (true) {
125 cout << "> ";
126 cin.getline(query, 2048, '\n');
127 SetCStr (queryArray, query);
128
129 // check for commands
130 if (queryArray.size() >= 2 && queryArray[0] == '.') {
131 if (queryArray[1] == 'q') break; // quit
132
133 if (queryArray[1] == 'h') { // help
134 printHelp();
135 }
136 if (queryArray[1] == 'i') {
137 cout << "current index="<< queryInfo.docLevel << "\nchange to index:";
138 cin >> query;
139 UCArrayClear(queryInfo.docLevel);
140 SetCStr(queryInfo.docLevel, query);
141 cout << "index set to " << queryInfo.docLevel <<"\n";
142 cin.getline(query, 2048, '\n');
143 }
144 if (queryArray[1] == 'l') {
145 cout << "current level="<< level << "\nchange to level:";
146 cin >> query;
147 UCArrayClear(level);
148 SetCStr(level, query);
149 cout << "level set to " << level <<"\n";
150 cin.getline(query, 2048, '\n');
151 }
152
153
154 else if (queryArray[1] == 'p') {
155 // print
156 UCArray docText;
157 unsigned long docNum = 0;
158 cin >> docNum;
159 cin.getline(query, 2048, '\n'); // eat up return
160
161 if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) {
162 FatalError (1, "Error while trying to get document %u", docNum);
163 }
164
165 cout << docText << "\n";
166 }
167 else if (queryArray[1] == 't') { // query type - all/some
168 if (queryArray[2] == '1') defaultBoolCombine = 1;
169 else if (queryArray[2] == '0') defaultBoolCombine = 0;
170 else {
171 cout << "Error: please enter .t0 (some) or .t1 (all)\n";
172 }
173 }
174 else if (queryArray[1] == 'r') { // ranking - on/off
175 if (queryArray[2] == '1') queryInfo.sortByRank = true;
176 else if (queryArray[2] == '0') queryInfo.sortByRank = false;
177 else {
178 cout << "Error: please enter .r0 (non-ranked) or .r1 (ranked)\n";
179 }
180 }
181 else if (queryArray[1] == 'c') { // casefolding - on/off
182 if (queryArray[2] == '1') defaultStemMethod |= 1;
183 else if (queryArray[2] == '0') defaultStemMethod &= 0xe;
184 else {
185 cout << "Error: please enter .c0 (case sensitive) or .c1 (casefolded)\n";
186 }
187 }
188 else if (queryArray[1] == 's') { // stemming - on/off
189 if (queryArray[2] == '1') defaultStemMethod |=2;
190 else if (queryArray[2] == '0') defaultStemMethod &=0xd;
191 else {
192 cout << "Error: please enter .s0 (unstemmed) or .s1 (stemmed)\n";
193 }
194 }
195 else if (queryArray[1] == 'o') { // output - short/long
196 if (queryArray[2] == '1') shortOutput = true;
197 else if (queryArray[2] == '0') shortOutput = false;
198 else {
199 cout << "Error: please enter .o0 (long output) or .o1 (short output)\n";
200 }
201 }
202 else if (queryArray[1] == 'b') {
203 // full text browse
204 cout<<"enter a few letters to start browsing from:";
205 cin>>query;
206 UCArrayClear(browseNode.term);
207 SetCStr(browseNode.term, query);
208 cin.getline(query, 2048, '\n'); // get rest of line
209
210 // print the query
211 PrintNode (cout, &browseNode);
212
213 MGBrowseQuery(indexData, docLevel, browseNode, browseResult);
214 cout << browseResult;
215 cout << "\n";
216
217 }
218 else { // bad option
219 cout << "bad command\n\n";
220 printHelp();
221 }
222 } // if a .x query
223 else {
224 // regular query
225 queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod);
226
227 // print the query
228 PrintNode (cout, queryTree);
229
230 MGQuery (indexData, queryInfo, queryTree, queryResult, level);
231 if (shortOutput) {
232 queryResult.printShort(cout);
233 cout << "\n";
234 }else {
235 cout << queryResult;
236 cout << "\n";
237 }
238 // delete the query
239 if (queryTree != NULL) delete queryTree;
240 queryTree = NULL;
241 }
242 }
243
244
245 // clean up, everybody clean up
246 textData.UnloadData ();
247 indexData.UnloadData ();
248}
249
250
Note: See TracBrowser for help on using the repository browser.