问题
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