Copy versions

Find an annotation of one type and copy modified versions of its features to all contained annotations of a specified type.

Rule: ArticleMention

(
  ({Article}):article
)
-->
{
   /* Get the Article annotations and its span (to use for start and end points) */
   AnnotationSet span    = (gate.AnnotationSet) bindings.get("article");
   Annotation  article   = span.iterator().next();

   /* Get all the contained Mention annotations */
   AnnotationSet mentions = inputAS.getContained(span.firstNode().getOffset(), 
                                               span.lastNode().getOffset())
                                   .get("Mention");
   Iterator<Annotation>  mentionIter = mentions.iterator();

   FeatureMap articleFeatures  = article.getFeatures();
   Set        articleFKeys     = articleFeatures.keySet();
   Annotation mention;
   FeatureMap mentionFeatures, additionalFeatures;
   Iterator   keyIter;
   Object     key, value;
   String     mKey;

   /* Produce a modified FeatureMap from that of the Article */
   additionalFeatures  = gate.Factory.newFeatureMap();
   keyIter             = articleFKeys.iterator();
   while(keyIter.hasNext()) {
       key = keyIter.next();

       /* ignore non-String keys; we don't expect them and wouldn't know
          what to do with them */
       if (key instanceof String) {
          value = articleFeatures.get(key);
          mKey     = "article_" + ((String) key);
          additionalFeatures.put(mKey, value);
       }
    }

   /* Iterate through the Mentions and copy modified versions of the Article's features
      into each Mention's feature map */
   while (mentionIter.hasNext()) {
      mention          = mentionIter.next();
      mentionFeatures  = mention.getFeatures();
      mentionFeatures.putAll(additionalFeatures);
   }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License