我有这句话:My dog also likes eating sausage.
我得到以下解析树:

(ROOT
 (S
   (NP (PRP$ My) (NN dog))
   (ADVP (RB also))
   (VP (VBZ likes)
     (S
       (VP (VBG eating)
        (NP (NN sausage)))))
(. .)))

我如何只获得语法类别,即:NP、ADVP、VP 等?

我尝试使用此代码:
  Tree t=sentence.get(TreeAnnotation.class);
  t.labels();

最佳答案

从句子注释中,您可以获得各种类型的依赖词集合。这可能是您正在寻找的“下一级”。

Tree tree = sentenceAnnotation.get(TreeAnnotation.class);
// print the tree if needed
SemanticGraph basic = sentenceAnnotation.get(BasicDependenciesAnnotation.class);
Collection<TypedDependency> deps = basic.typedDependencies();
for (TypedDependency typedDep : deps) {
    GrammaticalRelation reln = typedDep.reln();
    String type = reln.toString();
}

SemanticGraph colapsed = sentenceAnnotation
        .get(CollapsedDependenciesAnnotation.class);
Collection<TypedDependency> deps = colapsed.typedDependencies();
for (TypedDependency typedDep : deps) {
    GrammaticalRelation reln = typedDep.reln();
    String type = reln.toString();
}

SemanticGraph ccProcessed = sentenceAnnotation
        .get(CollapsedCCProcessedDependenciesAnnotation.class);
Collection<TypedDependency> deps = ccProcessed.typedDependencies();
for (TypedDependency typedDep : deps) {
    GrammaticalRelation reln = typedDep.reln();
    String type = reln.toString();
}

关于java - 斯坦福大学 : Parse Tree,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22410265/

10-12 19:14