【问题标题】:Get token string from tokenID using Stanford Parser in GATE在 GATE 中使用 Stanford Parser 从 tokenID 获取令牌字符串
【发布时间】:2018-06-05 07:51:08
【问题描述】:

我正在尝试使用一些 Java RHS 在 GATE 中使用斯坦福依赖解析器获取依赖标记的字符串值,并将它们添加为新注释的功能。

我在仅针对令牌的“依赖项”功能以及从令牌ID 获取字符串值时遇到问题。

使用下面仅指定'depdencies'也会引发java空指针错误:

for(Annotation lookupAnn : tokens.inDocumentOrder())
  {
   FeatureMap lookupFeatures  = lookupAnn.getFeatures();
   token = lookupFeatures.get("dependencies").toString();  
  }

我可以使用下面的来获取令牌的所有功能,

gate.Utils.inDocumentOrder

但它会返回所有特征,包括依赖的 tokenID;即:

dependencies = [nsubj(8390), dobj(8394)]

我想从这些 tokenID 中获取依赖令牌的字符串值。

有什么方法可以访问依赖的令牌字符串值并将它们作为特征添加到注释中?

非常感谢您的帮助

【问题讨论】:

    标签: java stanford-nlp gate


    【解决方案1】:

    这是一个有效的 JAPE 示例。它只打印到 GATE 的消息窗口(标准输出),它不会创建任何具有您要求的功能的新注释。请自己完成...

    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 窗口(标准输出)产生任何消息。当我简化并尝试以下操作时,我得到一个空指针异常: for (Annotation token : tokens.inDocumentOrder()) { FeatureMap lookupFeatures = token.getFeatures(); deps = token.getFeatures().get("依赖").toString();我已经加载了 corenlp 插件和以下导入......有什么建议吗?在此先感谢您的帮助。进口:{进口静态gate.Utils.*;进口门.stanford.DependencyRelation; }
    • 我可以在聊天中 ping 你吗?
    • @FJ1993 聊天没问题,但我不确定我们是否能找到一个共同的时间段……我的 jape 文件必须在斯坦福解析器处理的一些非空 GATE 文档上执行。我假设你有一个......或者它已经解决了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多