root/trunk/gsdl/src/mgpp/text/Queryer.cpp @ 861

Revision 861, 3.4 KB (checked in by rjmcnab, 20 years ago)

fixed a few more bugs

  • Property svn:keywords set to Author Date Id Revision
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$
21 *
22 **************************************************************************/
23
24#include "MGQuery.h"
25#include "TextGet.h"
26
27#include "messages.h"
28#include "mg_files.h"
29
30#include "QueryParser.h"
31
32
33int main (int argc, char **argv) {
34  int ch;
35  char *filename = "";
36  char *basePath = "";
37 
38  opterr = 0;
39  msg_prefix = argv[0];
40 
41  // process the command line arguments
42  while ((ch = getopt (argc, argv, "f:d:h")) != -1) {
43    switch (ch) {
44    case 'f':       /* input file */
45      filename = optarg;
46      break;
47    case 'd':
48      basePath = optarg;
49      set_basepath (basePath);
50      break;
51    case 'h':
52    case '?':
53      fprintf (stderr, "usage: %s [-h] [-d directory] -f name\n", argv[0]);
54      exit (1);
55    }
56  }
57
58  if (filename[0] == '\0') {
59    FatalError (1, "A file name must be specified with -f\n");
60  }
61 
62  // init the text system
63  TextData textData;
64  if (!textData.LoadData (filename)) {
65    FatalError (1, "Couldn't load text information for \"%s\"", filename);
66  }
67 
68  // init the query system
69  IndexData indexData;
70  if (!indexData.LoadData (basePath, filename)) {
71    FatalError (1, "Couldn't load index information for \"%s\"", filename);
72  }
73
74  // do querying
75  QueryInfo queryInfo;
76  SetCStr (queryInfo.docLevel, "Document");
77  queryInfo.maxDocs = 10;
78  queryInfo.sortByRank = true;
79  queryInfo.exactWeights = false;
80  queryInfo.needRankInfo = true;
81  queryInfo.needTermFreqs = true;
82 
83  QueryResult queryResult;
84  char query[2048];
85  UCArray queryArray;
86  QueryNode *queryTree = NULL;
87
88  while (true) {
89    //    cerr << "getting next line\n";
90    cout << "> ";
91    cin.getline(query, 2048, '\n');
92    SetCStr (queryArray, query);
93
94    // check for commands
95    if (queryArray.size() >= 2 && queryArray[0] == '.') {
96      if (queryArray[1] == 'q') break;  // quit
97
98      if (queryArray[1] == 'p') {
99    // print
100    UCArray docText;
101    unsigned long docNum = 0;
102    cin >> docNum;
103    cin.getline(query, 2048, '\n'); // eat up return
104
105    if (!GetDocText (textData, queryInfo.docLevel, docNum, docText)) {
106      FatalError (1, "Error while trying to get document %u", docNum);
107    }
108
109    cout << docText << "\n";
110      }
111     
112    } else {
113      // regular query
114      queryTree = ParseQuery (queryArray);
115
116      // print the query
117      PrintNode (cout, queryTree);
118
119      MGQuery (indexData, queryInfo, queryTree, queryResult);
120      cout << queryResult;
121      cout << "\n";
122     
123      // delete the query
124      if (queryTree != NULL) delete queryTree;
125      queryTree = NULL;
126    }
127  }
128
129
130  // clean up, everybody clean up
131  textData.UnloadData ();
132  indexData.UnloadData ();
133}
134
Note: See TracBrowser for help on using the browser.