Changeset 6496
- Timestamp:
- 2004-01-14T16:26:50+13:00 (20 years ago)
- Location:
- trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/AbstractHierarchyNode.java
r6351 r6496 29 29 AbstractHierarchyNode parent; // the parent of the node 30 30 31 class AbstractHierarchyDocument 32 { DocumentID documentId; 33 String sortKey; 34 35 public AbstractHierarchyDocument(DocumentID documentId, String sortKey) 36 { this.documentId = documentId; 37 this.sortKey = sortKey; 38 } 39 40 public DocumentID getID() 41 { return this.documentId; 42 } 43 44 public String toString() 45 { return this.sortKey; 46 } 47 } 48 31 49 /** 32 50 * Simple node … … 53 71 this.matches = new ArrayList(); 54 72 this.matches.add(this.id); 73 this.matches.add(this.name); 55 74 } 56 75 … … 88 107 } 89 108 90 public void addDocument(DocumentID document) 91 { this.childDocs.add(document); 109 public void addDocument(DocumentID document, String sortKey) 110 { this.childDocs.add(new AbstractHierarchyDocument(document, sortKey)); 111 112 if (sortKey != null) { 113 int first = 0; 114 int last = this.childDocs.size() - 1; 115 116 while (first != last) { 117 int at = (first + last) / 2; 118 119 if (this.childDocs.get(at) == null) { 120 last = at; 121 continue; 122 } 123 124 if (this.childDocs.get(at).toString().compareTo(sortKey) > 0) { 125 last = at; 126 } 127 else { 128 first = at + 1; 129 } 130 } 131 132 Object newItem = this.childDocs.get(this.childDocs.size() - 1); 133 last = this.childDocs.size() - 1; 134 while (last > first) { 135 this.childDocs.set(last, this.childDocs.get(last-1)); 136 last --; 137 } 138 this.childDocs.set(first, newItem); 139 } 92 140 } 93 141 … … 185 233 * classifications that it fell within. 186 234 */ 187 abstract public void getClassifications(DocumentID documentID, List values, 235 abstract public void getClassifications(DocumentID documentID, List values, String sortKey, 188 236 ClassifierObserverInterface observer); 189 237 … … 280 328 // note the child documents... 281 329 iterator = this.childDocs.iterator(); 330 int order = 1; 282 331 while (iterator.hasNext()) { 283 DocumentID docId = (DocumentID) iterator.next(); 332 AbstractHierarchyDocument hierarchyDoc = (AbstractHierarchyDocument) iterator.next(); 333 DocumentID docId = hierarchyDoc.getID(); 334 284 335 insert = new GS3SQLInsert("classdocuments"); 285 336 insert.addValue("ClassifyRef", Integer.toString(classifyRef), GS3SQLField.INTEGER_TYPE); 286 337 insert.addValue("DocID", docId.toString()); 338 insert.addValue("DocOrder", Integer.toString(order), GS3SQLField.INTEGER_TYPE); 287 339 288 340 connection.execute(insert.toString()); 341 342 order ++; 289 343 } 290 344 return true; 291 345 } 292 346 } 347 348 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/ClassifierManager.java
r6104 r6496 82 82 83 83 if (document.isModified()) { 84 // System.out.println("Writing document " + document.getID()); 84 85 this.documents.modifiedDocument(document); 85 86 } -
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/GS2HierarchyClassifier.java
r6351 r6496 33 33 public void recordClassification(String label) 34 34 { this.document.addDocumentMetadata("gsdl3", "classified", label); 35 System.out.println("Assigned document " + this.document.getID().toString() + " to " + label); 35 this.document.setModified(true); 36 // System.out.println("Assigned document " + this.document.getID().toString() + " to " + label); 36 37 } 37 38 … … 65 66 * classifications that it fell within. 66 67 */ 67 public void getClassifications(DocumentID documentID, List values, 68 public void getClassifications(DocumentID documentID, List values, String sortKey, 68 69 ClassifierObserverInterface observer) 69 70 { // Go through the list of values, and if this node matches, record … … 73 74 while (valueList.hasNext()) 74 75 { if (this.isMatch(valueList.next().toString())) 75 { observer.recordClassification(this. name);76 this.addDocument(documentID );76 { observer.recordClassification(this.id); 77 this.addDocument(documentID, sortKey); 77 78 } 78 79 } … … 82 83 83 84 while (childList.hasNext()) 84 { ((GS2HierarchyNode) childList.next()).getClassifications(documentID, values, observer);85 { ((GS2HierarchyNode) childList.next()).getClassifications(documentID, values, sortKey, observer); 85 86 } 86 87 } … … 238 239 while (thisField.hasNext()) 239 240 { String fieldName = thisField.next().toString(); 240 241 241 242 // ...get the values for that field... 242 243 List values = document.getDocumentMetadataItem(fieldName); … … 253 254 } 254 255 256 // get the sort key for the metadata item if possible 257 String sortKey = null; 258 259 if (this.sortBy != null) { 260 List sortKeys = document.getDocumentMetadataItem(this.sortBy); 261 if (sortKeys != null && sortKeys.size() > 0) { 262 sortKey = sortKeys.get(0).toString(); 263 } 264 } 265 255 266 // ...and send them to the classifier 256 this.hierarchy.getClassifications(documentID, values, classifyObserver);267 this.hierarchy.getClassifications(documentID, values, sortKey, classifyObserver); 257 268 } 258 269 return true; -
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/HierarchyClassifier.java
r6351 r6496 21 21 public class HierarchyClassifier implements ClassifierInterface 22 22 { 23 class HierarchyClassifierObserver 23 class HierarchyClassifierObserver implements ClassifierObserverInterface 24 24 { List classifications; 25 25 DocumentInterface document; … … 38 38 } 39 39 40 class HierarchyNode 41 { String descriptor; // the textual descriptor used on screen or long-hand 42 String path; // the index number, letter assignment or other item 43 // used to identify the position of the item in the 44 // hierarchy 45 String name; // an identifier used by the GLI for maintenance 46 // purposes; this plays no active role in the 47 // rebuilding process (at the moment) 48 List childNodes; // the child classification nodes of this node 49 List childDocs; // the child documents of this node 50 List matches; // the other metadata values that may be matched 51 // against the classifier 52 HierarchyNode parent; // the parent of the node 53 54 /** 40 class HierarchyNode extends AbstractHierarchyNode 41 { /** 55 42 * Simple node 56 43 */ 57 44 public HierarchyNode() 58 { this.descriptor = null; 59 this.name = null; 60 this.path = null; 61 this.childNodes = new ArrayList(); 62 this.childDocs = new ArrayList(); 63 this.parent = null; 45 { super(); 64 46 } 65 47 66 public void addChild(HierarchyNode child) 67 { this.childNodes.add(child); 68 child.setParent(this); 69 } 70 71 public void addDocument(DocumentID document) 72 { this.childDocs.add(document); 73 } 74 75 public void setParent(HierarchyNode parent) 76 { this.parent = parent; 77 } 78 79 public HierarchyNode getParent() 80 { return this.parent; 81 } 82 83 public void setDescriptor(String descriptor) 84 { this.descriptor = descriptor; 85 } 86 87 public void setPath(String path) 88 { this.path = path; 89 } 90 91 public void setName(String name) 92 { this.name = name; 93 } 94 95 public void addMatch(String match) 96 { this.matches.add(match); 97 } 98 99 public boolean isMatch (String toMatch) 100 { Iterator thisMatch = this.matches.iterator(); 101 102 while (thisMatch.hasNext()) 103 { String thisMatchText = thisMatch.next().toString(); 104 105 if (thisMatchText.equals(toMatch)) 106 { return true; 107 } 108 } 109 return false; 110 } 111 112 public void getClassifications(DocumentID documentID, List values, 113 HierarchyClassifierObserver observer) 48 public void getClassifications(DocumentID documentID, List values, String sortKey, 49 ClassifierObserverInterface observer) 114 50 { Iterator valueList = values.iterator(); 115 51 … … 117 53 { if (this.isMatch(valueList.next().toString())) 118 54 { observer.recordClassification(this.name); 119 this.addDocument(documentID); 120 } 121 } 55 this.addDocument(documentID, sortKey); 56 } 57 } 58 59 // recurse into the child nodes for them to do the same... 122 60 Iterator childList = this.childNodes.iterator(); 123 61 124 62 while (childList.hasNext()) 125 { ((HierarchyNode) childList.next()).getClassifications(documentID, values, observer);63 { ((HierarchyNode) childList.next()).getClassifications(documentID, values, sortKey, observer); 126 64 } 127 65 } … … 137 75 StringBuffer match; 138 76 boolean inElement; 139 HierarchyNode rootNode = null;140 HierarchyNode currentNode = null;77 AbstractHierarchyNode rootNode = null; 78 AbstractHierarchyNode currentNode = null; 141 79 List rootNodes = null; 142 80 … … 195 133 } 196 134 else if (localName.equals("Path")) 197 { this.currentNode.set Path(XMLTools.cleanString(this.path.toString()));135 { this.currentNode.setID(XMLTools.cleanString(this.path.toString())); 198 136 this.path = null; 199 137 } … … 227 165 } 228 166 229 public HierarchyNode getHierarchy()167 public AbstractHierarchyNode getHierarchy() 230 168 { return this.rootNode; 231 169 } 232 170 } 233 234 private HierarchyNode hierarchy; 235 private List fields; 236 private String sort; 237 238 public HierarchyClassifier(File basefile, List fields, String sort) 171 172 private GS3SQLConnection database; 173 private AbstractHierarchyNode hierarchy; 174 private List fields; 175 private String sortBy; 176 177 public HierarchyClassifier(File basefile, List fields, String sortBy) 239 178 { 240 179 try … … 250 189 this.hierarchy = handler.getHierarchy(); 251 190 this.fields = fields; 252 this.sort = sort;191 this.sortBy = sortBy; 253 192 } 254 193 catch (SAXException saxException) … … 265 204 public void configure(List parameters) 266 205 { 206 Iterator iterator = parameters.iterator(); 207 while (iterator.hasNext()) { 208 String param = iterator.next().toString(); 209 if (param.equals("-metadata")) { 210 if (iterator.hasNext()) { 211 String field = iterator.next().toString(); 212 if (field != null && field.length() > 0) { 213 this.fields.add(field); 214 } 215 } 216 } 217 } 267 218 } 268 219 269 220 public void setDatabase(GS3SQLConnection database) 270 { 221 { this.database = database; 271 222 } 272 223 … … 298 249 } 299 250 251 if (documentID == null) { 252 System.out.println("Bad documentID"); 253 continue; 254 } 255 256 // get the sort key for the metadata item if possible 257 String sortKey = null; 258 259 if (this.sortBy != null) { 260 List sortKeys = document.getDocumentMetadataItem(this.sortBy); 261 if (sortKeys != null && sortKeys.size() > 0) { 262 sortKey = sortKeys.get(0).toString(); 263 } 264 } 265 300 266 // ...and send them to the classifier 301 this.hierarchy.getClassifications(documentID, values, classifyObserver);267 this.hierarchy.getClassifications(documentID, values, sortKey, classifyObserver); 302 268 } 303 269 return true; … … 318 284 public void completeClassification() 319 285 { // TODO: store the classifications to file... 286 if (this.hierarchy != null) { 287 this.hierarchy.writeSQL(this.database); 288 } 320 289 } 321 290 }
Note:
See TracChangeset
for help on using the changeset viewer.