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

Last change on this file since 2468 was 2468, checked in by sjboddie, 23 years ago

Fiddled about with mgpp to get it compiling on Windows under VC++ 6.0. I
still can't get it to compile under VC++ 4.2 because of some weird
behaviour in STLport.

Also tidied up a little and removed some of the old log information
that was scattered about in some of the files.

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