解析树中节点的深度

吾吾

我在Java 7和NetBeans 7.3.1中使用stanford-nlp

Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    String text = "the dog who bit the man";// Add your text here!
    Annotation document = new Annotation(text);
    pipeline.annotate(document);
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    for(CoreMap sentence: sentences) {
      for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
        String word = token.get(TextAnnotation.class);
        String pos = token.get(PartOfSpeechAnnotation.class);
        String ne = token.get(NamedEntityTagAnnotation.class);       
      }
    Tree tree = sentence.get(TreeAnnotation.class);
    System.out.println(tree);
    System.out.println(tree.depth());

有了这个,我可以得到树的深度,但是在解析树中,如何获得术语“狗”的深度或任何其他术语的深度呢?

吾吾

经过研究,我知道这是一个愚蠢的问题(非常愚蠢)对不起:D

无论如何,这是我学到的东西:

由于句子的术语/字符串在分析树中表示为叶子,因此它们的深度将为0。

所以现在如何访问该术语,即如何将树迭代到该术语/字符串/叶::

for (Tree subtree : tree) { 
          if(subtree.label().value().equals("term----xxxx"))
          //You can do your own stuff here 
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章