source: gsdl/trunk/trunk/mgpp/java/org/greenstone/mgpp/Queryer.java@ 16583

Last change on this file since 16583 was 16583, checked in by davidb, 16 years ago

Undoing change commited in r16582

  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1package org.greenstone.mgpp;
2
3import java.util.Vector;
4import java.io.BufferedReader;
5import java.io.InputStreamReader;
6import java.io.File;
7/** Queryer - java port of c++ Queryer
8 * - uses MGPPSearchWrapper and MGPPRetrieveWrapper to access mgpp
9 * only has get document and search - full text browse not implemented
10 *
11 * run as:
12 * java org.greenstone.mgpp.Queryer <basedir> <indexdir> <textdir>
13 *@see MGPPSearchWrapper
14 *@see MGPPRetrieveWrapper
15 */
16public class Queryer {
17
18 /** outputs to std out a help message describing the commands used by
19 * Queryer
20 */
21 public void printHelp() {
22
23 System.out.println( "commands available are:\n"+
24 "\t.q\t\tquit\n"+
25 "\t.h\t\tprint this help message\n"+
26 "\t.d\t\tprint the current query parameter settings\n" +
27 "\t.i<level>\tchange the search level to level\n"+
28 "\t.l<level>\tchange the result level to level\n"+
29 "\t.r0/.r1\t\tranking off/on\n"+
30 "\t.t0/.t1\t\tquery type some/all\n"+
31 "\t.c0/.c1\t\tcasefolding off/on\n"+
32 "\t.s0/.s1\t\tstemming off/on\n"+
33 "\t.o0/.o1\t\tshort output off/on\n"+
34 "\t.m<num>\t\tset max docs to return to num\n\n"+
35 "\t.p<docnum>\tprint document docnum\n"+
36 "\t<query string>\t do a query\n");
37 }
38
39
40 public static void main(String[] args) {
41 if (args.length != 3) {
42 System.out.println("Usage: java org.greenstone.mgpp.Queryer <basedir> <indexdir> <textdir>");
43 return;
44 }
45
46 Queryer self = new Queryer();
47
48 String base_dir = args[0];
49 String text_path = args[2];
50 String index_path = args[1];
51
52 text_path = base_dir + File.separatorChar+text_path;
53 index_path = base_dir + File.separatorChar+ index_path;
54
55 // the jni classes to access mgpp stuff
56 MGPPSearchWrapper searcher = new MGPPSearchWrapper();
57 MGPPRetrieveWrapper retriever = new MGPPRetrieveWrapper();
58 searcher.loadIndexData(index_path);
59
60 // the return level
61 String level = "Section";
62
63 System.out.println("Welcome to Java Queryer :-)");
64 self.printHelp();
65
66 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
67 boolean shortOutput=false;
68
69 String command; // user input
70 char x; // the command letter
71 String data=null; // any auxiliary input to a command
72 while (true) {
73 System.out.print(">"); // the prompt
74 try {
75 command = br.readLine();
76 command=command.trim();
77 if (command.startsWith(".")) {
78 // a system command
79 x = command.charAt(1);
80 if (command.length() > 2) {
81 data = command.substring(2);
82 data = data.trim();
83
84 }
85 switch (x) {
86 case 'q': // clean up and exit
87 searcher.unloadIndexData();
88 return;
89 case 'h': // print help message
90 self.printHelp();
91 break;
92 case 'd': // print query param settings
93 String info = searcher.getQueryParams();
94 System.out.println(info);
95 break;
96 case 'p': // print doc
97 int docnum = Integer.parseInt(data);
98 String doc = retriever.getDocument(text_path,
99 level, docnum);
100 System.out.println(doc);
101 break;
102 case 'm': //match docs
103 int match = Integer.parseInt(data);
104 searcher.setMaxDocs(match);
105 break;
106 case 's': // set stem on/off
107 int stem = Integer.parseInt(data);
108 if (stem==0 ){
109 searcher.setStem(false);
110 } else if(stem==1) {
111 searcher.setStem(true);
112 } else {
113 System.err.println("Error: stem should be 0 or 1");
114 }
115 break;
116 case 'c': // set case on/off
117 int casef = Integer.parseInt(data);
118 if (casef==0) {
119 searcher.setCase(false);
120 } else if (casef==1) {
121 searcher.setCase(true);
122 } else {
123 System.err.println("Error: case should be 0 or 1");
124 }
125 break;
126 case 'i': // set search level
127 searcher.setQueryLevel(data);
128 break;
129 case 'l': // set result level
130 searcher.setReturnLevel(data);
131 level = data;
132 break;
133 case 'r': // set rank on/off
134 int rank = Integer.parseInt(data);
135 if (rank==0) {
136 searcher.setSortByRank(false);
137 } else if (rank==1) {
138 searcher.setSortByRank(true);
139 } else {
140 System.err.println("Error: rank should be 0 or 1");
141 }
142 break;
143 case 't': // set query type some/all
144 int type = Integer.parseInt(data);
145 if (type==0 || type==1) {
146 searcher.setMatchMode(type);
147 } else {
148 System.err.println("Error: type should be 0 (some) or 1 (all)");
149 }
150 break;
151 case 'o': // set output short/long
152 int output = Integer.parseInt(data);
153 if (output==0) {
154 shortOutput = false;
155 } else if (output==1) {
156 shortOutput = true;
157 }else {
158 System.err.println("Error: output should be 0 or 1");
159 }
160 break;
161 }
162 }
163 else {
164 // a query
165 searcher.runQuery(command);
166 MGPPQueryResult res = searcher.getQueryResult();
167 if (res.hasSyntaxError()) {
168 System.out.println("invalid syntax error\n");
169 } else {
170 if (shortOutput) {
171 System.out.println(res.toShortString());
172 } else {
173 System.out.println(res.toString());
174 }
175 }
176 }
177
178 } catch (Exception e) {
179 System.out.println("Queryer error: "+e.getClass() + " "+e.getMessage());
180 }
181
182 }
183 }
184
185
186}
Note: See TracBrowser for help on using the repository browser.