Fluffy Fluffy - 3 months ago 21x
Java Question

How to get parent node in Stanford's JavaNLP?

Suppose I have such chunk of a sentence:

(NP (DT A) (JJ single) (NN page))
(PP (IN in)
(NP (DT a) (NN wiki) (NN website))))

At a certain moment of time I have a reference to
(JJ single)
and I want to get the
node binding
A single page
. If I get it right, that
is the parent of the node,
are its siblings and it has no children (?). When I try to use the
method of a tree, I always get null. The API says that's because the implementation doesn't know how to determine the parent node. Another method of interest is
.ancestor(int height, Tree root)
, but I don't know how to get the root of the node. In both cases, since the parser knows how to indent and group trees, it must know the "parent" tree, right? How can I get it? Thanks


It looks like the Tree itself will never return the parent of the node, since it's hardcoded in the Tree sources. Meanwhile TreeGraphNode overrides that method and works well. Changing a Tree to a TreeGraphNode is as easy as

TreeGraphNode sentence = new TreeGraph(tree).root();