Revision: 14572
          http://gate.svn.sourceforge.net/gate/?rev=14572&view=rev
Author:   adamfunk
Date:     2011-11-18 15:20:14 +0000 (Fri, 18 Nov 2011)
Log Message:
-----------
Last (I think) round of changes to FlexibleGazetteer.

Loop for mapping back to original document is a little more efficient;
NodePosition (individual mapping) is now immutable.

Modified Paths:
--------------
    gate/trunk/src/gate/creole/gazetteer/FlexibleGazetteer.java
    gate/trunk/src/gate/creole/gazetteer/NodePosition.java

Modified: gate/trunk/src/gate/creole/gazetteer/FlexibleGazetteer.java
===================================================================
--- gate/trunk/src/gate/creole/gazetteer/FlexibleGazetteer.java 2011-11-18 
12:15:35 UTC (rev 14571)
+++ gate/trunk/src/gate/creole/gazetteer/FlexibleGazetteer.java 2011-11-18 
15:20:14 UTC (rev 14572)
@@ -77,11 +77,9 @@
    * be called before the gazetteer can be used
    */
   public Resource init() throws ResourceInstantiationException {
-    // check for parameters
-    if(gazetteerInst == null)
+    if(gazetteerInst == null)  {
       throw new ResourceInstantiationException("No Gazetteer Provided!");
-
-    this.changedNodes = new ArrayList<NodePosition>();
+    }
     return this;
   }
 
@@ -90,7 +88,7 @@
    * parameters are set. If they are not, an exception will be fired.
    */
   public void execute() throws ExecutionException {
-    changedNodes = new ArrayList<NodePosition>();
+    annotationMappings = new ArrayList<NodePosition>();
     fireProgressChanged(0);
     fireStatusChanged("Checking Document...");
     if(document == null) {
@@ -108,7 +106,7 @@
     long totalDeductedSpaces = 0;
     fireStatusChanged("Replacing contents with the feature value...");
     outer: for (Annotation currentToken : 
Utils.inDocumentOrder(document.getAnnotations(inputAnnotationSetName))) {
-      // check if it is a chinesesplit; if it is, replace no space character 
with a single space
+      // Where ChineseSplits occur, insert a single space
       
if(currentToken.getType().equals(ANNIEConstants.SPACE_TOKEN_ANNOTATION_TYPE)
           && 
((String)(currentToken.getFeatures().get(ANNIEConstants.TOKEN_KIND_FEATURE_NAME))).equals("ChineseSplit"))
 {
 
@@ -119,12 +117,14 @@
         // the endoffset will become newStartOffset + 1
         long newStartOffset = startOffset - totalDeductedSpaces;
         long newEndOffset = newStartOffset + 1;
-        NodePosition newNode = new NodePosition(startOffset, startOffset,
-                newStartOffset, newEndOffset, totalDeductedSpaces);
+        NodePosition mapping = new NodePosition(startOffset, startOffset,
+                newStartOffset, newEndOffset);
 
         // here is the addition of space in the document
         totalDeductedSpaces--;
-        changedNodes.add(newNode);
+        // Should these Splits actually be mappable from the temp document
+        // back to the original one?  (AF)
+        annotationMappings.add(mapping);
         newdocString = newdocString.insert((int)newStartOffset, ' ');
         continue outer;
       } // chineseSplit if
@@ -146,23 +146,20 @@
             long startOffset = 
currentToken.getStartNode().getOffset().longValue();
             long endOffset = currentToken.getEndNode().getOffset().longValue();
             
-            // replacement code start
-            long actualLength = endOffset - startOffset;
             // let us find the difference between the lengths of the
             // actual string and the newTokenValue
+            long actualLength = endOffset - startOffset;
             long lengthDifference = actualLength - newTokenValue.length();
             
-            // replacement code end
-            
             // so lets find out the new startOffset and endOffset
             long newStartOffset = startOffset - totalDeductedSpaces;
             long newEndOffset = newStartOffset + newTokenValue.length();
             totalDeductedSpaces += lengthDifference;
             
             // and make the entry for this
-            NodePosition newNode = new NodePosition(startOffset, endOffset,
-                newStartOffset, newEndOffset, totalDeductedSpaces);
-            changedNodes.add(newNode);
+            NodePosition mapping = new NodePosition(startOffset, endOffset,
+                newStartOffset, newEndOffset);
+            annotationMappings.add(mapping);
             
             // and finally replace the actual string in the document
             // with the new document
@@ -174,6 +171,9 @@
         }
       } // END OF "inner" LOOP
     } // END OF "outer" LOOP
