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

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

more options added

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