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 | */
|
---|
19 | package org.greenstone.mg;
|
---|
20 |
|
---|
21 | import java.util.Vector;
|
---|
22 | import java.io.BufferedReader;
|
---|
23 | import 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 | */
|
---|
33 | public 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 classes to access mg stuff
|
---|
69 | MGSearchWrapper searcher = new MGSearchWrapper();
|
---|
70 | MGRetrieveWrapper retriever = new MGRetrieveWrapper();
|
---|
71 | searcher.setIndex(index_path);
|
---|
72 | retriever.setIndex(index_path);
|
---|
73 |
|
---|
74 | System.out.println("Welcome to Java Queryer :-)");
|
---|
75 | self.printHelp();
|
---|
76 |
|
---|
77 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
---|
78 | boolean shortOutput = false;
|
---|
79 |
|
---|
80 | String command; // user input
|
---|
81 | char x; // the command letter
|
---|
82 | String data = null; // any auxiliary input to a command
|
---|
83 | while (true) {
|
---|
84 | System.out.print(">"); // the prompt
|
---|
85 | try {
|
---|
86 | command = br.readLine();
|
---|
87 | command = command.trim();
|
---|
88 | if (command.startsWith(".")) {
|
---|
89 | // a system command
|
---|
90 | x = command.charAt(1);
|
---|
91 | if (command.length() > 2) {
|
---|
92 | data = command.substring(2);
|
---|
93 | data = data.trim();
|
---|
94 | }
|
---|
95 |
|
---|
96 | switch (x) {
|
---|
97 | case 'q': // clean up and exit
|
---|
98 | searcher.unloadIndexData();
|
---|
99 | retriever.unloadIndexData();
|
---|
100 | return;
|
---|
101 | case 'h': // print help message
|
---|
102 | self.printHelp();
|
---|
103 | break;
|
---|
104 | case 'd': // print query param settings
|
---|
105 | String info = searcher.getQueryParams();
|
---|
106 | System.out.println(info);
|
---|
107 | break;
|
---|
108 | case 'p': // print doc
|
---|
109 | int docnum = Integer.parseInt(data);
|
---|
110 | String doc = retriever.getDocument(base_dir, text_path, docnum);
|
---|
111 | System.out.println(doc);
|
---|
112 | break;
|
---|
113 | case 'm': //match docs
|
---|
114 | int match = Integer.parseInt(data);
|
---|
115 | searcher.setMaxDocs(match);
|
---|
116 | break;
|
---|
117 | case 's': // set stem on/off
|
---|
118 | int stem = Integer.parseInt(data);
|
---|
119 | if (stem==0 ){
|
---|
120 | searcher.setStem(false);
|
---|
121 | } else if(stem==1) {
|
---|
122 | searcher.setStem(true);
|
---|
123 | } else {
|
---|
124 | System.err.println("Error: stem should be 0 or 1");
|
---|
125 | }
|
---|
126 | break;
|
---|
127 | case 'c': // set case on/off
|
---|
128 | int casef = Integer.parseInt(data);
|
---|
129 | if (casef==0) {
|
---|
130 | searcher.setCase(false);
|
---|
131 | } else if (casef==1) {
|
---|
132 | searcher.setCase(true);
|
---|
133 | } else {
|
---|
134 | System.err.println("Error: case should be 0 or 1");
|
---|
135 | }
|
---|
136 | break;
|
---|
137 | case 'i': // set index
|
---|
138 | searcher.setIndex(data);
|
---|
139 | break;
|
---|
140 | case 't': // set query type some/all
|
---|
141 | int type = Integer.parseInt(data);
|
---|
142 | if (type==0 || type==1) {
|
---|
143 | searcher.setMatchMode(type);
|
---|
144 | } else {
|
---|
145 | System.err.println("Error: type should be 0 (some) or 1 (all)");
|
---|
146 | }
|
---|
147 | break;
|
---|
148 | case 'o': // set output short/long
|
---|
149 | int output = Integer.parseInt(data);
|
---|
150 | if (output==0) {
|
---|
151 | shortOutput = false;
|
---|
152 | } else if (output==1) {
|
---|
153 | shortOutput = true;
|
---|
154 | } else {
|
---|
155 | System.err.println("Error: output should be 0 or 1");
|
---|
156 | }
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | else {
|
---|
161 | // a query
|
---|
162 | searcher.runQuery(base_dir, text_path, command);
|
---|
163 | MGQueryResult res = searcher.getQueryResult();
|
---|
164 | System.out.println("(Java) Matching documents: " + res.getTotalDocs());
|
---|
165 | if (shortOutput) {
|
---|
166 | System.out.println(res.toShortString());
|
---|
167 | } else {
|
---|
168 | System.out.println(res.toString());
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | } catch (Exception e) {
|
---|
173 | System.out.println("Queryer error: "+e.getClass() + " "+e.getMessage());
|
---|
174 | e.printStackTrace();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|