1 | package org.greenstone.gsdl3.gs3build.indexers;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.BufferedReader;
|
---|
12 | import java.io.InputStreamReader;
|
---|
13 |
|
---|
14 | import org.w3c.dom.*;
|
---|
15 |
|
---|
16 | import org.greenstone.mg.*;
|
---|
17 |
|
---|
18 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
|
---|
19 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
|
---|
20 | import org.greenstone.gsdl3.gs3build.doctypes.HTMLDocument;
|
---|
21 | import org.greenstone.gsdl3.gs3build.doctypes.METSDocument;
|
---|
22 | import org.greenstone.gsdl3.gs3build.metadata.*;
|
---|
23 | import org.greenstone.gsdl3.gs3build.xpointer.XPointer;
|
---|
24 | import org.greenstone.gsdl3.util.GSXML;
|
---|
25 | import org.greenstone.gsdl3.util.Misc;
|
---|
26 | import org.greenstone.gsdl3.util.Processing;
|
---|
27 |
|
---|
28 | public class MGIndexer extends AbstractIndexer
|
---|
29 | {
|
---|
30 | int pass;
|
---|
31 | int documentSeqNo;
|
---|
32 | int sectionSeqNo;
|
---|
33 | boolean firstDocument;
|
---|
34 | String outputDirectory;
|
---|
35 | // InputStream indexerFeedback;
|
---|
36 | // InputStream indexerErrors;
|
---|
37 | //OutputStream indexerTextfeed;
|
---|
38 | StringBuffer indexBuffer;
|
---|
39 | //Process mg_passes;
|
---|
40 | File textDirectory;
|
---|
41 | File indexDirectory;
|
---|
42 | String indexStem;
|
---|
43 | String textStem;
|
---|
44 | List indexes;
|
---|
45 | String overallName;
|
---|
46 |
|
---|
47 | String currentIndexName;
|
---|
48 | String currentIndexLevel;
|
---|
49 | String currentIndexField;
|
---|
50 |
|
---|
51 | MGPassesWrapper mgPasses;
|
---|
52 |
|
---|
53 | static final char END_OF_DOCUMENT = (char) 2;
|
---|
54 | static final char END_OF_SECTION = (char) 3; // actually this is end of para for mg
|
---|
55 | static final char END_OF_STREAM = (char) 4;
|
---|
56 |
|
---|
57 | public static final String MG_INDEX_TYPE = "mg";
|
---|
58 | public static final String INDEX_FILE_STEM = "index";
|
---|
59 |
|
---|
60 | class MGIndex
|
---|
61 | { String name=null;
|
---|
62 | String level=null;
|
---|
63 | String field=null;
|
---|
64 | boolean error = false;// assume built until we get an error
|
---|
65 |
|
---|
66 | public MGIndex(String name, String level, String field)
|
---|
67 | { this.name = name;
|
---|
68 | this.level = level;
|
---|
69 | this.field = field;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public MGIndex(String indexLabel)
|
---|
73 | { int colonAt = indexLabel.indexOf(':');
|
---|
74 |
|
---|
75 | if (colonAt >= 0)
|
---|
76 | { this.field = indexLabel.substring(colonAt+1);
|
---|
77 | this.level = indexLabel.substring(0, colonAt);
|
---|
78 | createIndexName();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public String getLevel()
|
---|
83 | { return this.level;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public String getField()
|
---|
87 | { return this.field;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public String getName()
|
---|
91 | {
|
---|
92 | if (this.name==null || this.name.equals("")) {
|
---|
93 | createIndexName();
|
---|
94 | }
|
---|
95 | return this.name;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean hasError() {
|
---|
99 | return this.error;
|
---|
100 | }
|
---|
101 | public void setError(boolean b) {
|
---|
102 | this.error = b;
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void createIndexName() {
|
---|
106 | StringBuffer new_name = new StringBuffer();
|
---|
107 | new_name.append(Character.toLowerCase((char) this.level.charAt(0)));
|
---|
108 |
|
---|
109 | int c, w;
|
---|
110 | w = 0;
|
---|
111 | c = 0;
|
---|
112 | while (c < this.field.length() && w < 2) {
|
---|
113 | char ch = this.field.charAt(c);
|
---|
114 |
|
---|
115 | ch = Character.toLowerCase(ch);
|
---|
116 | if (Character.isLetter(ch)) {
|
---|
117 | if (ch != 'a' && ch != 'e' && ch != 'i' &&
|
---|
118 | ch != 'o' && ch != 'u') {
|
---|
119 | new_name.append(ch);
|
---|
120 | w++;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | c ++;
|
---|
124 | }
|
---|
125 | this.name = new_name.toString();
|
---|
126 |
|
---|
127 | }
|
---|
128 | } // MGIndex
|
---|
129 |
|
---|
130 | public MGIndexer(String name)
|
---|
131 | { this.indexes = new ArrayList();
|
---|
132 | this.overallName = name;
|
---|
133 |
|
---|
134 | }
|
---|
135 |
|
---|
136 | public String getIndexType()
|
---|
137 | { return MG_INDEX_TYPE;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public String getName()
|
---|
141 | { return this.overallName;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // private String getIndexDirectory(String level, String field)
|
---|
145 | // { StringBuffer directory = new StringBuffer();
|
---|
146 | // directory.append(Character.toLowerCase((char) level.charAt(0)));
|
---|
147 |
|
---|
148 | // int c, w;
|
---|
149 | // w = 0;
|
---|
150 | // c = 0;
|
---|
151 | // while (c < field.length() && w < 2) {
|
---|
152 | // char ch = field.charAt(c);
|
---|
153 |
|
---|
154 | // ch = Character.toLowerCase(ch);
|
---|
155 | // if (Character.isLetter(ch)) {
|
---|
156 | // if (ch != 'a' && ch != 'e' && ch != 'i' &&
|
---|
157 | // ch != 'o' && ch != 'u') {
|
---|
158 | // directory.append(ch);
|
---|
159 | // w++;
|
---|
160 | // }
|
---|
161 | // }
|
---|
162 | // c ++;
|
---|
163 | // }
|
---|
164 | // return directory.toString();
|
---|
165 | // }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * The output directory should be (collection)/building/text/ for
|
---|
169 | * normal Greenstone builds.
|
---|
170 | *
|
---|
171 | * @param <code>String</code> the label to configure
|
---|
172 | * @param <code>String</code> the value...
|
---|
173 | */
|
---|
174 | public boolean configure(String label, String value)
|
---|
175 | {
|
---|
176 | if (label.equals(IndexerManager.outputDir)) {
|
---|
177 | this.outputDirectory = value;
|
---|
178 | this.pass = 0;
|
---|
179 |
|
---|
180 | // attempt to ensure that the text subdirectory exists
|
---|
181 | this.textDirectory = new File(outputDirectory, "text");
|
---|
182 | if (!textDirectory.exists()) {
|
---|
183 | if (!textDirectory.mkdir()) {
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else if (!textDirectory.isDirectory()) {
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 | this.textStem = this.textDirectory.getPath() + File.separator + INDEX_FILE_STEM;
|
---|
191 |
|
---|
192 | // Sign to the user which mg directory is being used...
|
---|
193 | System.out.println("Output MG directory is " + this.textStem);
|
---|
194 | }
|
---|
195 | else if (label.equals(IndexerInterface.GS2_INDEX_LABEL)) {
|
---|
196 | this.indexes.add(new MGIndex(value));
|
---|
197 | }
|
---|
198 |
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public boolean addIndex(String name, String level, String field)
|
---|
203 | {
|
---|
204 | MGIndex index = new MGIndex(name, level, field);
|
---|
205 | this.indexes.add(index);
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 |
|
---|
209 | private Node recurseDOM(DocumentInterface metsDoc, Node node,
|
---|
210 | AbstractStructure structure, StringBuffer textBuffer,
|
---|
211 | StringBuffer extraBuffer, String namespace)
|
---|
212 | //String name, String namespace, String field)
|
---|
213 | {
|
---|
214 | // send out the ctrl-c...if this is
|
---|
215 | if (structure.getStructureType().equals(METSDivision.DIVISION_TYPE)) {
|
---|
216 | // try doing this for all index types
|
---|
217 | if ((this.currentIndexName != null)) { // && this.level != null && this.level.equals(IndexerInterface.SECTION_LEVEL)) { //name.startsWith("s")) {
|
---|
218 | METSDivision division = (METSDivision) structure;
|
---|
219 |
|
---|
220 | // get the division metadata block
|
---|
221 | METSDescriptive descriptive;
|
---|
222 | String metadataId = division.getDefaultMetadataReference();
|
---|
223 | if (metadataId == null) {
|
---|
224 | descriptive = metsDoc.getDocumentMetadata().createDescriptive(division.getLabel());
|
---|
225 | division.addMetadataReference(descriptive.getID());
|
---|
226 | }
|
---|
227 | else {
|
---|
228 | // Get the descriptive item...
|
---|
229 | descriptive = metsDoc.getDocumentMetadata().getDescriptiveById(metadataId);
|
---|
230 | }
|
---|
231 |
|
---|
232 | descriptive.addMetadata("gsdl3", "mgseqno", this.overallName + "." + Integer.toString(this.sectionSeqNo));
|
---|
233 |
|
---|
234 | metsDoc.setChanged(true);
|
---|
235 | //metsDoc.setModified(true);
|
---|
236 | // System.out.println("Assigning " + this.sectionSeqNo + " to " + metsDoc.getID() + " " + division.getLabel());
|
---|
237 | } // section level
|
---|
238 |
|
---|
239 | // append an 'end of section' marker
|
---|
240 | //textBuffer.append(END_OF_SECTION);
|
---|
241 | this.sectionSeqNo ++;
|
---|
242 |
|
---|
243 | // for document-level indexes, always append an 'end of document' tag at the
|
---|
244 | // end of the document for each section. Otherwise, each section is followed
|
---|
245 | // by an end of document character. This ensures that all indexes use the
|
---|
246 | // same document numbering...
|
---|
247 | if (this.currentIndexLevel == null ||
|
---|
248 | this.currentIndexLevel.equals(IndexerInterface.DOCUMENT_LEVEL)) {
|
---|
249 | extraBuffer.append(END_OF_DOCUMENT);
|
---|
250 | }
|
---|
251 | else {
|
---|
252 | textBuffer.append(END_OF_DOCUMENT);
|
---|
253 | this.documentSeqNo ++;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // produce the body here for metadata output of divisions - in the case of
|
---|
257 | // text output, that will happen below...
|
---|
258 | if (!this.currentIndexField.equals("text"))
|
---|
259 | { METSDescriptive descriptive;
|
---|
260 |
|
---|
261 | METSDivision division = (METSDivision) structure;
|
---|
262 |
|
---|
263 | String metadataId = division.getDefaultMetadataReference();
|
---|
264 |
|
---|
265 | descriptive = metsDoc.getDocumentMetadata().getDescriptiveById(metadataId);
|
---|
266 | if (descriptive != null) {
|
---|
267 | List values = descriptive.getMetadata(namespace, this.currentIndexField);
|
---|
268 |
|
---|
269 | if (values != null) {
|
---|
270 | Iterator valueIter = values.iterator();
|
---|
271 | while (valueIter.hasNext()) {
|
---|
272 | String value = valueIter.next().toString();
|
---|
273 |
|
---|
274 | textBuffer.append(value);
|
---|
275 | if (valueIter.hasNext()) {
|
---|
276 | //textBuffer.append(END_OF_SECTION);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | // go through our children as required...
|
---|
285 | Iterator children = structure.getChildIterator();
|
---|
286 | Node startNode;
|
---|
287 | while (children.hasNext()) {
|
---|
288 | AbstractStructure child = (AbstractStructure) children.next();
|
---|
289 |
|
---|
290 | // get xpointer for child
|
---|
291 | // get start position node
|
---|
292 | if (metsDoc.getDocumentType() == "METS"){
|
---|
293 | startNode = ((METSDocument) metsDoc).getSectionStartNode((METSDivision) child);
|
---|
294 | } else {
|
---|
295 | startNode = ((HTMLDocument) metsDoc).getSectionStartNode((METSDivision) child);
|
---|
296 | }
|
---|
297 | //Node startNode = ((HTMLDocument) metsDoc).getSectionStartNode((METSDivision) child);
|
---|
298 |
|
---|
299 | // while this node isn't the child's start node, produce the HTML node text, if
|
---|
300 | // in text field mode...
|
---|
301 | if (this.currentIndexField.equals("text")) {
|
---|
302 | while (node != startNode) {
|
---|
303 | XPointer.printNode(node, textBuffer, false);
|
---|
304 |
|
---|
305 | // print buffer to node
|
---|
306 | node = XPointer.getNextNode(node, (this.currentIndexField.equals("text") ? textBuffer : null));
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | // recurse to child
|
---|
311 | node = this.recurseDOM(metsDoc, node, child, textBuffer, extraBuffer, namespace); // name, namespace, field);
|
---|
312 | } // while next child
|
---|
313 |
|
---|
314 | // close a document - the actual closing \B will be done by the main
|
---|
315 | // loop, so only a required \C is printed here...
|
---|
316 | if (structure.getStructureType().equals(METSStructure.STRUCTURE_TYPE)) {
|
---|
317 | while (node != null) {
|
---|
318 | if (this.currentIndexField.equals("text")) {
|
---|
319 | XPointer.printNode(node, textBuffer, false);
|
---|
320 | }
|
---|
321 | node = XPointer.getNextNode(node, (this.currentIndexField.equals("text") ? textBuffer : null));
|
---|
322 | }
|
---|
323 |
|
---|
324 | //textBuffer.append(END_OF_SECTION);
|
---|
325 | this.sectionSeqNo ++;
|
---|
326 |
|
---|
327 | }
|
---|
328 | return node;
|
---|
329 | }
|
---|
330 |
|
---|
331 | private String prepareDOM(DocumentInterface metsDoc, Document document, METSStructure structure, String namespace)
|
---|
332 | // String name, String namespace, String field)
|
---|
333 | { StringBuffer extraBuffer = new StringBuffer();
|
---|
334 | Node node = document.getDocumentElement();
|
---|
335 | StringBuffer textBuffer = new StringBuffer();
|
---|
336 |
|
---|
337 | this.recurseDOM(metsDoc, node, structure, textBuffer, extraBuffer, namespace); //name, namespace, field);
|
---|
338 | textBuffer.append(extraBuffer.toString());
|
---|
339 | return textBuffer.toString();
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Index a single document; the document interface can be used to extract individual
|
---|
344 | * metadata items etc. as required or desired and index those instead or as well as
|
---|
345 | * the body text of the document.
|
---|
346 | */
|
---|
347 | public boolean indexDocument(DocumentID docID, DocumentInterface document)
|
---|
348 | {
|
---|
349 | if (this.pass == 0) {
|
---|
350 | document.removeAllMetadata("gsdl3", "mgseqno");
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (!this.firstDocument) {
|
---|
354 | this.indexBuffer.append(END_OF_DOCUMENT);
|
---|
355 | mgPasses.processDocument(indexBuffer.toString());
|
---|
356 | this.indexBuffer.delete(0, this.indexBuffer.length());
|
---|
357 |
|
---|
358 | }
|
---|
359 |
|
---|
360 | String docText = null;
|
---|
361 |
|
---|
362 | int startSeqNo = this.sectionSeqNo;
|
---|
363 | this.sectionSeqNo ++;
|
---|
364 |
|
---|
365 | Document domDocument = document.getDOMDocument();
|
---|
366 | if (domDocument != null) {
|
---|
367 | System.err.println("dom doc is not null");
|
---|
368 | METSStructure sections = document.getDocumentStructure().getStructure("Section");
|
---|
369 | if (sections != null) {
|
---|
370 | System.err.println("sections are not null");
|
---|
371 | docText = this.prepareDOM(document, domDocument, sections, "gsdl3"); //this.name, "gsdl3", this.field);
|
---|
372 | // System.out.println(docText);
|
---|
373 | }
|
---|
374 | }
|
---|
375 | if (docText == null) {
|
---|
376 | System.err.println("dom doc or sections was null - asking for doc text");
|
---|
377 | if (this.currentIndexField.equals("text")) {
|
---|
378 | //docText = Character.toString(END_OF_DOCUMENT) + document.getDocumentText();
|
---|
379 | docText = document.getDocumentText();
|
---|
380 | }
|
---|
381 | else {
|
---|
382 | StringBuffer textBuffer = new StringBuffer();
|
---|
383 | //textBuffer.append(END_OF_DOCUMENT);
|
---|
384 | List values = document.getDocumentMetadataItem("gsdl3", this.currentIndexField);
|
---|
385 | if (values != null) {
|
---|
386 | Iterator valueIter = values.iterator();
|
---|
387 | while (valueIter.hasNext()) {
|
---|
388 | String value = valueIter.next().toString();
|
---|
389 |
|
---|
390 | textBuffer.append(value);
|
---|
391 | if (valueIter.hasNext()) {
|
---|
392 | //textBuffer.append(END_OF_SECTION);
|
---|
393 | // sectionSeqNo ++;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | }
|
---|
397 | else {
|
---|
398 | textBuffer.append("No data");
|
---|
399 | }
|
---|
400 | docText = textBuffer.toString();
|
---|
401 | }
|
---|
402 | sectionSeqNo ++;
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | this.indexBuffer.append(docText);
|
---|
407 | // remember that we're not on the first document,
|
---|
408 | this.firstDocument = false;
|
---|
409 | // assign the sequence number on the first pass only, and increment the sequence number.
|
---|
410 | if (this.pass == 0) {
|
---|
411 | document.addDocumentMetadata("gsdl3", "mgseqno", this.overallName+"."+Integer.toString(startSeqNo));
|
---|
412 | }
|
---|
413 | this.documentSeqNo += 1;
|
---|
414 |
|
---|
415 | return true;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Initialise the pass: open required files, check status
|
---|
420 | */
|
---|
421 | public boolean startPass(int passNumber) {
|
---|
422 |
|
---|
423 |
|
---|
424 | this.pass = passNumber;
|
---|
425 | this.firstDocument = true;
|
---|
426 | this.documentSeqNo = 1;
|
---|
427 | this.sectionSeqNo = 1;
|
---|
428 |
|
---|
429 | this.mgPasses = new MGPassesWrapper();
|
---|
430 | this.indexBuffer = new StringBuffer();
|
---|
431 | int indexNo = (this.pass - 2) / 2;
|
---|
432 | MGIndex index = null;
|
---|
433 | if (this.pass >= 2) {
|
---|
434 | index = (MGIndex) this.indexes.get(indexNo);
|
---|
435 | if (index.hasError()) {
|
---|
436 | // an error has already occurred for this index, don't continue
|
---|
437 | System.out.println("pass "+this.pass+": aborted due to errors in the previous pass");
|
---|
438 | return false;
|
---|
439 | }
|
---|
440 | // attempt to ensure that the text subdirectory exists
|
---|
441 | this.indexDirectory = new File(outputDirectory, index.getName());
|
---|
442 | if (!indexDirectory.exists()) {
|
---|
443 | if (!indexDirectory.mkdir()) {
|
---|
444 | return false;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | else if (!indexDirectory.isDirectory()) {
|
---|
448 | return false;
|
---|
449 | }
|
---|
450 |
|
---|
451 | this.currentIndexLevel = index.getLevel();
|
---|
452 | this.currentIndexField = index.getField();
|
---|
453 | this.currentIndexName = index.getName();
|
---|
454 |
|
---|
455 | if (this.currentIndexLevel == null || this.currentIndexField == null ) {
|
---|
456 | System.out.println("invalid index - level or field was null");
|
---|
457 | return false;
|
---|
458 | }
|
---|
459 | this.indexStem = this.indexDirectory.getPath() + File.separatorChar + INDEX_FILE_STEM; // TODO: modify for index
|
---|
460 | if (this.pass % 2 == 1) {
|
---|
461 | this.currentIndexName = null; // why???
|
---|
462 | }
|
---|
463 | }
|
---|
464 | else {
|
---|
465 |
|
---|
466 | this.currentIndexField = "text";
|
---|
467 | this.currentIndexLevel = "section";
|
---|
468 | this.currentIndexName = null;
|
---|
469 | }
|
---|
470 |
|
---|
471 | // get the parameters for this execution of mg_passes
|
---|
472 | mgPasses.setFileName((this.pass < 2 ? this.textDirectory.toString() : this.indexDirectory.toString())+File.separator+ "index");
|
---|
473 | if (!Misc.isWindows()) {
|
---|
474 | mgPasses.setBasePath("/");
|
---|
475 | }
|
---|
476 | int mgPass = this.pass < 2 ? this.pass : ((this.pass % 2) + 2);
|
---|
477 |
|
---|
478 | mgPasses.setBufferSize(100000);
|
---|
479 |
|
---|
480 | switch (mgPass) {
|
---|
481 | case 0:
|
---|
482 | // -b 100000 -T1
|
---|
483 | mgPasses.addPass(MGPassesWrapper.TEXT_PASS_1);
|
---|
484 |
|
---|
485 |
|
---|
486 | break;
|
---|
487 |
|
---|
488 | case 1:
|
---|
489 | // -b 100000 -T2
|
---|
490 | mgPasses.addPass(MGPassesWrapper.TEXT_PASS_2);
|
---|
491 | break;
|
---|
492 |
|
---|
493 | case 2:
|
---|
494 | // -b 100000 -2 -m 32 -s 0 -G -t 10 -N1
|
---|
495 | mgPasses.addPass(MGPassesWrapper.INDEX_PASS_1);
|
---|
496 | mgPasses.setInvfLevel(MGPassesWrapper.INVF_LEVEL_2);
|
---|
497 | mgPasses.setStemOptions(MGPassesWrapper.STEMMER_ENGLISH, MGPassesWrapper.NO_STEM_OR_CASE);
|
---|
498 | mgPasses.setInversionMemLimit(32);
|
---|
499 | mgPasses.ignoreSGMLTags(true);
|
---|
500 | break;
|
---|
501 |
|
---|
502 | case 3:
|
---|
503 | // -b 100000 -2 -c 3 -G -t 10 -N2
|
---|
504 | mgPasses.addPass(MGPassesWrapper.INDEX_PASS_2);
|
---|
505 | mgPasses.setInvfLevel(MGPassesWrapper.INVF_LEVEL_2);
|
---|
506 | mgPasses.ignoreSGMLTags(true);
|
---|
507 | break;
|
---|
508 | }
|
---|
509 |
|
---|
510 | mgPasses.init();
|
---|
511 | System.out.println("Pass " + this.pass);
|
---|
512 | return true;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Complete a pass - reset file counters, close files, etc.
|
---|
517 | */
|
---|
518 | public boolean endPass(int passNumber) {
|
---|
519 | Process p;
|
---|
520 |
|
---|
521 | int indexNo = (passNumber - 2) / 2;
|
---|
522 | MGIndex index = null;
|
---|
523 | if (passNumber >= 2) {
|
---|
524 | index = (MGIndex) this.indexes.get(indexNo);
|
---|
525 | }
|
---|
526 | try {
|
---|
527 | this.indexBuffer.append(END_OF_DOCUMENT);
|
---|
528 | mgPasses.processDocument(indexBuffer.toString());
|
---|
529 | this.indexBuffer.delete(0, this.indexBuffer.length());
|
---|
530 | Thread.sleep(1000); // what for??
|
---|
531 | }
|
---|
532 | catch (InterruptedException ex) {
|
---|
533 | System.out.println(ex);
|
---|
534 | }
|
---|
535 | mgPasses.finish();
|
---|
536 | try {
|
---|
537 | Thread.sleep(1000);
|
---|
538 | } catch (Exception e) {}
|
---|
539 |
|
---|
540 | int exit_value = 0;
|
---|
541 | System.out.println("Pass " + this.pass + " completed with " + exit_value);
|
---|
542 | if (exit_value !=0) {
|
---|
543 | //assume something has gone wrong, don't continue
|
---|
544 | if (index != null) {
|
---|
545 | index.setError(true);
|
---|
546 | return false;
|
---|
547 | }
|
---|
548 | }
|
---|
549 | int mgPass = this.pass < 2 ? this.pass : ((this.pass % 2) + 2);
|
---|
550 | String osextra = "";
|
---|
551 | if (!Misc.isWindows()) {
|
---|
552 | osextra = " -d / ";
|
---|
553 | }
|
---|
554 |
|
---|
555 | switch (mgPass) {
|
---|
556 |
|
---|
557 | case 0:
|
---|
558 | System.out.println("Compressing dictionary");
|
---|
559 | exit_value = Processing.runProcess("mg_compression_dict -f " + this.textDirectory.toString()+File.separator+"index" + osextra + " -S -H -2 -k 5120");
|
---|
560 | if (exit_value == 0) {
|
---|
561 | System.out.println("Compressed dictionary successfully written");
|
---|
562 | } else {
|
---|
563 | System.err.println("Error from mg_compression_dict: " + exit_value);
|
---|
564 | index.setError(true);
|
---|
565 |
|
---|
566 | return false;
|
---|
567 | }
|
---|
568 | break;
|
---|
569 |
|
---|
570 | case 2:
|
---|
571 | System.out.println("Creating perfect hash");
|
---|
572 | exit_value = Processing.runProcess("mg_perf_hash_build -f " + this.indexDirectory.toString()+File.separator+ "index"+osextra);
|
---|
573 | if (exit_value ==0) {
|
---|
574 | System.out.println("Perfect hashes completed");
|
---|
575 | } else {
|
---|
576 | System.err.println("Unable to build the perfect hash");
|
---|
577 | index.setError(true);
|
---|
578 | return false;
|
---|
579 | }
|
---|
580 | break;
|
---|
581 |
|
---|
582 | case 3:
|
---|
583 | System.out.println("Writing weights file");
|
---|
584 | exit_value = Processing.runProcess("mg_weights_build -f " + this.indexStem + " -t " + this.textStem + osextra);
|
---|
585 | if (exit_value ==0) {
|
---|
586 | System.out.println("Weights file successfully written");
|
---|
587 | } else {
|
---|
588 | System.err.println("Unable to create weights file");
|
---|
589 | index.setError(true);
|
---|
590 | return false;
|
---|
591 | }
|
---|
592 |
|
---|
593 | System.out.println("Creating inverted dictionary");
|
---|
594 | exit_value = Processing.runProcess("mg_invf_dict -f " + this.indexDirectory.toString()+File.separator+"index" + osextra);
|
---|
595 | if (exit_value ==0) {
|
---|
596 | System.out.println("Inverted dictionary file successfully written");
|
---|
597 | } else {
|
---|
598 | System.out.println("Unable to create inverted dictionary file");
|
---|
599 | index.setError(true);
|
---|
600 | return false;
|
---|
601 | }
|
---|
602 |
|
---|
603 | System.out.println("Creating Stem indexes");
|
---|
604 | exit_value = Processing.runProcess("mg_stem_idx -b 4096 -s1 -f " + this.indexDirectory.toString()+File.separator+"index"+osextra);
|
---|
605 | if (exit_value == 0) {
|
---|
606 | System.out.println("Stemmed index 1 successfully written");
|
---|
607 | } else {
|
---|
608 | System.out.println("Unable to create stemmed index 1");
|
---|
609 | index.setError(true);
|
---|
610 | return false;
|
---|
611 | }
|
---|
612 |
|
---|
613 | exit_value = Processing.runProcess("mg_stem_idx -b 4096 -s2 -f " + this.indexDirectory.toString()+File.separator+"index"+osextra);
|
---|
614 | if (exit_value == 0) {
|
---|
615 | System.out.println("Stemmed index 2 successfully written");
|
---|
616 | } else {
|
---|
617 | System.out.println("Unable to create stemmed index 2");
|
---|
618 | index.setError(true);
|
---|
619 | return false;
|
---|
620 | }
|
---|
621 | exit_value = Processing.runProcess("mg_stem_idx -b 4096 -s3 -f " + this.indexDirectory.toString()+File.separator+"index"+osextra);
|
---|
622 | if (exit_value == 0) {
|
---|
623 | System.out.println("Stemmed index 3 successfully written");
|
---|
624 | } else {
|
---|
625 | System.out.println("Unable to create stemmed index 3");
|
---|
626 | index.setError(true);
|
---|
627 | return false;
|
---|
628 | }
|
---|
629 |
|
---|
630 | break;
|
---|
631 | } // switch
|
---|
632 |
|
---|
633 | mgPasses = null;
|
---|
634 | return true;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Do any tidying up
|
---|
639 | */
|
---|
640 | public void tidyup()
|
---|
641 | {
|
---|
642 | }
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Return the number of passes required for this index.
|
---|
646 | */
|
---|
647 | public int getNumberOfPasses()
|
---|
648 | { return 2 + this.indexes.size() * 2;
|
---|
649 | }
|
---|
650 |
|
---|
651 | public boolean addServiceDescriptions(org.w3c.dom.Element service_rack_list) {
|
---|
652 | Document doc = service_rack_list.getOwnerDocument();
|
---|
653 |
|
---|
654 | // generate the list of indexes
|
---|
655 | Element index_list = doc.createElement(GSXML.INDEX_ELEM+GSXML.LIST_MODIFIER);
|
---|
656 | boolean found_index = false;
|
---|
657 | String def_index = ""; // the default index will just be the first one created for now.
|
---|
658 | for (int i=0; i<this.indexes.size(); i++) {
|
---|
659 | MGIndex index = (MGIndex)this.indexes.get(i);
|
---|
660 | if (!index.hasError()) {
|
---|
661 | Element e = doc.createElement(GSXML.INDEX_ELEM);
|
---|
662 | e.setAttribute(GSXML.NAME_ATT, index.getName());
|
---|
663 | index_list.appendChild(e);
|
---|
664 | if (found_index == false) {
|
---|
665 | // this is the first index
|
---|
666 | found_index = true;
|
---|
667 | def_index = index.getName();
|
---|
668 | }
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | if (!found_index) {
|
---|
673 | // no indexes were able to be created, so we can't use them or the text
|
---|
674 | return false;
|
---|
675 | }
|
---|
676 | Element default_index = doc.createElement("defaultIndex");
|
---|
677 | default_index.setAttribute(GSXML.NAME_ATT, def_index);
|
---|
678 | Element base_index_name = doc.createElement("baseIndexPrefix");
|
---|
679 | base_index_name.setAttribute(GSXML.NAME_ATT, overallName);
|
---|
680 | Element index_stem = doc.createElement("indexStem");
|
---|
681 | index_stem.setAttribute(GSXML.NAME_ATT, "index");
|
---|
682 |
|
---|
683 | Element search_service_elem = doc.createElement(GSXML.SERVICE_CLASS_ELEM);
|
---|
684 | Element retrieve_service_elem = doc.createElement(GSXML.SERVICE_CLASS_ELEM);
|
---|
685 | service_rack_list.appendChild(search_service_elem);
|
---|
686 | service_rack_list.appendChild(retrieve_service_elem);
|
---|
687 |
|
---|
688 | search_service_elem.setAttribute(GSXML.NAME_ATT, "GS3MGSearch");
|
---|
689 | search_service_elem.appendChild(index_list);
|
---|
690 | search_service_elem.appendChild(default_index);
|
---|
691 | search_service_elem.appendChild(base_index_name);
|
---|
692 | search_service_elem.appendChild(index_stem);
|
---|
693 |
|
---|
694 | retrieve_service_elem.setAttribute(GSXML.NAME_ATT, "GS3MGRetrieve");
|
---|
695 | retrieve_service_elem.appendChild(default_index.cloneNode(true));
|
---|
696 | retrieve_service_elem.appendChild(base_index_name.cloneNode(true));
|
---|
697 | retrieve_service_elem.appendChild(index_stem.cloneNode(true));
|
---|
698 |
|
---|
699 | return true;
|
---|
700 | }
|
---|
701 |
|
---|
702 | }
|
---|
703 |
|
---|