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?
You should be able to get the Tree using
gs.root()
. According to the documentation, that method returns a Tree (actually, aTreeGraphNode
) which represents the grammatical structure.You could print that tree in a human-friendly way with
gs.root().pennPrint()
.