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);
}