斯坦福nlp:解析树

问题描述 投票:3回答:1

我有这句话: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();
java nlp stanford-nlp
1个回答
4
投票

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

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();                                                    
}      
© www.soinside.com 2019 - 2024. All rights reserved.