source: trunk/gsdl/packages/mg/java/org/greenstone/mg/Queryer.java@ 3741

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

Initial MG JNI (Java side) code.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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.r0/.r1\t\tranking off/on\n"+
46 "\t.t0/.t1\t\tquery type some/all\n"+
47 "\t.c0/.c1\t\tcasefolding off/on\n"+
48 "\t.s0/.s1\t\tstemming off/on\n"+
49 "\t.o0/.o1\t\tshort output off/on\n"+
50 "\t.m<num>\t\tset max docs to return to num\n\n"+
51 "\t.p<docnum>\tprint document docnum\n"+
52 "\t<query string>\tdo a query\n");
53 }
54
55
56 public static void main(String[] args)
57 {
58 if (args.length != 3) {
59 System.out.println("Usage: java org.greenstone.mg.Queryer <basedir> <indexdir> <textdir>");
60 return;
61 }
62
63 Queryer self = new Queryer();
64
65 String base_dir = args[0];
66 String index_path = args[1];
67 String text_path = args[2];
68
69 // the jni class to access mg stuff
70 MGWrapper wrapper = new MGWrapper();
71 wrapper.setIndex(index_path);
72
73 System.out.println("Welcome to Java Queryer :-)");
74 self.printHelp();
75
76 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
77 boolean shortOutput = false;
78
79 String command; // user input
80 char x; // the command letter
81 String data = null; // any auxiliary input to a command
82 while (true) {
83 System.out.print(">"); // the prompt
84 try {
85 command = br.readLine();
86 command = command.trim();
87 if (command.startsWith(".")) {
88 // a system command
89 x = command.charAt(1);
90 if (command.length() > 2) {
91 data = command.substring(2);
92 data = data.trim();
93 }
94
95 switch (x) {
96 case 'q': // clean up and exit
97 wrapper.unloadIndexData();
98 return;
99 case 'h': // print help message
100 self.printHelp();
101 break;
102 case 'd': // print query param settings
103 String info = wrapper.getQueryParams();
104 System.out.println(info);
105 break;
106 case 'p': // print doc
107 int docnum = Integer.parseInt(data);
108 String doc = wrapper.getDocument(base_dir, text_path, docnum);
109 System.out.println(doc);
110 break;
111 case 'm': //match docs
112 int match = Integer.parseInt(data);
113 wrapper.setMaxDocs(match);
114 break;
115 case 's': // set stem on/off
116 int stem = Integer.parseInt(data);
117 if (stem==0 ){
118 wrapper.setStem(false);
119 } else if(stem==1) {
120 wrapper.setStem(true);
121 } else {
122 System.err.println("Error: stem should be 0 or 1");
123 }
124 break;
125 case 'c': // set case on/off
126 int casef = Integer.parseInt(data);
127 if (casef==0) {
128 wrapper.setCase(false);
129 } else if (casef==1) {
130 wrapper.setCase(true);
131 } else {
132 System.err.println("Error: case should be 0 or 1");
133 }
134 break;
135 case 'i': // set index
136 wrapper.setIndex(data);
137 break;
138 case 'r': // set rank on/off
139 int rank = Integer.parseInt(data);
140 if (rank==0) {
141 wrapper.setSortByRank(false);
142 } else if (rank==1) {
143 wrapper.setSortByRank(true);
144 } else {
145 System.err.println("Error: rank should be 0 or 1");
146 }
147 break;
148 case 't': // set query type some/all
149 int type = Integer.parseInt(data);
150 if (type==0 || type==1) {
151 wrapper.setMatchMode(type);
152 } else {
153 System.err.println("Error: type should be 0 (some) or 1 (all)");
154 }
155 break;
156 case 'o': // set output short/long
157 int output = Integer.parseInt(data);
158 if (output==0) {
159 shortOutput = false;
160 } else if (output==1) {
161 shortOutput = true;
162 } else {
163 System.err.println("Error: output should be 0 or 1");
164 }
165 break;
166 }
167 }
168 else {
169 // a query
170 wrapper.runQuery(base_dir, text_path, command);
171 MGQueryResult res = wrapper.getQueryResult();
172 System.out.println("(Java) Matching documents: " + res.getTotalDocs());
173 if (shortOutput) {
174 System.out.println(res.toShortString());
175 } else {
176 System.out.println(res.toString());
177 }
178 }
179
180 } catch (Exception e) {
181 System.out.println("Queryer error: "+e.getClass() + " "+e.getMessage());
182 }
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.