GATE:JAPE规则Java RHS特征映射

时间:2018-05-09 19:42:55

标签: java nlp grammar gate

我试图在Sentence注释中获得现有注释及其特征,即对于每个句子,可能有多个注释具有mainType,字符串和类型特征。

我想要一个新的'Sentence contains'注释,其中包含所包含注释的特征图及其各自的特征。

我认为它应该是优秀的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); }

除了我的新规则,我想注释句子,然后得到包含的注释:

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); }  

如何获取所包含注释的要素图?

非常感谢

2 个答案:

答案 0 :(得分:2)

您可以使用foreach获取句子中包含的所有注释,并根据其majorType或种类存储在要素图中。

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); 
}  

答案 1 :(得分:0)

创建一个新的注释只是为了找出“团队”并编写另一个规则,将团队的注释复制到 RHS 中的默认 Sentence 注释

我有一个类似的 jape 规则,我需要对句子进行注释并在句子中添加数字作为其特征。

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);
}

此代码将使用默认的 Sentence 注释对句子进行注释,但在 Sentence 注释的特征中显示 Number 注释的特征。

这是输出图像:

相关问题