Stanford NLP:以树格式打印解析器树

时间:2017-08-08 11:58:18

标签: tree stanford-nlp

这是一个愚蠢的问题,但我怎样才能以树格式打印NLP解析树的结果?

这是我的代码:

public static void main(String[] args) {

        Annotation document =
                new Annotation("My dog also likes eating sausage.");
            Properties props = new Properties();
            props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            pipeline.annotate(document);
            for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
              Tree constituencyParse = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
              System.out.println(constituencyParse);

        }
    }

执行后,我在Eclipse控制台中获得以下结果:

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

有没有办法以实际的树格式打印出来,比如这样的东西?

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

1 个答案:

答案 0 :(得分:0)

以下语句将以打印格式打印树。

tree.pennPrint();