Search code examples
javaparsingstanford-nlp

Stanford parser: how to print also parsing tree and universal dependencies?


I want to print the parsing tree and the Universal dependencies from a given text line as shown here in their demo at http://nlp.stanford.edu:8080/parser/index.jsp

This is my code

public class ParseDoc {

    private final static String PCG_MODEL = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";        

    private final TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "invertible=true");

    private static final LexicalizedParser parser = LexicalizedParser.loadModel(PCG_MODEL);

    public Tree parse(String str) {                
        List<CoreLabel> tokens = tokenize(str);
        Tree tree = parser.apply(tokens);
        return tree;
    }

    private List<CoreLabel> tokenize(String str) {
        Tokenizer<CoreLabel> tokenizer =
                tokenizerFactory.getTokenizer(
                        new StringReader(str));    
        return tokenizer.tokenize();
    }

    public static void main(String[] args) { 
        String str = "My dog also likes eating sausage.";
        // Parser parser = new Parser(); 
        Tree tree = parser.parse(str);  

        List<Tree> leaves = tree.getLeaves();
        // Print words and Pos Tags
        for (Tree leaf : leaves) { 
            Tree parent = leaf.parent(tree);
            System.out.print(leaf.label().value() + "-" + parent.label().value() + " ");
        }
        System.out.println(); 
    }
}

Unfortunately I can only get is the tagging

My-PRP$ dog-NN also-RB likes-VBZ eating-VBG sausage-NN .-.

which isn't of any use to me.

I want to print the tree:

(ROOT
  (S
    (NP (PRP$ My) (NN dog))
    (ADVP (RB also))
    (VP (VBZ likes)
      (S
        (VP (VBG eating)
          (NP (NN sausage)))))
    (. .)))

and the universal dependencies:

nmod:poss(dog-2, My-1)
nsubj(likes-4, dog-2)
advmod(likes-4, also-3)
root(ROOT-0, likes-4)
xcomp(likes-4, eating-5)
dobj(eating-5, sausage-6)

How can I achieve this?


Solution

  • Here is some sample code:

    package edu.stanford.nlp.examples;
    
    import edu.stanford.nlp.pipeline.*;
    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.semgraph.*;
    import edu.stanford.nlp.util.*;
    
    import java.util.*;
    
    public class PrintParse {
    
      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);
          SemanticGraph dependencyParse =
              sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
          System.out.println(dependencyParse.toList());
        }
      }
    
    }