source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/PhindPhraseBrowse.java@ 3868

Last change on this file since 3868 was 3868, checked in by kjdon, 21 years ago

relects GSXML changes

  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/*
2 * PhindServices.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.gsdl3.service;
20
21import org.greenstone.gsdl3.util.*;
22
23import org.greenstone.mgpp.*;
24import org.w3c.dom.Document;
25import org.w3c.dom.Node;
26import org.w3c.dom.Element;
27import org.w3c.dom.Text;
28
29import java.util.Vector;
30import java.util.HashMap;
31/**
32 * PhindServices - the phind phrase browsing service
33 *
34 * @author <a href="mailto:[email protected]">Katherine Don</a>
35 * @version $Revision: 3868 $
36 */
37public class PhindPhraseBrowse
38 extends ServiceRack {
39
40 // the services on offer
41 private static final String PHIND_SERVICE = "PhindApplet";
42
43 private MGPPWrapper mgpp_src_=null;
44 private String basepath_ = null;
45 public PhindPhraseBrowse() {
46 mgpp_src_ = new MGPPWrapper();
47 // set up the default params
48 mgpp_src_.setQueryLevel("Document");
49 mgpp_src_.setReturnLevel("Document");
50 mgpp_src_.setMaxDocs(5);
51 mgpp_src_.setStem(false);
52 mgpp_src_.setCase(true);
53 }
54 /** configure the service module
55 *
56 * @param info a DOM Element containing any config info for the service
57 * @return true if configured
58 */
59 public boolean configure(Element info) {
60
61 System.out.println("configuring PhindPhraseBrowse");
62
63 // set up short_service_info_ - for now just has name and type
64 Element e = doc_.createElement(GSXML.SERVICE_ELEM);
65 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
66 e.setAttribute(GSXML.NAME_ATT, PHIND_SERVICE);
67 short_service_info_.appendChild(e);
68
69 // set up service_info_map_ - we only have one element, and it has
70 // no extra info yet - we are not processing the config info
71 Element f = doc_.createElement(GSXML.SERVICE_ELEM);
72 f.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
73 f.setAttribute(GSXML.NAME_ATT, PHIND_SERVICE);
74
75 // add in the applet info for the phind applet
76 // need to make this dynamic - library names etc
77 // change the applet params - have a single param with the library name
78 // this is left blank at this end, and must be filled in by applet action - if the library name is not needed, this param is left out
79 // phindcgi param now is not complete - library must be prepended to it.
80 String app_info = "<"+GSXML.APPLET_ELEM+" CODEBASE='lib/java' CODE='org.greenstone.applet.phind.Phind.class' ARCHIVE='phind.jar, xercesImpl.jar, gsdl3.jar, jaxp.jar, xml-apis.jar' WIDTH='500' HEIGHT='400'><PARAM NAME='library' VALUE=''/> <PARAM NAME='phindcgi' VALUE='?";
81 app_info += GSCGI.ACTION_ARG +"=a&amp;"+GSCGI.REQUEST_TYPE_ARG +"=r&amp;"+GSCGI.SERVICE_ARG+"="+PHIND_SERVICE+"&amp;"+GSCGI.OUTPUT_ARG+"=xml'/>";
82 app_info +="<PARAM NAME='collection' VALUE='";
83 app_info += cluster_name_;
84 app_info += "'/> <PARAM NAME='classifier' VALUE='1'/> <PARAM NAME='orientation' VALUE='vertical'/> <PARAM NAME='depth' VALUE='2'/> <PARAM NAME='resultorder' VALUE='L,l,E,e,D,d'/> <PARAM NAME='backdrop' VALUE='interfaces/default/images/phindbg1.jpg'/><PARAM NAME='fontsize' VALUE='10'/> <PARAM NAME='blocksize' VALUE='10'/>The Phind java applet.</"+GSXML.APPLET_ELEM+">";
85
86 Document dom = converter_.getDOM(app_info);
87 Element app_elem = dom.getDocumentElement();
88 f.appendChild(doc_.importNode(app_elem, true));
89
90 service_info_map_.put(PHIND_SERVICE, f);
91
92 return true;
93 }
94
95 /** creates a display element containing all the text strings needed to display the service page, in the language specified */
96 protected Element createServiceDisplay(String service, String lang) {
97 Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
98 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
99 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
100
101 Element param;
102
103 return display;
104
105 }
106
107 protected Element processPhindApplet(Element request) {
108
109 Element param_elem = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
110 HashMap params = GSXML.extractParams(param_elem, false);
111
112 long first_e = Long.parseLong((String)params.get("pfe"));
113 long last_e = Long.parseLong((String)params.get("ple"));
114 long first_l = Long.parseLong((String)params.get("pfl"));
115 long last_l = Long.parseLong((String)params.get("pll"));
116 long first_d = Long.parseLong((String)params.get("pfd"));
117 long last_d = Long.parseLong((String)params.get("pld"));
118
119 long phrase;
120 String phrase_str = (String)params.get("ppnum");
121 if (phrase_str == null || phrase_str.equals("")) {
122 phrase=0;
123 } else {
124 phrase = Long.parseLong(phrase_str);
125 }
126 String word = (String)params.get("pptext");
127 String phind_index = (String)params.get("pc");
128 // the location of the mgpp database files
129 basepath_ = GSFile.phindBaseDir(site_home_, cluster_name_, phind_index);
130
131 // the result element
132 Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
133 String from = GSPath.appendLink(cluster_name_, "PhindApplet");
134 result.setAttribute(GSXML.FROM_ATT, from);
135 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
136
137 // applet result info must be in appletInfo element
138 Element applet_data = doc_.createElement(GSXML.APPLET_DATA_ELEM);
139 result.appendChild(applet_data);
140 Element phind_data = doc_.createElement("phindData");
141 applet_data.appendChild(phind_data);
142
143
144 // if we dont know the phrase number, look it up
145 if (phrase == 0) {
146 if (word==null || word.equals("")) {
147 Element error = phindError("no word or phrase");
148 phind_data.appendChild(error);
149 return result;
150 }
151 phrase = findPhraseNumberFromWord( word);
152 System.out.println("phind, term number for "+word+" is "+phrase);
153 }
154 if (phrase==0) {
155 // the word is not in the collection
156 // return a phind error string
157 Element error = phindError("the term "+word+" is not in the collection");
158 phind_data.appendChild(error);
159 return result;
160 }
161
162 // get the phrase data into the phind_data node
163 getPhraseData(phind_data, phrase, first_l, last_l,
164 first_e, last_e, first_d, last_d);
165 return result;
166
167
168 }// processPhindApplet
169
170 protected long findPhraseNumberFromWord(String word) {
171
172 // set the mgpp index data - we are looking up pword
173 mgpp_src_.loadIndexData(basepath_, "pword");
174
175 mgpp_src_.runQuery(word);
176
177 MGPPQueryResult res = mgpp_src_.getQueryResult();
178 Vector docs = res.getDocs();
179 if (docs.size()==0) {
180 // phrase not found
181 return 0;
182 }
183 MGPPDocInfo doc = (MGPPDocInfo)docs.firstElement();
184 return doc.num_;
185 }
186
187 protected boolean getPhraseData(Element phind_data,
188 long phrase, long first_l, long last_l,
189 long first_e, long last_e, long first_d,
190 long last_d) {
191
192 String record = mgpp_src_.getDocument(basepath_, "pdata", "Document",
193 phrase);
194 if (record.equals("")) {
195 Element error = phindError("somethings gone wrong - we haven't got a record for phrase number "+phrase);
196 phind_data.appendChild(error);
197 return false;
198 }
199
200 System.out.println("record="+record);
201 // parse the record - its in gordons cryptic form
202 // ":word:tf:ef:df:el:dl:lf:ll"
203 // el: e,e,e
204 // dl: d;f,d;f,
205 // lf and ll may be null
206 // l: type,dest, dest; type,dest,dest
207
208 // ignore everything up to and including first colon (has
209 // <Document>3505: at the start)
210 record = record.substring(record.indexOf(':')+1);
211
212 // split on ':'
213 String [] fields = record.split(":");
214 String word = fields[0];
215 String tf = fields[1];
216 String ef = fields[2];
217 String df = fields[3];
218
219
220 String expansions = fields[4];
221 String documents = fields[5];
222 String lf = "0";
223 String linklist = "";
224 if (fields.length > 7) {// have thesaurus stuff
225 lf =fields[6];
226 linklist = fields[7];
227 }
228
229 // the phindData attributes and phrase
230 phind_data.setAttribute("id", Long.toString(phrase));
231 phind_data.setAttribute("df", df);
232 phind_data.setAttribute("ef", ef);
233 phind_data.setAttribute("lf", lf);
234 phind_data.setAttribute("tf", tf);
235 GSXML.createTextElement(doc_, "phrase", word);
236
237 addExpansionList(phind_data, expansions, word, ef, first_e, last_e);
238 addDocumentList(phind_data, documents, word, df, first_d, last_d);
239 if (!lf.equals("0")) {
240 System.out.println("adding thesaurus stuff");
241 addThesaurusList(phind_data, linklist, word, lf, first_l, last_l);
242 }
243 return true;
244 }
245
246 protected boolean addExpansionList( Element phind_data, String record,
247 String word,
248 String freq,
249 long first, long last) {
250
251 Element expansion_list = doc_.createElement("expansionList");
252 phind_data.appendChild(expansion_list);
253 expansion_list.setAttribute("length", freq);
254 expansion_list.setAttribute("start", Long.toString(first));
255 expansion_list.setAttribute("end", Long.toString(last));
256
257 // get the list of strings
258 String [] expansions = record.split(",");
259 int length = expansions.length;
260 if (length < last) last = length;
261 for (long i = first; i < last; i++) {
262 long num = Long.parseLong(expansions[(int)i]);
263 Element expansion = getExpansion( num, word);
264 expansion.setAttribute("num", Long.toString(i));
265 expansion_list.appendChild(expansion);
266 }
267 return true;
268 }
269
270 protected Element getExpansion(long phrase_num,
271 String orig_phrase) {
272
273 // look up the phrase in the pdata thingy
274 String record = mgpp_src_.getDocument(basepath_, "pdata", "Document",
275 phrase_num);
276
277 if (record ==null || record.equals("")) return null;
278
279 // ignore everything up to and including first colon
280 record = record.substring(record.indexOf(':')+1);
281
282 String [] fields = record.split(":");
283 String phrase = fields[0];
284 String tf = fields[1];
285 //String ef = fields[2]; dont use this
286 String df = fields[3];
287
288 Element expansion = doc_.createElement("expansion");
289 expansion.setAttribute("tf", tf);
290 expansion.setAttribute("df", df);
291 expansion.setAttribute("id", Long.toString(phrase_num));
292
293 // get teh suffix and prefix
294 String [] ends = splitPhraseOnWord(phrase, orig_phrase);
295 if (!ends[0].equals("")) {
296 expansion.appendChild(GSXML.createTextElement(doc_, "prefix", ends[0]));
297 }
298 if (!ends[1].equals("")) {
299 expansion.appendChild(GSXML.createTextElement(doc_, "suffix", ends[1]));
300 }
301
302 return expansion;
303
304 }
305
306 protected boolean addDocumentList(Element phind_data, String record,
307 String word,
308 String freq,
309 long first, long last) {
310
311 Element document_list = doc_.createElement("documentList");
312 phind_data.appendChild(document_list);
313 document_list.setAttribute("length", freq);
314 document_list.setAttribute("start", Long.toString(first));
315 document_list.setAttribute("end", Long.toString(last));
316
317 // get the list of doc,freq
318 String [] doc_freqs = record.split(";");
319 int length = doc_freqs.length;
320 if (length<last) last=length;
321
322 for (long i = first; i < last; i++) {
323 String doc_elem = doc_freqs[(int)i];
324 int p = doc_elem.indexOf(',');
325 long doc_num;
326 String doc_freq;
327 if (p == -1) { // there is no freq in the record
328 doc_num =Long.parseLong(doc_elem);
329 doc_freq = "1";
330 } else {
331 doc_num = Long.parseLong(doc_elem.substring(0,p));
332 doc_freq = doc_elem.substring(p+1);
333 }
334 Element document = getDocument( doc_num);
335 document.setAttribute("freq", doc_freq);
336 document.setAttribute("num", Long.toString(i));
337 document_list.appendChild(document);
338 }
339
340
341 return true;
342 }
343
344
345 protected Element getDocument(long doc_num) {
346
347 // look up the phrase in the docs thingy
348 String record = mgpp_src_.getDocument(basepath_, "docs", "Document",
349 doc_num);
350
351 if (record ==null || record.equals("")) return null;
352 System.out.println("doc record:"+record);
353
354 // ignore everything up to and including first \t
355 record = record.substring(record.indexOf('\t')+1);
356
357 String [] fields = record.split("\t");
358 String hash = fields[0];
359 String title = fields[1];
360
361 Element d = doc_.createElement("document");
362 d.setAttribute("hash", hash);
363 d.appendChild(GSXML.createTextElement(doc_, "title", title));
364
365 return d;
366
367 }
368 protected boolean addThesaurusList(Element phind_data, String record,
369 String word,
370 String freq,
371 long first, long last) {
372
373
374 Element thesaurus_list = doc_.createElement("thesaurusList");
375 phind_data.appendChild(thesaurus_list);
376 thesaurus_list.setAttribute("length", freq);
377 thesaurus_list.setAttribute("start", Long.toString(first));
378 thesaurus_list.setAttribute("end", Long.toString(last));
379
380 System.out.println("record for thesaurus="+record);
381
382 // get the list of type,dest,dest
383 String [] links = record.split(";");
384 int length = links.length;
385 long index = 0;
386 for (int i = 0; i < length; i++) { // go through the entries
387 String link_info = links[(int)i];
388 String [] items = link_info.split(",");
389 // the first entry is teh type
390 String type = items[0];
391 for (int j = 1; j<items.length; j++, index++) {
392 if (index >= first && index < last) { // only output the ones we want
393 long phrase = Long.parseLong(items[j]);
394 Element t = getThesaurus(phrase);
395 t.setAttribute("type", type);
396 thesaurus_list.appendChild(t);
397 }
398 }
399 }
400
401 return true;
402 }
403
404 protected Element getThesaurus(long phrase_num) {
405
406 // look up the phrase in the pdata thingy
407 String record = mgpp_src_.getDocument(basepath_, "pdata", "Document",
408 phrase_num);
409
410 if (record ==null || record.equals("")) return null;
411
412 // ignore everything up to and including first colon
413 record = record.substring(record.indexOf(':')+1);
414
415 String [] fields = record.split(":");
416 String phrase = fields[0];
417 String tf = fields[1];
418 //String ef = fields[2]; dont use this
419 String df = fields[3];
420
421 Element thesaurus = doc_.createElement("thesaurus");
422 thesaurus.setAttribute("tf", tf);
423 thesaurus.setAttribute("df", df);
424 thesaurus.setAttribute("id", Long.toString(phrase_num));
425 thesaurus.appendChild(GSXML.createTextElement(doc_, "phrase", phrase));
426 return thesaurus;
427
428 }
429
430 /** returns an array of two elements - the prefix and the suffix*/
431 protected String [] splitPhraseOnWord(String phrase, String word) {
432
433 if (word.equals("")) {
434
435 String [] res = {phrase, ""};
436 return res;
437 }
438 // use 2 so that we only split on the first occurrance. trailing empty strings should be included
439 String [] result = phrase.split(word, 2);
440 if (result.length !=2) {
441 System.out.println("didn't get two substrings!!");
442 }
443 return result;
444
445 }
446
447 protected Element phindError(String message) {
448 Element e = doc_.createElement("phindError");
449 Text t = doc_.createTextNode(message);
450 e.appendChild(t);
451 return e;
452 }
453
454}
455
Note: See TracBrowser for help on using the repository browser.