1 | package org.greenstone.gsdl3.gs3build.classifier;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.Iterator;
|
---|
5 |
|
---|
6 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
|
---|
7 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
|
---|
8 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentList;
|
---|
9 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
10 |
|
---|
11 | public class ClassifierManager
|
---|
12 | {
|
---|
13 | DocumentList documents;
|
---|
14 | ClassifierInterface [] list;
|
---|
15 | int size;
|
---|
16 | int used;
|
---|
17 | GS3SQLConnection database;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Instantiate a classifier manager that will classify the
|
---|
21 | * documents in a given <code>DocumentList</code> object.
|
---|
22 | * The list of documents should not change during the
|
---|
23 | * <code>classifyDocuments</code> function, and the documents
|
---|
24 | * themselves may be altered at that time.
|
---|
25 | *
|
---|
26 | * @param <code>DocumentList</code> a provider of documents for
|
---|
27 | * classification.
|
---|
28 | */
|
---|
29 | public ClassifierManager(DocumentList documentList, GS3SQLConnection database)
|
---|
30 | { this.list = new ClassifierInterface[10];
|
---|
31 | this.size = 10;
|
---|
32 | this.used = 0;
|
---|
33 | this.documents = documentList;
|
---|
34 | this.database = database;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Add a classifier to the end of the list of classifiers...
|
---|
39 | *
|
---|
40 | * @param <code>ClassifierInterface</code> the classifier.
|
---|
41 | */
|
---|
42 | public void addClassifier(ClassifierInterface classifier)
|
---|
43 | { this.ensureSize(this.used + 1);
|
---|
44 | this.list[this.used] = classifier;
|
---|
45 | this.used ++;
|
---|
46 |
|
---|
47 | classifier.setDatabase(this.database);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Classify an individual document - the document itself may be written
|
---|
52 | * to in consequence, in which case the document <strong>must</strong>
|
---|
53 | * be rewritten to disk. This function does <strong>not</strong> do the
|
---|
54 | * writing itself.
|
---|
55 | *
|
---|
56 | * @param <code>DocumentID</code> the document's identifier.
|
---|
57 | * @param <code>DocumentInterface</code> the document itself.
|
---|
58 | */
|
---|
59 | public void classifyDocument(DocumentID docId, DocumentInterface document)
|
---|
60 | { for (int i = 0; i < this.used; i ++)
|
---|
61 | { this.list[i].classifyDocument(docId, document);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Classify all the documents that the document list is able to provide...
|
---|
67 | */
|
---|
68 | public void classifyDocuments()
|
---|
69 | { for (int i = 0; i < this.used; i ++)
|
---|
70 | { for (int p = 0; p < this.list[i].getClassifierPasses(); p ++)
|
---|
71 | { this.list[i].startClassifierPass(p);
|
---|
72 |
|
---|
73 | Iterator iterator = this.documents.iterator();
|
---|
74 |
|
---|
75 | while (iterator.hasNext()) {
|
---|
76 | DocumentInterface document = (DocumentInterface) iterator.next();
|
---|
77 |
|
---|
78 | if (document.isIndexed()) {
|
---|
79 | if (!this.list[i].classifyDocument(document.getID(), document)) {
|
---|
80 | System.out.println("Ending document");
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (document.isModified()) {
|
---|
84 | // System.out.println("Writing document " + document.getID());
|
---|
85 | this.documents.modifiedDocument(document);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | this.list[i].endClassifierPass(p);
|
---|
90 | }
|
---|
91 | this.list[i].completeClassification();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void ensureSize(int size)
|
---|
96 | { while (size >= this.size)
|
---|
97 | { ClassifierInterface newList [] = new ClassifierInterface[this.size*2];
|
---|
98 | this.size *= 2;
|
---|
99 | System.arraycopy(this.list, 0, newList, 0, this.size);
|
---|
100 | this.list = newList;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static ClassifierInterface loadClassifier(String classifierName, List params)
|
---|
105 | {
|
---|
106 | try {
|
---|
107 | ClassifierInterface classifier =
|
---|
108 | (ClassifierInterface) Class.forName("org.greenstone.gsdl3.gs3build.classifier."+classifierName).newInstance();
|
---|
109 | if (classifier != null) {
|
---|
110 | classifier.configure(params);
|
---|
111 | }
|
---|
112 | return classifier;
|
---|
113 | }
|
---|
114 | catch (ClassNotFoundException ex) {
|
---|
115 | System.err.println("Attempt to load classifier " + classifierName + " which does not exist");
|
---|
116 | }
|
---|
117 | catch (InstantiationException instEx) {
|
---|
118 | System.err.println("Unable to create classifier " + classifierName);
|
---|
119 | }
|
---|
120 | catch (IllegalAccessException accessEx) {
|
---|
121 | System.err.println("Unable to instantiate classifier " + classifierName + " due to permissions problems " + accessEx.toString());
|
---|
122 | }
|
---|
123 | return null;
|
---|
124 | }
|
---|
125 | }
|
---|