/** replaces all instances (by ==) of node with node1. Doesn't affect the node t itself */ public static void replaceNode(Tree node, Tree node1, Tree t) { if (t.isLeaf()) return; Tree[] kids = t.children(); List<Tree> newKids = new ArrayList<Tree>(kids.length); for (int i = 0, n = kids.length; i < n; i++) { if (kids[i] != node) { newKids.add(kids[i]); replaceNode(node, node1, kids[i]); } else { newKids.add(node1); } } t.setChildren(newKids); }
public static Tree untransformTree(Tree tree) { TreeFactory tf = tree.treeFactory(); if (tree.isPrePreTerminal()) { if (tree.firstChild().label().value().matches(".*_.")) { StringBuilder word = new StringBuilder(); for (int i = 0; i < tree.children().length; i++) { Tree child = tree.children()[i]; word.append(child.firstChild().label().value()); } Tree newChild = tf.newLeaf(word.toString()); tree.setChildren(Collections.singletonList(newChild)); } } else { for (int i = 0; i < tree.children().length; i++) { Tree child = tree.children()[i]; untransformTree(child); } } return tree; }