javanlpgate

Add list of all contained annotations as features of new annotation in GATE


I am trying to add a list of all contained "all_tags" annotations as a feature of a new annotation using Java RHS rule.

Below only adds one annotation rather than all of them in a list:

AnnotationSet contTagAS = getContainedAnnotations(inputAS,spanAs).get("all_tags");

for (Annotation tagAnn : contTagAS.inDocumentOrder())
{
  FeatureMap lookupFeatures  = tagAnn.getFeatures();
  tag = lookupFeatures.get("type").toString();  
}

I want each all_tags "type" to be added as features separated by commmas, i.e "type 1, type 2, type 3"

I have tried List Annotation classes but cannot find the right method.

Many thanks


Solution

  • AnnotationSet contTagAS = getContainedAnnotations(inputAS,spanAs).get("all_tags");
    
    StringJoiner joiner = new StringJoiner(",");
    
    for (Annotation tagAnn : contTagAS.inDocumentOrder())
    {
      FeatureMap lookupFeatures  = tagAnn.getFeatures();
      String tag = lookupFeatures.get("type").toString();
      joiner.add(tag);
    }
    
    outputAS.add(
        spanAs.firstNode(), 
        spanAs.lastNode(), 
        "new annotation", 
        featureMap("tags", joiner.toString())
    );