微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-斯坦福大学nlp:解析树

我有这样一句话:我的狗也喜欢吃香肠.

我得到以下解析树:

(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();                                                    
}      

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