这是一个愚蠢的问题,但我怎样才能以树格式打印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)))))
(. .)))
答案 0 :(得分:0)
以下语句将以打印格式打印树。
tree.pennPrint();