views:

224

answers:

1

Hello. Suppose I have such chunk of a sentence:

(NP
      (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 NP node binding A single page. If I get it right, that NP is the parent of the node, A and page are its siblings and it has no children (?). When I try to use the .parent() 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

A: 

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();
roddik
@dmcer - fine, so there is the `sentence.transform(transformer)` method, there is separate transformer class and another one that initiates transformation. *Where* do you suggest to keep the current root?
roddik
@dmcer - in the transform method I'm overriding, I need to know the parent of the current tree that is being transformed. Since the only parameter of the transform method is the tree, how do I get the parent of that tree? That is, I want to use root while transforming, not after
roddik
@dmcer - it is applied `traversed in a depth-first, left-to-right order, and the TreeTransformer is called on each node`. To transform a tree I need info about its parent. This way I can hold all code processing certain trees in one place. You suggest moving this code to the code, processing all possible parents of all nodes?
roddik
I think this probably isn't worth arguing over. Yes, it is possible to just make sure that all the `Tree` objects you're transforming are actually instances of `TreeGraph` so that you can call `parent()`. However, you will end up with a `TreeTransformer` that **throws a `RuntimeException`/`NullPointerException` exception if someone else mistakenly gives it a regular `Tree` object**. Could you edit the question and this answer to mention `TreeTransformer` and the issues with using `Tree.transform`? Maybe also add what sort of operation you are performing on the trees.
dmcer