Java 在GATE中使用Stanford解析器从tokenID获取令牌字符串

Java 在GATE中使用Stanford解析器从tokenID获取令牌字符串,java,stanford-nlp,gate,Java,Stanford Nlp,Gate,我正在尝试使用一些JavaRH来使用GATE中的Stanford dependency parser获取依赖令牌的字符串值,并将它们添加为新注释的功能 我在仅针对令牌的“依赖项”特性以及从令牌ID获取字符串值方面遇到问题 使用以下仅指定“depdencies”的选项也会引发java空指针错误: for(Annotation lookupAnn : tokens.inDocumentOrder()) { FeatureMap lookupFeatures = lookupAnn.get

我正在尝试使用一些JavaRH来使用GATE中的Stanford dependency parser获取依赖令牌的字符串值,并将它们添加为新注释的功能

我在仅针对令牌的“依赖项”特性以及从令牌ID获取字符串值方面遇到问题

使用以下仅指定“depdencies”的选项也会引发java空指针错误:

for(Annotation lookupAnn : tokens.inDocumentOrder())
  {
   FeatureMap lookupFeatures  = lookupAnn.getFeatures();
   token = lookupFeatures.get("dependencies").toString();  
  }
我可以使用下面的代码获取令牌的所有功能

gate.Utils.inDocumentOrder
但它返回所有特性,包括依赖的tokenID;i、 e:

dependencies = [nsubj(8390), dobj(8394)]
我只想从这些令牌ID中获取依赖令牌的字符串值

是否有任何方法可以访问依赖令牌字符串值并将其作为功能添加到注释中


非常感谢您的帮助

这里是一个日本工作的例子。它只会打印到GATE的消息窗口(std out),不会创建任何带有您要求的功能的新注释。请自己完成它

Stanford_CoreNLP
插件必须加载到GATE中才能加载此JAPE文件。否则,您将获得
DependencyRelation
类的类未找到异常

Imports: {
  import gate.stanford.DependencyRelation;
}

Phase: GetTokenDepsPhase
Input: Token
Options: control = all
Rule: GetTokenDepsRule
(
  {Token}
): token
--> 
:token {
  //note that tokenAnnots contains only a single annotation so the loop could be avoided...
  for (Annotation token : tokenAnnots) {
    Object deps = token.getFeatures().get("dependencies");

    //sometimes the dependencies feature is missing - skip it
    if (deps == null) continue;

    //token.getFeatures().get("string") could be used instead of gate.Utils.stringFor(doc,token)...
    System.out.println("Dependencies for token " + gate.Utils.stringFor(doc, token));

    //the dependencies feature has to be typed to List<DependencyRelation>
    List<DependencyRelation> typedDeps = (List<DependencyRelation>) deps;
    for (DependencyRelation r : typedDeps) {

      //use DependencyRelation.getTargetId() to get the id of the target token
      //use inputAS.get(id) to get the annotation for its id
      Annotation targetToken = inputAS.get(r.getTargetId());

      //use DependencyRelation.getType() to get the dependency type
      System.out.println("  " +r.getType()+ ": " +gate.Utils.stringFor(doc, targetToken));
    }
  }
}
导入:{
导入gate.stanford.dependency关系;
}
阶段:GetTokenDepsPhase
输入:令牌
选项:控制=全部
规则:GetTokenDepsRule
(
{Token}
):代币
--> 
:代币{
//请注意,tokenAnnots只包含一个注释,因此可以避免循环。。。
用于(注释标记:标记注释){
Object deps=token.getFeatures().get(“依赖项”);
//有时缺少依赖项功能—跳过它
如果(deps==null)继续;
//可以使用token.getFeatures().get(“string”)代替gate.Utils.stringFor(doc,token)。。。
System.out.println(“令牌依赖项”+gate.Utils.stringFor(doc,token));
//必须键入依赖项功能才能列出
列表类型DDEPS=(列表)deps;
for(依赖关系r:typedDeps){
//使用DependencyRelation.getTargetId()获取目标令牌的id
//使用inputAS.get(id)获取其id的注释
注释targetToken=inputAS.get(r.getTargetId());
//使用DependencyRelation.getType()获取依赖项类型
System.out.println(“+r.getType()+”:“+gate.Utils.stringFor(doc,targetToken));
}
}
}

我无法让它工作。。。使用复制/粘贴的完整规则不会向闸门窗口(std out)生成任何消息。当我简化并尝试以下操作时,我得到一个空指针异常:for(注释令牌:tokens.inDocumentOrder()){FeatureMap lookupFeatures=token.getFeatures();deps=token.getFeatures().get(“依赖项”).toString();}我已经加载了corenlp插件和以下导入。。。有什么建议吗?提前感谢你的帮助。导入:{import static gate.Utils.*;import gate.stanford.DependencyRelation;}我可以在聊天中ping你吗?@FJ1993聊天没有问题,但我不确定我们是否找到了一个公共时间段。。。我的jape文件必须在斯坦福解析器处理的非空门文档上执行。我想你有一个。。。还是已经解决了?