Revision: 14445
          http://gate.svn.sourceforge.net/gate/?rev=14445&view=rev
Author:   valyt
Date:     2011-10-27 12:26:57 +0000 (Thu, 27 Oct 2011)
Log Message:
-----------
The mimir-core side of supporting the indexing of document metadata.

Modified Paths:
--------------
    mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java
    mimir/trunk/mimir-core/src/gate/mimir/SemanticAnnotationHelper.java
    mimir/trunk/mimir-core/src/gate/mimir/index/mg4j/MentionsIndexBuilder.java
    
mimir/trunk/mimir-core/src/gate/mimir/util/DelegatingSemanticAnnotationHelper.java

Modified: 
mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java 
2011-10-27 12:26:19 UTC (rev 14444)
+++ mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java 
2011-10-27 12:26:57 UTC (rev 14445)
@@ -174,6 +174,8 @@
    */
   protected String annotationType;
        
+  private boolean isDocumentHelper = false;
+  
   public String getAnnotationType() {
     return annotationType;
   }
@@ -246,4 +248,20 @@
     }
     return concat;
   }
+
+  @Override
+  public boolean isInDocumentMode() {
+    return isDocumentHelper;
+  }
+  
+  /**
+   * Switches this helper in document helper mode. 
+   * @return a reference to this helper, after it has been configured to work
+   * as a document helper. 
+   */
+  public SemanticAnnotationHelper asDocumentHelper() {
+    isDocumentHelper = true;
+    return this;
+  }
+  
 }

Modified: mimir/trunk/mimir-core/src/gate/mimir/SemanticAnnotationHelper.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/SemanticAnnotationHelper.java 
2011-10-27 12:26:19 UTC (rev 14444)
+++ mimir/trunk/mimir-core/src/gate/mimir/SemanticAnnotationHelper.java 
2011-10-27 12:26:57 UTC (rev 14445)
@@ -30,7 +30,24 @@
 import java.util.Map;
 
 /**
- * Interface for classes that convert annotations into semantic metadata.
+ * Interface for classes that convert annotations into semantic metadata. At 
+ * indexing time, for each annotation to be indexed, the helper produces a set 
+ * of mentions URIs (obtained by calling 
+ * {@link #getMentionUris(Annotation, int, Indexer)}).
+ * At search time, given a set of constraints, the helper produces set of 
+ * <mention URIs, mention length> pairs (obtained by calling on of the 
+ * {@link #getMentions(String, List, QueryEngine)}, 
+ * {@link #getMentions(String, Map, QueryEngine)} methods).
+ * 
+ * Each helper can function in annotation mode (the default) or in document 
+ * mode. The current functioning mode can be obtained by calling 
+ * {@link #isInDocumentMode()}. When in document mode, the helper should behave
+ * as if a single annotation covering the whole document span is being indexed 
+ * (or searched for). In this mode, document features are used instead of 
+ * annotation features. This has efficiency advantages over actually creating 
a 
+ * document-spanning annotation, as the implementations can avoid actually 
+ * storing the annotation length in the index (as it is always the same as the 
+ * document length).     
  */
 public interface SemanticAnnotationHelper extends Serializable{
   
@@ -110,5 +127,16 @@
    * operations (such as closing connections to ORDI, etc) on this call.
    */
   public void close(QueryEngine qEngine);
+
+  /**
+   * Checks whether this helper is configured to work in document helper mode. 
+   * If that's the case, then the indexer will call its 
+   * {@link #getMentionUris(Annotation, int, Indexer)} method only once for 
+   * each input document, with a <code>null</code> value for the annotation.
+   * 
+   * @return Returns <code>true</code> if this helper has been configured to 
+   * work in document helper mode.
+   */
+  public boolean isInDocumentMode();
   
 }

Modified: 
mimir/trunk/mimir-core/src/gate/mimir/index/mg4j/MentionsIndexBuilder.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/index/mg4j/MentionsIndexBuilder.java  
2011-10-27 12:26:19 UTC (rev 14444)
+++ mimir/trunk/mimir-core/src/gate/mimir/index/mg4j/MentionsIndexBuilder.java  
2011-10-27 12:26:57 UTC (rev 14445)
@@ -24,11 +24,14 @@
 import gate.Document;
 import gate.mimir.SemanticAnnotationHelper;
 import gate.mimir.IndexConfig.SemanticIndexerConfig;
