Revision: 16712
          http://sourceforge.net/p/gate/code/16712
Author:   johann_p
Date:     2013-06-28 15:10:47 +0000 (Fri, 28 Jun 2013)
Log Message:
-----------
Add AnnotationSet getStartingAt(long) to AnnotationSetImpl,
add List<Annotation> inDocumentOrder() to AnnotationSetImpl
and also the interface AnnotationSet, add tests for these
methods.

Modified Paths:
--------------
    gate/trunk/src/gate/AnnotationSet.java
    gate/trunk/src/gate/annotation/AnnotationSetImpl.java
    gate/trunk/src/gate/annotation/TestAnnotation.java

Modified: gate/trunk/src/gate/AnnotationSet.java
===================================================================
--- gate/trunk/src/gate/AnnotationSet.java      2013-06-19 16:26:05 UTC (rev 
16711)
+++ gate/trunk/src/gate/AnnotationSet.java      2013-06-28 15:10:47 UTC (rev 
16712)
@@ -21,6 +21,7 @@
 import gate.util.InvalidOffsetException;
 
 import java.io.Serializable;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -225,6 +226,17 @@
   public AnnotationSet getContained(Long startOffset, Long endOffset);
 
   /**
+   * Return a list of annotations sorted by increasing start offset, i.e. in 
the order
+   * they appear in the document. If more than one annotation starts at a 
specific offset
+   * the order of these annotations is unspecified.
+   * 
+   * @return a list of annotations ordered by increasing start offset. If a 
positional
+   * index does not exist, it is created.
+   */
+  public List<Annotation> inDocumentOrder();
+  
+  
+  /**
    * Get the node with the smallest offset
    */
   public Node firstNode();

Modified: gate/trunk/src/gate/annotation/AnnotationSetImpl.java
===================================================================
--- gate/trunk/src/gate/annotation/AnnotationSetImpl.java       2013-06-19 
16:26:05 UTC (rev 16711)
+++ gate/trunk/src/gate/annotation/AnnotationSetImpl.java       2013-06-28 
15:10:47 UTC (rev 16712)
@@ -420,8 +420,49 @@
     return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
   }
 
+  
   /**
    * Select annotations by offset. This returns the set of annotations that
+   * start exactly at the given offset. If a
+   * positional index doesn't exist it is created. If there are no annotations
+   * at the given offset then an empty annotation set is returned.
+   * 
+   * @param offset The starting offset for which to return annotations 
+   * @return a ImmutableAnnotationSetImpl containing all annotations starting 
at the given
+   *   offset (possibly empty).
+   */
+  public AnnotationSet getStartingAt(long offset) {
+    if(annotsByStartNode == null) indexByStartOffset();
+    Node node = (Node)nodesByOffset.get(offset);
+    if(node == null) { // no nodes at or beyond this offset
+      return emptyAnnotationSet;
+    }
+    return new ImmutableAnnotationSetImpl(doc, 
getAnnotsByStartNode(node.getId()));
+  }
+  
+  /**
+   * Return a list of annotations sorted by increasing start offset, i.e. in 
the order
+   * they appear in the document. If more than one annotation starts at a 
specific offset
+   * the order of these annotations is unspecified.
+   * 
+   * @return a list of annotations ordered by increasing start offset. If a 
positional
+   * index does not exist, it is created.
+   */
+  public List<Annotation> inDocumentOrder() {
+    if(annotsByStartNode == null) indexByStartOffset();
+    Collection<Object> values = nodesByOffset.values();
+    List<Annotation> result = new ArrayList<Annotation>();
+    for(Object nodeObj : values) {
+      Collection<Annotation> anns = 
getAnnotsByStartNode(((Node)nodeObj).getId());
+      if(anns != null) {
+        result.addAll(anns);
+      }
+    }
+    return result;
+  }
+  
+  /**
+   * Select annotations by offset. This returns the set of annotations that
    * overlap totaly or partially with the interval defined by the two provided
    * offsets.The result will include all the annotations that either:
    * <ul>

Modified: gate/trunk/src/gate/annotation/TestAnnotation.java
===================================================================
--- gate/trunk/src/gate/annotation/TestAnnotation.java  2013-06-19 16:26:05 UTC 
(rev 16711)
+++ gate/trunk/src/gate/annotation/TestAnnotation.java  2013-06-28 15:10:47 UTC 
(rev 16712)
@@ -1154,6 +1154,41 @@
       return new AnnotationImpl(id, start, end, type, features);
   }
 
+  /** Test inDocumentOrder() and getStartingAt(long) */
+  public void testDocumentOrder() throws Exception {
+    FeatureMap params = Factory.newFeatureMap();
+    params.put(Document.DOCUMENT_URL_PARAMETER_NAME, 
Gate.getUrl("tests/doc0.html"));
+    params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "true");
+    Document doc = 
(Document)Factory.createResource("gate.corpora.DocumentImpl",
+                                                    params);
+
+    AnnotationSet originals = doc.getAnnotations("Original markups");
+    if(originals instanceof AnnotationSetImpl) {
+      AnnotationSetImpl origimpl = (AnnotationSetImpl)originals;
+      List<Annotation> ordered = origimpl.inDocumentOrder();
+      assertNotNull(ordered);
+      assertEquals(20, ordered.size());
+      assertEquals(33, ordered.get(4).getStartNode().getOffset().intValue());
+      for(int i=1;i<ordered.size();i++) {
+        assertTrue("Elements "+(i-1)+"/"+i,
+            ordered.get(i-1).getStartNode().getOffset() <= 
ordered.get(i).getStartNode().getOffset());
+      }
+      AnnotationSet anns;
+      anns = origimpl.getStartingAt(0);
+      assertEquals(4,anns.size());
+      anns = origimpl.getStartingAt(1);
+      assertEquals(0,anns.size());
+      anns = origimpl.getStartingAt(33);
+      assertEquals(4,anns.size());
+      anns = origimpl.getStartingAt(48);
+      assertEquals(1,anns.size());
+      anns = origimpl.getStartingAt(251);
+      assertEquals(1,anns.size());
+    }
+  } // testDocumentOrder()
+  
+  
+  
   public static void main(String[] args){
 
     try{

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to