Cast from GrammaticalStructure to Tree

2019-08-10 04:59发布

I am trying out the new NN Dependency Parser from Stanford. According to the demo they have provided, this is how the parsing is done:

import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.parser.nndep.DependencyParser;

...

GrammaticalStructure gs = null;

DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(sentence));
for (List<HasWord> sent : tokenizer) {
    List<TaggedWord> tagged = tagger.tagSentence(sent);
    gs = parser.predict(tagged);

    // Print typed dependencies
    System.out.println(Grammatical structure: " + gs);
} 

Now, what I want to do is this object gs, which is of class GrammaticalStructure, to be casted as a Tree object from edu.stanford.nlp.trees.Tree.

I naively tried out with simple casting:

Tree t = (Tree) gs;

but, this is not possible (the IDE gives an error: Cannot cast from GrammaticalStructure to Tree).

How do I do this?

1条回答
可以哭但决不认输i
2楼-- · 2019-08-10 05:09

You should be able to get the Tree using gs.root(). According to the documentation, that method returns a Tree (actually, a TreeGraphNode) which represents the grammatical structure.

You could print that tree in a human-friendly way with gs.root().pennPrint().

查看更多
登录 后发表回答