斯坦福大学NLP:以树格式打印解析器树
问题描述:
这是一个愚蠢的问题,但我怎样才能以树形格式打印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)))))
(. .)))
答
以下语句将以打印格式打印树。
tree.pennPrint();