Changeset 6496


Ignore:
Timestamp:
2004-01-14T16:26:50+13:00 (20 years ago)
Author:
cs025
Message:

Added AZList; support sorting within categories.

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  
    2929  AbstractHierarchyNode parent; // the parent of the node
    3030
     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
    3149  /**
    3250   *  Simple node
     
    5371    this.matches    = new ArrayList();
    5472    this.matches.add(this.id);
     73    this.matches.add(this.name);
    5574  }
    5675   
     
    88107  }
    89108
    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    }
    92140  }
    93141
     
    185233   *         classifications that it fell within.
    186234   */
    187   abstract public void getClassifications(DocumentID documentID, List values,
     235  abstract public void getClassifications(DocumentID documentID, List values, String sortKey,
    188236                      ClassifierObserverInterface observer);
    189237
     
    280328    // note the child documents...
    281329    iterator = this.childDocs.iterator();
     330    int order = 1;
    282331    while (iterator.hasNext()) {
    283       DocumentID docId = (DocumentID) iterator.next();
     332      AbstractHierarchyDocument hierarchyDoc = (AbstractHierarchyDocument) iterator.next();
     333      DocumentID docId = hierarchyDoc.getID();
     334
    284335      insert = new GS3SQLInsert("classdocuments");
    285336      insert.addValue("ClassifyRef", Integer.toString(classifyRef), GS3SQLField.INTEGER_TYPE);
    286337      insert.addValue("DocID", docId.toString());
     338      insert.addValue("DocOrder", Integer.toString(order), GS3SQLField.INTEGER_TYPE);
    287339
    288340      connection.execute(insert.toString());
     341
     342      order ++;
    289343    }
    290344    return true;
    291345  }
    292346}
     347
     348
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/ClassifierManager.java

    r6104 r6496  
    8282       
    8383        if (document.isModified()) {
     84          //          System.out.println("Writing document " + document.getID());
    8485          this.documents.modifiedDocument(document);
    8586        }
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/GS2HierarchyClassifier.java

    r6351 r6496  
    3333    public void recordClassification(String label)
    3434    { 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);
    3637    }
    3738
     
    6566     *         classifications that it fell within.
    6667     */
    67     public void getClassifications(DocumentID documentID, List values,
     68    public void getClassifications(DocumentID documentID, List values, String sortKey,
    6869                   ClassifierObserverInterface observer)
    6970    { // Go through the list of values, and if this node matches, record
     
    7374      while (valueList.hasNext())
    7475      { 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);
    7778    }
    7879      }
     
    8283
    8384      while (childList.hasNext())
    84       { ((GS2HierarchyNode) childList.next()).getClassifications(documentID, values, observer);
     85      { ((GS2HierarchyNode) childList.next()).getClassifications(documentID, values, sortKey, observer);
    8586      }
    8687    }
     
    238239    while (thisField.hasNext())
    239240    { String fieldName = thisField.next().toString();
    240            
     241
    241242      // ...get the values for that field...
    242243      List values = document.getDocumentMetadataItem(fieldName);
     
    253254      }
    254255
     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
    255266      // ...and send them to the classifier
    256       this.hierarchy.getClassifications(documentID, values, classifyObserver);
     267      this.hierarchy.getClassifications(documentID, values, sortKey, classifyObserver);
    257268    }
    258269    return true;
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/HierarchyClassifier.java

    r6351 r6496  
    2121public class HierarchyClassifier implements ClassifierInterface
    2222{
    23   class HierarchyClassifierObserver
     23  class HierarchyClassifierObserver implements ClassifierObserverInterface
    2424  { List              classifications;
    2525    DocumentInterface document;
     
    3838  }
    3939
    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  { /**
    5542     *  Simple node
    5643     */
    5744    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();
    6446    }
    6547   
    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)
    11450    { Iterator valueList = values.iterator();
    11551
     
    11753      { if (this.isMatch(valueList.next().toString()))
    11854    { 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...
    12260      Iterator childList = this.childNodes.iterator();
    12361
    12462      while (childList.hasNext())
    125       { ((HierarchyNode) childList.next()).getClassifications(documentID, values, observer);
     63      { ((HierarchyNode) childList.next()).getClassifications(documentID, values, sortKey, observer);
    12664      }
    12765    }
     
    13775    StringBuffer match;
    13876    boolean inElement;
    139     HierarchyNode rootNode = null;
    140     HierarchyNode currentNode = null;
     77    AbstractHierarchyNode rootNode = null;
     78    AbstractHierarchyNode currentNode = null;
    14179    List          rootNodes = null;
    14280
     
    195133      }
    196134      else if (localName.equals("Path"))
    197       { this.currentNode.setPath(XMLTools.cleanString(this.path.toString()));
     135      { this.currentNode.setID(XMLTools.cleanString(this.path.toString()));
    198136        this.path = null;
    199137      }
     
    227165    }
    228166
    229     public HierarchyNode getHierarchy()
     167    public AbstractHierarchyNode getHierarchy()
    230168    { return this.rootNode;
    231169    }
    232170  }
    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)
    239178  {
    240179    try
     
    250189      this.hierarchy = handler.getHierarchy();
    251190      this.fields = fields;
    252       this.sort   = sort;
     191      this.sortBy = sortBy;
    253192    }
    254193    catch (SAXException saxException)
     
    265204  public void configure(List parameters)
    266205  {
     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    }
    267218  }
    268219
    269220  public void setDatabase(GS3SQLConnection database)
    270   {
     221  { this.database = database;
    271222  }
    272223
     
    298249      }
    299250
     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
    300266      // ...and send them to the classifier
    301       this.hierarchy.getClassifications(documentID, values, classifyObserver);
     267      this.hierarchy.getClassifications(documentID, values, sortKey, classifyObserver);
    302268    }
    303269    return true;
     
    318284  public void completeClassification()
    319285  { // TODO: store the classifications to file...
     286    if (this.hierarchy != null) {
     287      this.hierarchy.writeSQL(this.database);
     288    }
    320289  }
    321290}
Note: See TracChangeset for help on using the changeset viewer.