source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/PhindPhraseBrowse.java@ 29235

Last change on this file since 29235 was 29235, checked in by ak19, 10 years ago

Dr Bainbridge fixed another wrong document error (WRONG_DOCUMENT_ERR) to do with whether we need to cloneNodes in the same document or call importNode to copy/import the nodes into another document.

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