private static Tree parentHelper(Tree parent, Tree[] kids, Tree node) { for (Tree kid : kids) { if (kid == node) { return parent; } Tree ret = node.parent(kid); if (ret != null) { return ret; } } return null; }
/** * Finds the distance from this node to the specified node. * return -1 if this is not an ancestor of node. * * @param node A subtree contained in this tree * @return the depth */ public int depth(Tree node) { Tree p = node.parent(this); if (this == node) { return 0; } if (p == null) { return -1; } int depth = 1; while (this != p) { p = p.parent(this); depth++; } return depth; }
/** * returns the maximal projection of <code>head</code> in * <code>root</code> given a {@link HeadFinder} */ public static Tree maximalProjection(Tree head, Tree root, HeadFinder hf) { Tree projection = head; if (projection == root) { return root; } Tree parent = projection.parent(root); while (hf.determineHead(parent) == projection) { projection = parent; if (projection == root) { return root; } parent = projection.parent(root); } return projection; }
@Override boolean satisfies(Tree t1, Tree t2, Tree root, final TregexMatcher matcher) { if (t1 == t2 || t1 == root) { return false; } Tree parent = t1.parent(root); return PARENT_OF.satisfies(parent, t2, root, matcher); }
@Override boolean satisfies(Tree t1, Tree t2, Tree root, final TregexMatcher matcher) { if (t1 == t2 || t1 == root) { return false; } Tree[] sisters = t1.parent(root).children(); for (int i = sisters.length - 1; i > 0; i--) { if (sisters[i] == t1) { return false; } if (sisters[i] == t2) { return sisters[i - 1] == t1; } } return false; }
@Override boolean satisfies(Tree t1, Tree t2, Tree root, final TregexMatcher matcher) { if (t1 == t2 || t1 == root) { return false; } Tree parent = t1.parent(root); Tree[] kids = parent.children(); for (int i = kids.length - 1; i > 0; i--) { if (kids[i] == t1) { return false; } if (kids[i] == t2) { return true; } } return false; }
/** * Returns the siblings of this Tree node. The siblings are all * children of the parent of this node except this node. * * @param root The root within which this tree node is contained * @return The siblings as a list, an empty list if there are no siblings. * The returned list is a modifiable new list structure, but contains * the actual children. */ public List<Tree> siblings(Tree root) { Tree parent = parent(root); if (parent == null) { return null; } List<Tree> siblings = parent.getChildrenAsList(); siblings.remove(this); return siblings; }
Tree getParent(Tree node) { if (node instanceof HasParent) { return node.parent(); } if (nodesToParents == null) { nodesToParents = new IdentityHashMap<>(); } if (nodesToParents.isEmpty()) { fillNodesToParents(root, null); } return nodesToParents.get(node); }
public static Tree applyToProjections(TreeVisitor v, Tree head, Tree root, HeadFinder hf) { Tree projection = head; Tree parent = projection.parent(root); if (parent == null && projection != root) { return null; } v.visitTree(projection); if (projection == root) { return root; } while (hf.determineHead(parent) == projection) { projection = parent; v.visitTree(projection); if (projection == root) { return root; } parent = projection.parent(root); } return projection; }
@Override public List<String> makeDependency(Tree head, Tree dep, Tree root) { List<String> result = new ArrayList<>(6); Tree headTerm = head.headTerminal(hf); Tree depTerm = dep.headTerminal(hf); result.add(headTerm.value()); result.add(depTerm.value()); result.add(head.parent(root).value()); result.add(head.value()); result.add(dep.value()); return result; }
/** Return the highest node of the (perhaps trivial) unary chain that * this node is part of. * In case this node is the only child of its parent, trace up the chain of * unaries, and return the uppermost node of the chain (the node whose * parent has multiple children, or the node that is the root of the tree). * * @param root The root of the tree that contains this subtree * @return The uppermost node of the unary chain, if this node is in a unary * chain, or else the current node */ public Tree upperMostUnary(Tree root) { Tree parent = parent(root); if (parent == null) { return this; } if (parent.numChildren() > 1) { return this; } return parent.upperMostUnary(root); }
@Override public List<String> makeDependency(Tree head, Tree dep, Tree root) { List<String> result = new ArrayList<>(6); Tree headTerm = head.headTerminal(hf); Tree depTerm = dep.headTerminal(hf); boolean headLeft = root.leftCharEdge(headTerm) < root.leftCharEdge(depTerm); result.add(headTerm.value()); result.add(depTerm.value()); result.add(head.parent(root).value()); result.add(head.value()); result.add(dep.value()); if(headLeft) result.add(leftHeaded); else result.add(rightHeaded); return result; }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { Tree result = tree; for (TsurgeonMatcher child : childMatcher) { Tree nodeToDelete = child.evaluate(tree, tregex); if (nodeToDelete == tree) { result = null; } Tree parent = nodeToDelete.parent(tree); parent.removeChild(Trees.objectEqualityIndexOf(parent,nodeToDelete)); } return result; } }
private static Tree pruneHelper(Tree root, Tree nodeToPrune) { if(nodeToPrune==root) return null; Tree parent = nodeToPrune.parent(root); parent.removeChild(Trees.objectEqualityIndexOf(parent,nodeToPrune)); if(parent.children().length==0) return pruneHelper(root,parent); return root; } }
/** * Get the right sibling of the given node, or some node which is * the right sibling of an ancestor of the given node. * * If no such node can be found, this method returns {@code null}. */ private Tree getRightSiblingOrRightAncestor(Tree t, Tree root) { Tree parent = t.parent(root); if (parent == null) return null; int idxWithinParent = parent.objectIndexOf(t); if (idxWithinParent < parent.numChildren() - 1) // Easy case: just return the immediate right sibling return parent.getChild(idxWithinParent + 1); return getRightSiblingOrRightAncestor(parent, root); }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToMove = childMatcher[0].evaluate(tree, tregex); Tree oldParent = nodeToMove.parent(tree); oldParent.removeChild(Trees.objectEqualityIndexOf(oldParent,nodeToMove)); Pair<Tree,Integer> position = locationMatcher.evaluate(tree, tregex); position.first().insertDtr(nodeToMove,position.second()); return tree; } }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { Tree oldNode = childMatcher[0].evaluate(tree, tregex); if (oldNode==tree) { if (children.length > 2) { throw new TsurgeonRuntimeException("Attempted to replace a root node with more than one node, unable to proceed"); } return childMatcher[1].evaluate(tree, tregex); } Tree parent = oldNode.parent(tree); int i = parent.objectIndexOf(oldNode); parent.removeChild(i); for (int j = 1; j < children.length; ++j) { Tree newNode = childMatcher[j].evaluate(tree, tregex); parent.insertDtr(newNode.deepCopy(), i + j - 1); } return tree; } }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = childMatcher[0].evaluate(tree, tregex); Tree parent = targetNode.parent(tree); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = adjunctionTree.copy(this, tree.treeFactory(), tree.label().labelFactory()); ft.foot.setChildren(targetNode.getChildrenAsList()); // replace match with root of auxiliary tree if (parent==null) { return ft.tree; } else { int i = parent.objectIndexOf(targetNode); parent.setChild(i,ft.tree); return tree; } } }