source: trunk/indexers/mg/java/org/greenstone/mg/Queryer.java@ 3796

Last change on this file since 3796 was 3796, checked in by mdewsnip, 21 years ago

Removed option to sort by rank/natural order.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/*
2 * Queryer.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.mg;
20
21import java.util.Vector;
22import java.io.BufferedReader;
23import java.io.InputStreamReader;
24
25/** Queryer - java port of c++ Queryer
26 * - uses MGWrapper to access mg
27 * only has get document and search - full text browse not implemented
28 *
29 * run as:
30 * java org.greenstone.mg.Queryer <basedir> <indexdir> <textdir>
31 *@see MGWrapper
32 */
33public class Queryer {
34
35 /** outputs to std out a help message describing the commands used by
36 * Queryer
37 */
38 public void printHelp()
39 {
40 System.out.println( "commands available are:\n"+
41 "\t.q\t\tquit\n"+
42 "\t.h\t\tprint this help message\n"+
43 "\t.d\t\tprint the current query parameter settings\n" +
44 "\t.i<index>\tsearch index <index>\n"+
45 "\t.t0/.t1\t\tquery type some/all\n"+
46 "\t.c0/.c1\t\tcasefolding off/on\n"+
47 "\t.s0/.s1\t\tstemming off/on\n"+
48 "\t.o0/.o1\t\tshort output off/on\n"+
49 "\t.m<num>\t\tset max docs to return to num\n\n"+
50 "\t.p<docnum>\tprint document docnum\n"+
51 "\t<query string>\tdo a query\n");
52 }
53
54
55 public static void main(String[] args)
56 {
57 if (args.length != 3) {
58 System.out.println("Usage: java org.greenstone.mg.Queryer <basedir> <indexdir> <textdir>");
59 return;
60 }
61
62 Queryer self = new Queryer();
63
64 String base_dir = args[0];
65 String index_path = args[1];
66 String text_path = args[2];
67
68 // the jni class to access mg stuff
69 MGWrapper wrapper = new MGWrapper();
70 wrapper.setIndex(index_path);
71
72 System.out.println("Welcome to Java Queryer :-)");
73 self.printHelp();
74
75 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
76 boolean shortOutput = false;
77
78 String command; // user input
79 char x; // the command letter
80 String data = null; // any auxiliary input to a command
81 while (true) {
82 System.out.print(">"); // the prompt
83 try {
84 command = br.readLine();
85 command = command.trim();
86 if (command.startsWith(".")) {
87 // a system command
88 x = command.charAt(1);
89 if (command.length() > 2) {
90 data = command.substring(2);
91 data = data.trim();
92 }
93
94 switch (x) {
95 case 'q': // clean up and exit
96 wrapper.unloadIndexData();
97 return;
98 case 'h': // print help message
99 self.printHelp();
100 break;
101 case 'd': // print query param settings
102 String info = wrapper.getQueryParams();
103 System.out.println(info);
104 break;
105 case 'p': // print doc
106 int docnum = Integer.parseInt(data);
107 String doc = wrapper.getDocument(base_dir, text_path, docnum);
108 System.out.println(doc);
109 break;
110 case 'm': //match docs
111 int match = Integer.parseInt(data);
112 wrapper.setMaxDocs(match);
113 break;
114 case 's': // set stem on/off
115 int stem = Integer.parseInt(data);
116 if (stem==0 ){
117 wrapper.setStem(false);
118 } else if(stem==1) {
119 wrapper.setStem(true);
120 } else {
121 System.err.println("Error: stem should be 0 or 1");
122 }
123 break;
124 case 'c': // set case on/off
125 int casef = Integer.parseInt(data);
126 if (casef==0) {
127 wrapper.setCase(false);
128 } else if (casef==1) {
129 wrapper.setCase(true);
130 } else {
131 System.err.println("Error: case should be 0 or 1");
132 }
133 break;
134 case 'i': // set index
135 wrapper.setIndex(data);
136 break;
137 case 't': // set query type some/all
138 int type = Integer.parseInt(data);
139 if (type==0 || type==1) {
140 wrapper.setMatchMode(type);
141 } else {
142 System.err.println("Error: type should be 0 (some) or 1 (all)");
143 }
144 break;
145 case 'o': // set output short/long
146 int output = Integer.parseInt(data);
147 if (output==0) {
148 shortOutput = false;
149 } else if (output==1) {
150 shortOutput = true;
151 } else {
152 System.err.println("Error: output should be 0 or 1");
153 }
154 break;
155 }
156 }
157 else {
158 // a query
159 wrapper.runQuery(base_dir, text_path, command);
160 MGQueryResult res = wrapper.getQueryResult();
161 System.out.println("(Java) Matching documents: " + res.getTotalDocs());
162 if (shortOutput) {
163 System.out.println(res.toShortString());
164 } else {
165 System.out.println(res.toString());
166 }
167 }
168
169 } catch (Exception e) {
170 System.out.println("Queryer error: "+e.getClass() + " "+e.getMessage());
171 e.printStackTrace();
172 }
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.