/************************************************************************** * * Queryer.cpp -- simple interactive query program * Copyright (C) 1999 Rodger McNab * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: Queryer.cpp 879 2000-01-31 03:01:25Z rjmcnab $ * **************************************************************************/ #include "MGQuery.h" #include "TextGet.h" #include "messages.h" #include "mg_files.h" #include "QueryParser.h" int main (int argc, char **argv) { int ch; char *filename = ""; char *basePath = ""; opterr = 0; msg_prefix = argv[0]; // process the command line arguments while ((ch = getopt (argc, argv, "f:d:h")) != -1) { switch (ch) { case 'f': /* input file */ filename = optarg; break; case 'd': basePath = optarg; set_basepath (basePath); break; case 'h': case '?': fprintf (stderr, "usage: %s [-h] [-d directory] -f name\n", argv[0]); exit (1); } } if (filename[0] == '\0') { FatalError (1, "A file name must be specified with -f\n"); } // init the text system TextData textData; if (!textData.LoadData (filename)) { FatalError (1, "Couldn't load text information for \"%s\"", filename); } // init the query system IndexData indexData; if (!indexData.LoadData (basePath, filename)) { FatalError (1, "Couldn't load index information for \"%s\"", filename); } // do querying QueryInfo queryInfo; SetCStr (queryInfo.docLevel, "Chapter"); queryInfo.maxDocs = 10; queryInfo.sortByRank = true; queryInfo.exactWeights = false; queryInfo.needRankInfo = true; queryInfo.needTermFreqs = true; QueryResult queryResult; char query[2048]; UCArray queryArray; QueryNode *queryTree = NULL; while (true) { // cerr << "getting next line\n"; cout << "> "; cin.getline(query, 2048, '\n'); SetCStr (queryArray, query); // check for commands if (queryArray.size() >= 2 && queryArray[0] == '.') { if (queryArray[1] == 'q') break; // quit if (queryArray[1] == 'p') { // print UCArray docText; unsigned long docNum = 0; cin >> docNum; cin.getline(query, 2048, '\n'); // eat up return if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) { FatalError (1, "Error while trying to get document %u", docNum); } cout << docText << "\n"; } } else { // regular query queryTree = ParseQuery (queryArray); // print the query PrintNode (cout, queryTree); MGQuery (indexData, queryInfo, queryTree, queryResult); cout << queryResult; cout << "\n"; // delete the query if (queryTree != NULL) delete queryTree; queryTree = NULL; } } // clean up, everybody clean up textData.UnloadData (); indexData.UnloadData (); }