+import gate.mimir.index.IndexException;
 import gate.mimir.index.Indexer;
 import gate.util.OffsetComparator;
 
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.BlockingQueue;
 
@@ -47,8 +50,10 @@
   /**
    * Helpers for each semantic annotation type.
    */
-  protected Map<String, SemanticAnnotationHelper> helpers;
+  protected Map<String, SemanticAnnotationHelper> annotationHelpers;
   
+  protected List<SemanticAnnotationHelper> documentHelpers;
+  
   /**
    * An {@link OffsetComparator} used to sort the annotations by offset before 
    * indexing.
@@ -60,10 +65,17 @@
           Indexer indexer, String baseName, SemanticIndexerConfig config){
     super(inputQueue, outputQueue, indexer, baseName);
     //get the helpers
-    helpers = new HashMap<String, SemanticAnnotationHelper>(
+    annotationHelpers = new HashMap<String, SemanticAnnotationHelper>(
               config.getAnnotationTypes().length);
+    documentHelpers = new LinkedList<SemanticAnnotationHelper>();
     for(int i = 0; i <  config.getAnnotationTypes().length; i++){
-      helpers.put(config.getAnnotationTypes()[i], config.getHelpers()[i]);
+      SemanticAnnotationHelper theHelper = config.getHelpers()[i];
+      if(theHelper.isInDocumentMode()) {
+        documentHelpers.add(theHelper);
+      } else {
+        annotationHelpers.put(config.getAnnotationTypes()[i], theHelper);  
+      }
+      
     }
     offsetComparator = new OffsetComparator();
   }
@@ -73,18 +85,31 @@
    * Inform the helpers that a new document is about to start.
    */
   protected void documentStarting(GATEDocument gateDocument) {
-    for(SemanticAnnotationHelper aHelper : helpers.values()){
+    for(SemanticAnnotationHelper aHelper : annotationHelpers.values()){
       aHelper.documentStart(gateDocument.getDocument());
     }
+    for(SemanticAnnotationHelper aHelper : documentHelpers){
+      aHelper.documentStart(gateDocument.getDocument());
+    }    
   }
 
   /**
    * Inform the helpers that we finished the current document.
+   * @throws IndexException 
    */
-  protected void documentEnding(GATEDocument gateDocument) {
-    for(SemanticAnnotationHelper aHelper : helpers.values()){
+  protected void documentEnding(GATEDocument gateDocument) throws 
IndexException {
+    for(SemanticAnnotationHelper aHelper : annotationHelpers.values()){
       aHelper.documentEnd();
     }
+    
+    if(!documentHelpers.isEmpty()) {
+      processAnnotation(null, gateDocument);
+    }
+    
+    for(SemanticAnnotationHelper aHelper : documentHelpers){     
+      aHelper.documentEnd();
+    }
+    
   }
   
   /**
@@ -103,7 +128,7 @@
     if(semAnnSet.size() > 0){
       AnnotationSet semAnns = null;
       synchronized(semAnnSet) {
-        semAnns = semAnnSet.get(helpers.keySet());
+        semAnns = semAnnSet.get(annotationHelpers.keySet());
       }
       semanticAnnots = semAnns.toArray(new Annotation[semAnns.size()]);
       Arrays.sort(semanticAnnots, offsetComparator);
@@ -126,24 +151,29 @@
    */
   protected void calculateStartPositionForAnnotation(Annotation ann,
           GATEDocument gateDocument) {
-    //calculate the term position for the current semantic annotation
-    while(tokenPosition <  gateDocument.getTokenAnnots().length &&
-          gateDocument.getTokenAnnots()[tokenPosition].
-            getEndNode().getOffset().longValue() <= 
-            ann.getStartNode().getOffset().longValue()){
-      tokenPosition++;
+    if(ann == null) {
+      // we're supposed index the document metadata
+      tokenPosition = 0;
+    } else {
+      //calculate the term position for the current semantic annotation
+      while(tokenPosition <  gateDocument.getTokenAnnots().length &&
+            gateDocument.getTokenAnnots()[tokenPosition].
+              getEndNode().getOffset().longValue() <= 
+              ann.getStartNode().getOffset().longValue()){
+        tokenPosition++;
+      }
+      //check if lastTokenposition is valid
+      if(tokenPosition >= gateDocument.getTokenAnnots().length){
+        //malfunction
+        logger.error(
+                "Semantic annotation [Type:" + ann.getType() +
+                ", start: " + ann.getStartNode().getOffset().toString() +
+                ", end: " + ann.getEndNode().getOffset().toString() +
+                "] outside of the tokens area in document" +
+                " URI: " + gateDocument.uri() +
+                " Title: " + gateDocument.title());
+      }      
     }
-    //check if lastTokenposition is valid
-    if(tokenPosition >= gateDocument.getTokenAnnots().length){
-      //malfunction
-      logger.error(
-              "Semantic annotation [Type:" + ann.getType() +
-              ", start: " + ann.getStartNode().getOffset().toString() +
-              ", end: " + ann.getEndNode().getOffset().toString() +
-              "] outside of the tokens area in document" +
-              " URI: " + gateDocument.uri() +
-              " Title: " + gateDocument.title());
-    }
   }
 
   /**
@@ -155,17 +185,30 @@
    */
   protected String[] calculateTermStringForAnnotation(Annotation ann,
           GATEDocument gateDocument) {
-    //calculate the annotation length (as number of terms)
-    SemanticAnnotationHelper helper = helpers.get(ann.getType());
-    int length = 1;
-    while(tokenPosition + length <  gateDocument.getTokenAnnots().length &&
-            gateDocument.getTokenAnnots()[tokenPosition + length].
-              getStartNode().getOffset().longValue() < 
-              ann.getEndNode().getOffset().longValue()){
-        length++;
+    if(ann == null) {
+      // obtain the URIs to be indexed for the *document* metadata
+      List<String> terms = new LinkedList<String>();
+      for(SemanticAnnotationHelper aHelper : documentHelpers) {
+        String[] someTerms = aHelper.getMentionUris(null, -1, indexer);
+        if(someTerms != null) {
+          for(String aTerm : someTerms) {
+            terms.add(aTerm);
+          }
+        }
       }
-    //get the annotation URI
-    return helper.getMentionUris(ann, length, indexer);
-//    currentTerm.replace(helper.getMentionUri(ann, length, indexer));
+      return terms.toArray(new String[terms.size()]);
+    } else {
+      //calculate the annotation length (as number of terms)
+      SemanticAnnotationHelper helper = annotationHelpers.get(ann.getType());
+      int length = 1;
+      while(tokenPosition + length <  gateDocument.getTokenAnnots().length &&
+              gateDocument.getTokenAnnots()[tokenPosition + length].
+                getStartNode().getOffset().longValue() < 
+                ann.getEndNode().getOffset().longValue()){
+          length++;
+        }
+      //get the annotation URI
+      return helper.getMentionUris(ann, length, indexer);
+    }
   }
 }

Modified: 
mimir/trunk/mimir-core/src/gate/mimir/util/DelegatingSemanticAnnotationHelper.java
===================================================================
--- 
mimir/trunk/mimir-core/src/gate/mimir/util/DelegatingSemanticAnnotationHelper.java
  2011-10-27 12:26:19 UTC (rev 14444)
+++ 
mimir/trunk/mimir-core/src/gate/mimir/util/DelegatingSemanticAnnotationHelper.java
  2011-10-27 12:26:57 UTC (rev 14445)
@@ -110,7 +110,22 @@
     delegate.documentEnd();
   }
 
+  
   @Override
+  public boolean isInDocumentMode() {
+    return delegate.isInDocumentMode();
+  }
+
+
+  @Override
+  public SemanticAnnotationHelper asDocumentHelper() {
+    if(delegate.isInDocumentMode()) return this;
+    else throw new UnsupportedOperationException(
+      "The delegate provided is not configured to work as a document helper.");
+  }
+
+
+  @Override
   public void close(Indexer indexer) {
     delegate.close(indexer);
   }

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


------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn 
about Cisco certifications, training, and career opportunities. 
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to