在 GATE 中使用 Stanford Parser 从 tokenID 获取令牌字符串
Get token string from tokenID using Stanford Parser in GATE
我正在尝试使用一些 Java RHS 在 GATE 中使用 Stanford 依赖解析器获取依赖标记的字符串值,并将它们添加为新注释的特征。
我在仅针对令牌的 'dependencies' 功能以及从令牌 ID 获取字符串值时遇到问题。
使用下面仅指定 'depdencies' 也会引发 java 空指针错误:
for(Annotation lookupAnn : tokens.inDocumentOrder())
{
FeatureMap lookupFeatures = lookupAnn.getFeatures();
token = lookupFeatures.get("dependencies").toString();
}
我可以使用下面的代码来获取令牌的所有功能,
gate.Utils.inDocumentOrder
但它 returns 所有功能,包括相关的 tokenID;即:
dependencies = [nsubj(8390), dobj(8394)]
我只想从这些令牌 ID 中获取相关令牌的字符串值。
有什么方法可以访问依赖标记字符串值并将它们作为特征添加到注释中吗?
非常感谢您的帮助
这是一个有效的 JAPE 示例。它只打印 GATE 的消息 window(标准输出),它不会创建任何具有您要求的功能的新注释。请自己完成...
Stanford_CoreNLP
必须在 GATE 中加载插件才能加载此 JAPE 文件。否则你会得到 class not found exception for DependencyRelation
class。
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));
}
}
}
我正在尝试使用一些 Java RHS 在 GATE 中使用 Stanford 依赖解析器获取依赖标记的字符串值,并将它们添加为新注释的特征。
我在仅针对令牌的 'dependencies' 功能以及从令牌 ID 获取字符串值时遇到问题。
使用下面仅指定 'depdencies' 也会引发 java 空指针错误:
for(Annotation lookupAnn : tokens.inDocumentOrder())
{
FeatureMap lookupFeatures = lookupAnn.getFeatures();
token = lookupFeatures.get("dependencies").toString();
}
我可以使用下面的代码来获取令牌的所有功能,
gate.Utils.inDocumentOrder
但它 returns 所有功能,包括相关的 tokenID;即:
dependencies = [nsubj(8390), dobj(8394)]
我只想从这些令牌 ID 中获取相关令牌的字符串值。
有什么方法可以访问依赖标记字符串值并将它们作为特征添加到注释中吗?
非常感谢您的帮助
这是一个有效的 JAPE 示例。它只打印 GATE 的消息 window(标准输出),它不会创建任何具有您要求的功能的新注释。请自己完成...
Stanford_CoreNLP
必须在 GATE 中加载插件才能加载此 JAPE 文件。否则你会得到 class not found exception for DependencyRelation
class。
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));
}
}
}