javanlpgrammargate

GATE: JAPE rule Java RHS feature map


I am trying to get existing annotations and their features within a Sentence annotation i.e for each sentence, there may be multiple annotations which have a i.e majorType, string and type features.

I want a new ‘Sentence contains’ annotation with a feature map of the contained annotations and their respective features.

I believe it should be an extension of the below rule from the excellent Gate Jape Grammar Tutorial pdf :

Phase:usingJAVAinRHS  
Input:  Lookup  
Options: control = all  
Rule: javainRHS1  
(  
{Lookup.majorType == Team}  
)  
:team  
-->  
{  
gate.AnnotationSet team = (gate.AnnotationSet)bindings.get("team");       
gate.Annotation teamAnn = (gate.Annotation)team.iterator().next();   
gate.FeatureMap features = Factory.newFeatureMap(); 
features.put("teamOfSport", teamAnn.getFeatures().get("minorType"));  
features.put("rule","javainRHS1");  
outputAS.add(team.firstNode(), team.lastNode(), "Team",features); }

Except in my new rule, I want to annotate the Sentence, then get the contained annotation:

Phase:usingJAVAinRHS  
Input:  Lookup Sentence  
Options: control = all  
Rule: javainRHS1  
(  
{Sentence contains {Lookup.majorType == Team}}  
)  
:team  
-->  
{  
gate.AnnotationSet team = (gate.AnnotationSet)bindings.get("team");   
gate.Annotation teamAnn = (gate.Annotation)team.iterator().next();   
gate.FeatureMap features = Factory.newFeatureMap(); 
features.put("teamOfSport",   teamAnn.getFeatures().get("minorType"));  
features.put("rule","javainRHS1");  
outputAS.add(team.firstNode(), team.lastNode(), "Team",features); }  

How do you get the feature map of the contained annotations?

Many thanks


Solution

  • You can use foreach to get all annotation contained in a sentence and store in feature map based on their majorType or kind.

    Imports: {
    import static gate.Utils.*;
    }
    Phase:usingJAVAinRHS  
    Input:  Lookup Sentence  
    Options: control = appelt
    Rule: javainRHS1  
    (  
    {Sentence contains {Lookup.majorType == Team}}  
    )  
    :team  
    -->  
    {  
        gate.AnnotationSet team = (gate.AnnotationSet)bindings.get("team"); 
        gate.FeatureMap features = Factory.newFeatureMap(); 
        for(Annotation annotation:team.inDocumentOrder())  
        {
            if(annotation.getType() == "Lookup"){
                features.put(annotation.getFeatures().get("majorType"),stringFor(doc,annotation));
            }
            else{
                features.put(annotation.getType(), stringFor(doc,annotation));
            }
        }
        features.put("rule","javainRHS1");  
        outputAS.add(team.firstNode(), team.lastNode(), "Team",features); 
    }