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 1769 2000-12-07 22:31:53Z 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 |
|
---|
33 | int 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 = 1; // casefolded, unstemmed
|
---|
101 | int defaultBoolCombine = 0; // OR
|
---|
102 | BrowseQueryNode browseNode;
|
---|
103 | browseNode.startPosition = -10;
|
---|
104 | browseNode.numTerms = 40;
|
---|
105 |
|
---|
106 | BrowseQueryResult browseResult;
|
---|
107 |
|
---|
108 | while (true) {
|
---|
109 | cout << "> ";
|
---|
110 | cin.getline(query, 2048, '\n');
|
---|
111 | SetCStr (queryArray, query);
|
---|
112 |
|
---|
113 | // check for commands
|
---|
114 | if (queryArray.size() >= 2 && queryArray[0] == '.') {
|
---|
115 | if (queryArray[1] == 'q') break; // quit
|
---|
116 |
|
---|
117 | if (queryArray[1] == 'i') {
|
---|
118 | cout << "current index="<< queryInfo.docLevel << "\nchange to index:";
|
---|
119 | cin >> query;
|
---|
120 | UCArrayClear(queryInfo.docLevel);
|
---|
121 | SetCStr(queryInfo.docLevel, query);
|
---|
122 | cout << "index set to " << queryInfo.docLevel <<"\n";
|
---|
123 | cin.getline(query, 2048, '\n');
|
---|
124 | }
|
---|
125 | if (queryArray[1] == 'l') {
|
---|
126 | cout << "current level="<< level << "\nchange to level:";
|
---|
127 | cin >> query;
|
---|
128 | UCArrayClear(level);
|
---|
129 | SetCStr(level, query);
|
---|
130 | cout << "level set to " << level <<"\n";
|
---|
131 | cin.getline(query, 2048, '\n');
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | else if (queryArray[1] == 'p') {
|
---|
136 | // print
|
---|
137 | UCArray docText;
|
---|
138 | unsigned long docNum = 0;
|
---|
139 | cin >> docNum;
|
---|
140 | cin.getline(query, 2048, '\n'); // eat up return
|
---|
141 |
|
---|
142 | if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) {
|
---|
143 | FatalError (1, "Error while trying to get document %u", docNum);
|
---|
144 | }
|
---|
145 |
|
---|
146 | cout << docText << "\n";
|
---|
147 | }
|
---|
148 | else if (queryArray[1] == 't') { // query type - all/some
|
---|
149 | if (queryArray[2] == '1') defaultBoolCombine = 1;
|
---|
150 | else if (queryArray[2] == '0') defaultBoolCombine = 0;
|
---|
151 | else {
|
---|
152 | cout << "Error: please enter .t0 (some) or .t1 (all)\n";
|
---|
153 | }
|
---|
154 | }
|
---|
155 | else if (queryArray[1] == 'r') { // ranking - on/off
|
---|
156 | if (queryArray[2] == '1') queryInfo.sortByRank = true;
|
---|
157 | else if (queryArray[2] == '0') queryInfo.sortByRank = false;
|
---|
158 | else {
|
---|
159 | cout << "Error: please enter .r0 (non-ranked) or .r1 (ranked)\n";
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else if (queryArray[1] == 'b') {
|
---|
163 | // full text browse
|
---|
164 | cout<<"enter a few letters to start browsing from:";
|
---|
165 | cin>>query;
|
---|
166 | UCArrayClear(browseNode.term);
|
---|
167 | SetCStr(browseNode.term, query);
|
---|
168 | cin.getline(query, 2048, '\n'); // get rest of line
|
---|
169 |
|
---|
170 | // print the query
|
---|
171 | PrintNode (cout, &browseNode);
|
---|
172 |
|
---|
173 | MGBrowseQuery(indexData, docLevel, browseNode, browseResult);
|
---|
174 | cout << browseResult;
|
---|
175 | cout << "\n";
|
---|
176 |
|
---|
177 | }
|
---|
178 | } // if a .x query
|
---|
179 | else {
|
---|
180 | // regular query
|
---|
181 | queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod);
|
---|
182 |
|
---|
183 | // print the query
|
---|
184 | PrintNode (cout, queryTree);
|
---|
185 |
|
---|
186 | MGQuery (indexData, queryInfo, queryTree, queryResult, level);
|
---|
187 | cout << queryResult;
|
---|
188 | cout << "\n";
|
---|
189 |
|
---|
190 | // delete the query
|
---|
191 | if (queryTree != NULL) delete queryTree;
|
---|
192 | queryTree = NULL;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | // clean up, everybody clean up
|
---|
198 | textData.UnloadData ();
|
---|
199 | indexData.UnloadData ();
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|