[855] | 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 |
|
---|
[1126] | 30 | #include "GSDLQueryParser.h"
|
---|
[855] | 31 |
|
---|
[1850] | 32 | void printHelp() {
|
---|
[855] | 33 |
|
---|
[1850] | 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 |
|
---|
[855] | 48 | int main (int argc, char **argv) {
|
---|
| 49 | int ch;
|
---|
[926] | 50 | char *textfilename = "";
|
---|
| 51 | char *indexfilename = "";
|
---|
[855] | 52 | char *basePath = "";
|
---|
| 53 |
|
---|
| 54 | opterr = 0;
|
---|
| 55 | msg_prefix = argv[0];
|
---|
| 56 |
|
---|
| 57 | // process the command line arguments
|
---|
[926] | 58 | while ((ch = getopt (argc, argv, "f:t:d:h")) != -1) {
|
---|
[855] | 59 | switch (ch) {
|
---|
| 60 | case 'f': /* input file */
|
---|
[926] | 61 | indexfilename = optarg;
|
---|
[855] | 62 | break;
|
---|
[926] | 63 | case 't':
|
---|
| 64 | textfilename = optarg;
|
---|
| 65 | break;
|
---|
[855] | 66 | case 'd':
|
---|
| 67 | basePath = optarg;
|
---|
| 68 | set_basepath (basePath);
|
---|
| 69 | break;
|
---|
| 70 | case 'h':
|
---|
| 71 | case '?':
|
---|
[1299] | 72 | fprintf (stderr, "usage: %s [-h] [-d directory] -f indexname -t textname\n", argv[0]);
|
---|
[855] | 73 | exit (1);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[926] | 77 | if (textfilename[0] == '\0' || indexfilename[0] == '\0') {
|
---|
[1299] | 78 | FatalError (1, "Index and text file names must be specified with -f and -t \n");
|
---|
[855] | 79 | }
|
---|
| 80 |
|
---|
| 81 | // init the text system
|
---|
| 82 | TextData textData;
|
---|
[1126] | 83 | if (!textData.LoadData (basePath, textfilename)) {
|
---|
[926] | 84 | FatalError (1, "Couldn't load text information for \"%s\"", textfilename);
|
---|
[855] | 85 | }
|
---|
| 86 |
|
---|
| 87 | // init the query system
|
---|
| 88 | IndexData indexData;
|
---|
[926] | 89 | if (!indexData.LoadData (basePath, indexfilename)) {
|
---|
| 90 | FatalError (1, "Couldn't load index information for \"%s\"", indexfilename);
|
---|
[855] | 91 | }
|
---|
| 92 |
|
---|
| 93 | // do querying
|
---|
| 94 | QueryInfo queryInfo;
|
---|
[926] | 95 | SetCStr (queryInfo.docLevel, "Document");
|
---|
| 96 | queryInfo.maxDocs = 50;
|
---|
[855] | 97 | queryInfo.sortByRank = true;
|
---|
| 98 | queryInfo.exactWeights = false;
|
---|
| 99 | queryInfo.needRankInfo = true;
|
---|
| 100 | queryInfo.needTermFreqs = true;
|
---|
| 101 |
|
---|
[926] | 102 | ExtQueryResult queryResult;
|
---|
[855] | 103 | char query[2048];
|
---|
| 104 | UCArray queryArray;
|
---|
| 105 | QueryNode *queryTree = NULL;
|
---|
| 106 |
|
---|
[1299] | 107 |
|
---|
| 108 | UCArray docLevel;
|
---|
| 109 | SetCStr(docLevel, "Document");
|
---|
| 110 |
|
---|
[926] | 111 | UCArray level;
|
---|
[1126] | 112 | UCArrayClear(level);
|
---|
[926] | 113 | //SetCStr(level, "");
|
---|
[1299] | 114 |
|
---|
[1849] | 115 | int defaultStemMethod = 0; // uncasefolded, unstemmed
|
---|
[1769] | 116 | int defaultBoolCombine = 0; // OR
|
---|
[1849] | 117 | bool shortOutput = false;
|
---|
[1299] | 118 | BrowseQueryNode browseNode;
|
---|
| 119 | browseNode.startPosition = -10;
|
---|
| 120 | browseNode.numTerms = 40;
|
---|
| 121 |
|
---|
| 122 | BrowseQueryResult browseResult;
|
---|
| 123 |
|
---|
[855] | 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
|
---|
[1850] | 132 |
|
---|
| 133 | if (queryArray[1] == 'h') { // help
|
---|
| 134 | printHelp();
|
---|
| 135 | }
|
---|
[926] | 136 | if (queryArray[1] == 'i') {
|
---|
| 137 | cout << "current index="<< queryInfo.docLevel << "\nchange to index:";
|
---|
| 138 | cin >> query;
|
---|
[1126] | 139 | UCArrayClear(queryInfo.docLevel);
|
---|
[926] | 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;
|
---|
[1126] | 147 | UCArrayClear(level);
|
---|
[926] | 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') {
|
---|
[860] | 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 | }
|
---|
[1769] | 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 | }
|
---|
[1849] | 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 | }
|
---|
[1299] | 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 |
|
---|
[1850] | 217 | }
|
---|
| 218 | else { // bad option
|
---|
| 219 | cout << "bad command\n\n";
|
---|
| 220 | printHelp();
|
---|
| 221 | }
|
---|
[1769] | 222 | } // if a .x query
|
---|
| 223 | else {
|
---|
[855] | 224 | // regular query
|
---|
[1769] | 225 | queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod);
|
---|
[855] | 226 |
|
---|
| 227 | // print the query
|
---|
| 228 | PrintNode (cout, queryTree);
|
---|
| 229 |
|
---|
[926] | 230 | MGQuery (indexData, queryInfo, queryTree, queryResult, level);
|
---|
[1849] | 231 | if (shortOutput) {
|
---|
| 232 | queryResult.printShort(cout);
|
---|
| 233 | cout << "\n";
|
---|
| 234 | }else {
|
---|
| 235 | cout << queryResult;
|
---|
| 236 | cout << "\n";
|
---|
| 237 | }
|
---|
[855] | 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 | }
|
---|
[860] | 249 |
|
---|
[1769] | 250 |
|
---|