+    
+    // make sure the conversion table is in the right order
+    Collections.sort(annotationMappings, new NodePositionComparator());
 
     fireStatusChanged("New Document to be processed with Gazetteer...");
     try {
@@ -185,7 +185,6 @@
       }
       
       FeatureMap features = Factory.newFeatureMap();
-      // Gate.setHiddenAttribute(features, true);
       tempDoc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
               params, features);
     }
@@ -194,7 +193,6 @@
     }
 
     // lets create the gazetteer based on the provided gazetteer name
-    //FeatureMap params = Factory.newFeatureMap();
     gazetteerInst.setDocument(tempDoc);
     gazetteerInst.setAnnotationSetName(this.outputAnnotationSetName);
 
@@ -217,40 +215,35 @@
 
       long originalStart = 0;
       long originalEnd = tempDoc.getContent().size() - 1L;
-
-      int i = 0;
-      for(; i < changedNodes.size(); i++) {
-        NodePosition np = changedNodes.get(i);
-
-        // Find the last node whose temp start node is less than or equal 
+      boolean foundStart = false;
+      
+      for (NodePosition mapping : annotationMappings)  {
+        // Find the last mapping whose temp start offset is less than or equal 
         // to the temp lookup's start 
-        if(np.getNewStartNode() <= startOffset) {
-          originalStart = np.getOldStartNode();
-        } 
-        else {
-          break;
+        if (! foundStart) {
+          if (mapping.getNewStartOffset() <= startOffset)  {
+            originalStart = mapping.getOriginalStartOffset();
+          }
+          else {
+            foundStart = true;
+          }
         }
-      }
-      
-      for(; i < changedNodes.size(); i++) {
-        NodePosition np = changedNodes.get(i);
-
-        // Find the first node whose temp end node is greater than or equal
-        // to the temp lookup's end
-        if(np.getNewEndNode() >= endOffset) {
-          originalEnd = np.getOldEndNode();
+        
+        // Find the first mapping whose temp end offset is greater than or 
equal
+        // to the temp lookup's end; typically this will be the same as the 
mapping found
+        // for the start offset
+        if (mapping.getNewEndOffset() >= endOffset) {
+          originalEnd = mapping.getOriginalEndOffset();
           break;
         }
       }
-
+      
       try { 
-        original.add(originalStart, originalEnd, 
-            currentLookup.getType(), currentLookup.getFeatures());
+        original.add(originalStart, originalEnd, currentLookup.getType(), 
currentLookup.getFeatures());
       } // This should no longer happen
       catch(InvalidOffsetException ioe) {
         throw new ExecutionException(ioe);
       }
-
     }
 
     // now remove the newDoc
@@ -357,5 +350,5 @@
   private java.util.List<String> inputFeatureNames;
 
   // parameters required within the program
-  private ArrayList<NodePosition> changedNodes;
+  private ArrayList<NodePosition> annotationMappings;
 }

Modified: gate/trunk/src/gate/creole/gazetteer/NodePosition.java
===================================================================
--- gate/trunk/src/gate/creole/gazetteer/NodePosition.java      2011-11-18 
12:15:35 UTC (rev 14571)
+++ gate/trunk/src/gate/creole/gazetteer/NodePosition.java      2011-11-18 
15:20:14 UTC (rev 14572)
@@ -1,7 +1,7 @@
 /*
  * NodePosition.java
  *
- * Copyright (c) 2004, The University of Sheffield.
+ * Copyright (c) 2004--2011, The University of Sheffield.
  *
  * This file is part of GATE (see http://gate.ac.uk/), and is free
  * software, licenced under the GNU Library General Public License,
@@ -11,43 +11,37 @@
  * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
  *
  * Niraj Aswani 02/2002
+ * $Id$
  *
+ * 2011-11-18: AF made this immutable.
  */
 
 package gate.creole.gazetteer;
 
+import java.util.Comparator;
+
 /**
  * <p>Title: NodePosition.java </p>
  * <p>Description: This class is used to store the information about the
  * changes in the text and the addition or the subtraction of the spaces.
  * It is used by FlexibleGazetteer. </p>
  * @author Niraj Aswani
- * @version 1.0
  */
 
 public class NodePosition {
 
   /** The original start offset before changes */
-  private long oldStartNode;
+  private long originalStartOffset;
 
   /** The original end offset before changes */
-  private long oldEndNode;
+  private long originalEndOffset;
 
   /** The new start offset after the changes */
-  private long newStartNode;
+  private long newStartOffset;
 
   /** The new end offset after the changes */
-  private long newEndNode;
+  private long newEndOffset;
 
-  /** total deducted spaces due to change in the text before the start
-   * offset in the document
-   */
-  private long deductedSpaces;
-
-  /** Constructor */
-  public NodePosition() {
-  }
-
   /**
    * constructor
    * @param osn - old start offset
@@ -57,91 +51,70 @@
    * @param space - total deducted spaces due to change in the text before
    * the start offset in the document
    */
-  public NodePosition(long osn, long oen, long nsn, long nen, long space) {
-    oldStartNode = osn;
-    oldEndNode = oen;
-    newStartNode = nsn;
-    newEndNode = nen;
-    deductedSpaces = space;
+  public NodePosition(long osn, long oen, long nsn, long nen) {
+    originalStartOffset = osn;
+    originalEndOffset = oen;
+    newStartOffset = nsn;
+    newEndOffset = nen;
   }
 
   /**
    * Returns the old start offset
    * @return a <tt>long</tt> value.
    */
-  public long getOldStartNode() {
-    return oldStartNode;
+  public long getOriginalStartOffset() {
+    return originalStartOffset;
   }
 
   /**
    * Returns the old end offset
    * @return a <tt>long</tt> value.
    */
-  public long getOldEndNode() {
-    return oldEndNode;
+  public long getOriginalEndOffset() {
+    return originalEndOffset;
   }
 
   /**
    * Returns new start offset
    * @return  a <tt>long</tt> value.
    */
-  public long getNewStartNode() {
-    return newStartNode;
+  public long getNewStartOffset() {
+    return newStartOffset;
   }
 
   /**
    * Returns the new end offset
    * @return a <tt>long</tt> value.
    */
-  public long getNewEndNode() {
-    return newEndNode;
+  public long getNewEndOffset() {
+    return newEndOffset;
   }
 
-  /**
-   * Sets the old start offset
-   * @param node
-   */
-  public void setOldStartNode(long node) {
-    oldStartNode = node;
-  }
+}
 
-  /**
-   * Sets the old end offset
-   * @param node
-   */
-  public void setOldEndNode(long node) {
-    oldEndNode = node;
-  }
 
-  /**
-   * sets the new start offset
-   * @param node
-   */
-  public void setNewStartNode(long node) {
-    newStartNode = node;
-  }
+class NodePositionComparator implements Comparator<NodePosition> {
 
-  /**
-   * Sets the new end offset
-   * @param node
-   */
-  public void setNewEndNode(long node) {
-    newEndNode = node;
+  public int compare(NodePosition arg0, NodePosition arg1) {
+    long diff = arg0.getNewStartOffset() - arg1.getNewStartOffset();
+    if (diff != 0L) {
+      return (int) Long.signum(diff);
+    }
+    // implied else
+    diff = arg0.getNewEndOffset() - arg1.getNewEndOffset();
+    if (diff != 0L) {
+      return (int) Long.signum(diff);
+    }
+    // implied else
+    diff = arg0.getOriginalStartOffset() - arg1.getOriginalStartOffset();
+    if (diff != 0L) {
+      return (int) Long.signum(diff);
+    }
+    // implied else
+    diff = arg0.getOriginalEndOffset() - arg1.getOriginalEndOffset();
+    return (int) Long.signum(diff);
   }
-
-  /**
-   * Sets the deducted spaces
-   * @param space
-   */
-  public void setDeductedSpaces(long space) {
-    deductedSpaces = space;
-  }
-
-  /**
-   * Returns the total deducted spaces
-   * @return a <tt>long</tt> value.
-   */
-  public long getDeductedSpaces() {
-    return deductedSpaces;
-  }
-}
\ No newline at end of file
+  
+  
+  
+}

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


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to