source: trunk/mgpp/java/org/greenstone/mgpp/Queryer.java@ 3365

Last change on this file since 3365 was 3365, checked in by kjdon, 22 years ago

Initial revision

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