Search code examples
javaannotationsgatejava-annotations

Jape Rule to annotate sentence and show the numbers in the sentence as feature


I need to write a jape rule to annotate sentence and display the numbers in the sentence as features and tried the following rule

Imports: {import static gate.Utils.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; }
Phase:Sentence
Input: Sentence Token
Options: control = appelt
Rule: SentenceNum
({Sentence}):temp
-->
{try{
    AnnotationSet val = bindings.get("temp");
    String strSent = doc.getContent().getContent(val.firstNode().getOffset(), val.lastNode().getOffset()).toString();
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(strSent);
    while(m.find()){
        FeatureMap features = Factory.newFeatureMap();
        String num = m.group();
        features.put("number",num);
        features.put("rule","Sentence");
        outputAS.add(val.firstNode(),val.lastNode(),"Sentence",features);
    }
}
catch(Exception e)
{
    e.printStackTrace();
}
}

The sentence is not getting annotated fully its getting annotated only where numbers are present and also gets annotated as many times as the numbers. Can someone help me with this.


Solution

  • Got the answer guys

    Imports: {import gate.Utils;}
    
    Phase:Sentence
    Input: Token
    Options: control = appelt
    Rule: Number
    (
        {Token.category ==~ CD}
    ):num
    -->
    :num.Number = {Number = :num.Token.string}
    
    Phase:Sentence
    Input: Sentence
    Options: control = appelt
    Rule: GetNumber
    ({Sentence}):tag
    -->
    {
      Annotation numSent = Utils.getOnlyAnn(bindings.get("tag"));
      List<Annotation> val = Utils.inDocumentOrder(Utils.getContainedAnnotations(inputAS, numSent, "Number"));
      List<String> str = new ArrayList<String>(val.size());
      for(Annotation a : val) {
        str.add((String)a.getFeatures().get("Number"));
      }
      numSent.getFeatures().put("Number", str);
    }
    

    this jape rule will add the number annotation to the existing Sentence annotation This is how the output looks