How to print the parse tree of Stanford JavaNLP

谁说我不能喝 提交于 2019-12-22 01:44:41

问题


I am trying to get all the noun phrases using the edu.stanford.nlp.* package. I got all the subtrees of label value "NP", but I am not able to get the normal original String format (not Penn Tree format).

E.g. for the subtree.toString() gives (NP (ND all)(NSS times))) but I want the string "all times". Can anyone please help me. Thanks in advance.


回答1:


I believe what you want is something like:

final StringBuilder sb = new StringBuilder();

for ( final Tree t : tree.getLeaves() ) {
     sb.append(t.toString()).append(" ");
}

While I'm not 100% sure, I seem to recall this being the solution used for some software I worked on a few years back.




回答2:


This can be accomplished using the yield() method for the subtree, instead of creating a separate StringBuilder objext.

if (subtree.label().value().equals("NP")) {
    out.println(subtree);       //print subtree
    out.println(Sentence.listToString(subtree.yield()));    //print phrase  
    break;
}


来源:https://stackoverflow.com/questions/11148890/how-to-print-the-parse-tree-of-stanford-javanlp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!