source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/PhindPhraseBrowse.java@ 9798

Last change on this file since 9798 was 9798, checked in by kjdon, 19 years ago

for all the converter.getDOM calls, now check for null document before using it - hopefully avoid lots of the exceptions that get printed to the screen if something goes wrong

  • 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;
31import java.io.File;
32/**
33 * PhindServices - the phind phrase browsing service
34 *
35 * @author <a href="mailto:[email protected]">Katherine Don</a>
36 * @version $Revision: 9798 $
37 */
38public class PhindPhraseBrowse
39 extends ServiceRack {
40
41 // the services on offer
42 private static final String PHIND_SERVICE = "PhindApplet";
43
44 private MGPPWrapper mgpp_src=null;
45 private String basepath = null;
46
47 private Element applet_description = null;
48
49 public PhindPhraseBrowse() {
50 this.mgpp_src = new MGPPWrapper();
51 // set up the default params
52 this.mgpp_src.setQueryLevel("Document");
53 this.mgpp_src.setReturnLevel("Document");
54 this.mgpp_src.setMaxDocs(5);
55 this.mgpp_src.setStem(false);
56 this.mgpp_src.setCase(true);
57 }
58 /** configure the service module
59 *
60 * @param info a DOM Element containing any config info for the service
61 * @return true if configured
62 */
63 public boolean configure(Element info, Element extra_info) {
64
65 System.out.println("configuring PhindPhraseBrowse");
66
67 // set up short_service_info_ - for now just has name and type
68 Element e = this.doc.createElement(GSXML.SERVICE_ELEM);
69 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
70 e.setAttribute(GSXML.NAME_ATT, PHIND_SERVICE);
71 this.short_service_info.appendChild(e);
72
73 // set up the static applet description
74
75 applet_description = this.doc.createElement(GSXML.SERVICE_ELEM);
76 applet_description.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
77 applet_description.setAttribute(GSXML.NAME_ATT, PHIND_SERVICE);
78
79 // add in the applet info for the phind applet
80 // need to make this dynamic - library names etc
81 // change the applet params - have a single param with the library name
82 // 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
83 // phindcgi param now is not complete - library must be prepended to it.
84 String app_info = "<"+GSXML.APPLET_ELEM+" CODEBASE='lib' CODE='org.greenstone.applet.phind.Phind.class' ARCHIVE='phind.jar, xercesImpl.jar, xml-apis.jar' WIDTH='500' HEIGHT='400'><PARAM NAME='library' VALUE=''/> <PARAM NAME='phindcgi' VALUE='?";
85 app_info += GSParams.ACTION +"=a&amp;"+GSParams.REQUEST_TYPE +"=r&amp;"+GSParams.SERVICE+"="+PHIND_SERVICE+"&amp;"+GSParams.OUTPUT+"=xml&amp;"+GSParams.RESPONSE_ONLY+"=1'/>";
86 app_info +="<PARAM NAME='collection' VALUE='";
87 app_info += this.cluster_name;
88 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+">";
89
90 Document dom = this.converter.getDOM(app_info);
91 if (dom==null) {
92 System.err.println("PhindPhraseBrowse.configure Error: Couldn't parse applet info");
93 return false;
94 }
95 Element app_elem = dom.getDocumentElement();
96 applet_description.appendChild(this.doc.importNode(app_elem, true));
97
98 return true;
99 }
100
101 protected Element getServiceDescription(String service, String lang, String subset) {
102 if (!service.equals(PHIND_SERVICE)) {
103 return null;
104 }
105 Element describe = (Element) applet_description.cloneNode(true);
106 describe.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(PHIND_SERVICE+".name", lang)));
107 describe.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(PHIND_SERVICE+".description", lang)));
108 return describe;
109 }
110
111 protected Element processPhindApplet(Element request) {
112
113 Element param_elem = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
114 HashMap params = GSXML.extractParams(param_elem, false);
115
116 long first_e = Long.parseLong((String)params.get("pfe"));
117 long last_e = Long.parseLong((String)params.get("ple"));
118 long first_l = Long.parseLong((String)params.get("pfl"));
119 long last_l = Long.parseLong((String)params.get("pll"));
120 long first_d = Long.parseLong((String)params.get("pfd"));
121 long last_d = Long.parseLong((String)params.get("pld"));
122
123 long phrase;
124 String phrase_str = (String)params.get("ppnum");
125 if (phrase_str == null || phrase_str.equals("")) {
126 phrase=0;
127 } else {
128 phrase = Long.parseLong(phrase_str);
129 }
130 String word = (String)params.get("pptext");
131 String phind_index = (String)params.get("pc");
132 // the location of the mgpp database files
133 this.basepath = GSFile.phindBaseDir(this.site_home, this.cluster_name, phind_index);
134
135 // the result element
136 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
137 result.setAttribute(GSXML.FROM_ATT, PHIND_SERVICE);
138 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
139
140 // applet result info must be in appletInfo element
141 Element applet_data = this.doc.createElement(GSXML.APPLET_DATA_ELEM);
142 result.appendChild(applet_data);
143 Element phind_data = this.doc.createElement("phindData");
144 applet_data.appendChild(phind_data);
145
146
147 // if we dont know the phrase number, look it up
148 if (phrase == 0) {
149 if (word==null || word.equals("")) {
150 Element error = phindError("no word or phrase");
151 phind_data.appendChild(error);
152 return result;
153 }
154 phrase = findPhraseNumberFromWord( word);
155 }
156 if (phrase==0) {
157 // the word is not in the collection
158 // return a phind error string
159 Element error = phindError("the term "+word+" is not in the collection");
160 phind_data.appendChild(error);
161 return result;
162 }
163
164 // get the phrase data into the phind_data node
165 getPhraseData(phind_data, phrase, first_l, last_l,
166 first_e, last_e, first_d, last_d);
167 return result;
168
169
170 }// processPhindApplet
171
172 protected long findPhraseNumberFromWord(String word) {
173
174 // set the mgpp index data - we are looking up pword
175 this.mgpp_src.loadIndexData(this.basepath+File.separatorChar+"pword");
176
177 this.mgpp_src.runQuery(word);
178
179 MGPPQueryResult res = this.mgpp_src.getQueryResult();
180 Vector docs = res.getDocs();
181 if (docs.size()==0) {
182 // phrase not found
183 return 0;
184 }
185 MGPPDocInfo doc = (MGPPDocInfo)docs.firstElement();
186 return doc.num_;
187 }
188
189 protected boolean getPhraseData(Element phind_data,
190 long phrase, long first_l, long last_l,
191 long first_e, long last_e, long first_d,
192 long last_d) {
193
194 String record = this.mgpp_src.getDocument(this.basepath+File.separatorChar+"pdata", "Document",
195 phrase);
196 if (record.equals("")) {
197 Element error = phindError("somethings gone wrong - we haven't got a record for phrase number "+phrase);
198 phind_data.appendChild(error);
199 return false;
200 }
201
202 // parse the record - its in gordons cryptic form
203 // ":word:tf:ef:df:el:dl:lf:ll"
204 // el: e,e,e
205 // dl: d;f,d;f,
206 // lf and ll may be null
207 // l: type,dest, dest; type,dest,dest
208
209 // ignore everything up to and including first colon (has
210 // <Document>3505: at the start)
211 record = record.substring(record.indexOf(':')+1);
212
213 // split on ':'
214 String [] fields = record.split(":");
215 String word = fields[0];
216 String tf = fields[1];
217 String ef = fields[2];
218 String df = fields[3];
219
220
221 String expansions = fields[4];
222 String documents = fields[5];
223 String lf = "0";
224 String linklist = "";
225 if (fields.length > 7) {// have thesaurus stuff
226 lf =fields[6];
227 linklist = fields[7];
228 }
229
230 // the phindData attributes and phrase
231 phind_data.setAttribute("id", Long.toString(phrase));
232 phind_data.setAttribute("df", df);
233 phind_data.setAttribute("ef", ef);
234 phind_data.setAttribute("lf", lf);
235 phind_data.setAttribute("tf", tf);
236 GSXML.createTextElement(this.doc, "phrase", word);
237
238 addExpansionList(phind_data, expansions, word, ef, first_e, last_e);
239 addDocumentList(phind_data, documents, word, df, first_d, last_d);
240 if (!lf.equals("0")) {
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 = this.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 = this.mgpp_src.getDocument(this.basepath+File.separatorChar+"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 = this.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(this.doc, "prefix", ends[0]));
297 }
298 if (!ends[1].equals("")) {
299 expansion.appendChild(GSXML.createTextElement(this.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 = this.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 = this.mgpp_src.getDocument(this.basepath+File.separatorChar+"docs", "Document",
349 doc_num);
350
351 if (record ==null || record.equals("")) return null;
352
353 // ignore everything up to and including first \t
354 record = record.substring(record.indexOf('\t')+1);
355
356 String [] fields = record.split("\t");
357 String hash = fields[0];
358 String title = fields[1];
359
360 Element d = this.doc.createElement("document");
361 d.setAttribute("hash", hash);
362 d.appendChild(GSXML.createTextElement(this.doc, "title", title));
363
364 return d;
365
366 }
367 protected boolean addThesaurusList(Element phind_data, String record,
368 String word,
369 String freq,
370 long first, long last) {
371
372
373 Element thesaurus_list = this.doc.createElement("thesaurusList");
374 phind_data.appendChild(thesaurus_list);
375 thesaurus_list.setAttribute("length", freq);
376 thesaurus_list.setAttribute("start", Long.toString(first));
377 thesaurus_list.setAttribute("end", Long.toString(last));
378
379 // get the list of type,dest,dest
380 String [] links = record.split(";");
381 int length = links.length;
382 long index = 0;
383 for (int i = 0; i < length; i++) { // go through the entries
384 String link_info = links[(int)i];
385 String [] items = link_info.split(",");
386 // the first entry is teh type
387 String type = items[0];
388 for (int j = 1; j<items.length; j++, index++) {
389 if (index >= first && index < last) { // only output the ones we want
390 long phrase = Long.parseLong(items[j]);
391 Element t = getThesaurus(phrase);
392 t.setAttribute("type", type);
393 thesaurus_list.appendChild(t);
394 }
395 }
396 }
397
398 return true;
399 }
400
401 protected Element getThesaurus(long phrase_num) {
402
403 // look up the phrase in the pdata thingy
404 String record = this.mgpp_src.getDocument(this.basepath+File.separatorChar+"pdata", "Document",
405 phrase_num);
406
407 if (record ==null || record.equals("")) return null;
408
409 // ignore everything up to and including first colon
410 record = record.substring(record.indexOf(':')+1);
411
412 String [] fields = record.split(":");
413 String phrase = fields[0];
414 String tf = fields[1];
415 //String ef = fields[2]; dont use this
416 String df = fields[3];
417
418 Element thesaurus = this.doc.createElement("thesaurus");
419 thesaurus.setAttribute("tf", tf);
420 thesaurus.setAttribute("df", df);
421 thesaurus.setAttribute("id", Long.toString(phrase_num));
422 thesaurus.appendChild(GSXML.createTextElement(this.doc, "phrase", phrase));
423 return thesaurus;
424
425 }
426
427 /** returns an array of two elements - the prefix and the suffix*/
428 protected String [] splitPhraseOnWord(String phrase, String word) {
429
430 if (word.equals("")) {
431
432 String [] res = {phrase, ""};
433 return res;
434 }
435 // use 2 so that we only split on the first occurrance. trailing empty strings should be included
436 String [] result = phrase.split(word, 2);
437 return result;
438
439 }
440
441 protected Element phindError(String message) {
442 Element e = this.doc.createElement("phindError");
443 Text t = this.doc.createTextNode(message);
444 e.appendChild(t);
445 return e;
446 }
447
448}
449
Note: See TracBrowser for help on using the repository browser.