Pergunta

I am attempting to add a new node to a stanford parse tree although what I have currently tried is not working:

So I use TregexMatcher and get back a tree like:

(VP 
    (VB run) 
    (PP 
        (To to) 
        (NP 
            (DT the) 
            (NN shop) 
        ) 
    )
)

I am trying to insert (VBD did) to be the first child of (VP) giving the tree:

(VP 
    (VBD did)
    (VB run) 
    (PP 
        (To to) 
        (NP 
            (DT the) 
            (NN shop) 
        ) 
    )
)

Here is the code I have tried:

private Tree addNode(Tree tree, Label posTag, Label value, int index) {

    SimpleTree posNode = new SimpleTree(posTag);
    posNode.setValue(posTag.value());

    SimpleTree valueNode = new SimpleTree(value);
    valueNode.setValue(value.value());

    posNode.addChild(valueNode);

    tree.insertDtr(posNode, index);
    return tree;
}

I have been using the online documentation although I am not sure exactly how to approach this issue.

Where am I going wrong? Is there an example online someone could link me to?

EDIT: The altered tree after the above code is:

(VP
    ( ) 
    (VB run)
    (PP 
        (TO to)
        (NP 
            (DT the) 
            (NN shop)
        )
    )
)
Foi útil?

Solução

The mistake is using SimpleTree. A SimpleTree is just a tree structure with no node labels, and so isn't useful for much. I've added a note in the documentation saying that. With LabeledScoredTreeNode it works fine:

private static Tree addNodeFixed(Tree tree, Label posTag, Label value, int index) {
  Tree posNode = new LabeledScoredTreeNode(posTag);
  posNode.setValue(posTag.value());
  Tree valueNode = new LabeledScoredTreeNode(value);
  valueNode.setValue(value.value());
  posNode.addChild(valueNode);

  tree.insertDtr(posNode, index);
  return tree;
}

However, if you're using Tregex to match trees anyway, you might find it easier to do things with Tsurgeon. Watch for the infinite loops on insertions, though!

private static Tree addNodeTsurgeon(Tree tree, String tag, String word) {
  TregexPattern pat = TregexPattern.compile("__=root !> __ !< " + tag);
  TsurgeonPattern surgery = Tsurgeon.parseOperation(
          "insert (" + tag + " " + word +") >1 root");
  return Tsurgeon.processPattern(pat, surgery, tree);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top