Search code examples
javanlpstanford-nlp

How to print the parse tree of Stanford JavaNLP


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.


Solution

  • 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.