@Override public Tree transformTree(Tree t) { TreeFactory tf = t.treeFactory(); return transformTreeHelper(t, t, tf); }
/** * Makes a deep copy of not only the Tree structure but of the labels as well. * Uses the TreeFactory of the root node given by treeFactory(). * Assumes that your labels give a non-null labelFactory(). * (Added by Aria Haghighi.) * * @return A deep copy of the tree structure and its labels */ public Tree deepCopy() { return deepCopy(treeFactory()); }
/** * Creates a (partial) deep copy of the tree, where all nodes that the * filter does not accept are spliced out. If the result is not a tree * (that is, it's a forest), an empty root node is generated. * * @param nodeFilter a Filter method which returns true to mean * keep this node, false to mean delete it * @return a filtered copy of the tree */ public Tree spliceOut(final Predicate<Tree> nodeFilter) { return spliceOut(nodeFilter, treeFactory()); }
/** * Create a deep copy of the tree structure. The entire structure is * recursively copied, but label data themselves are not cloned. * The copy is built using a {@code TreeFactory} that will * produce a {@code Tree} like the input one. * * @return A deep copy of the tree structure (but not its labels). */ public Tree treeSkeletonCopy() { return treeSkeletonCopy(treeFactory()); }
@Override public Tree transformTree(Tree tree) { return normalizeWholeTree(tree, tree.treeFactory()); }
/** * Create a transformed Tree. The tree is traversed in a depth-first, * left-to-right order, and the {@code TreeTransformer} is called * on each node. It returns some {@code Tree}. The transformed * tree has a new tree structure (i.e., a "deep copy" is done), but it * will usually share its labels with the original tree. * * @param transformer The function that transforms tree nodes or subtrees * @return a transformation of this {@code Tree} */ public Tree transform(final TreeTransformer transformer) { return transform(transformer, treeFactory()); }
public Tree transformTree(Tree tree) { return transformRoot(tree, tree.treeFactory()); }
/** * Return a flattened version of a tree. In many circumstances, this * will just return the tree, but if the tree is something like a * binarized version of a dependency grammar tree, then it will be * flattened back to a dependency grammar tree representation. Formally, * a node will be removed from the tree when: it is not a terminal or * preterminal, and its {@code label()} is {@code equal()} to * the {@code label()} of its parent, and all its children will * then be promoted to become children of the parent (in the same * position in the sequence of daughters. * * @return A flattened version of this tree. */ public Tree flatten() { return flatten(treeFactory()); }
public Tree transformTerminal(Tree tree) { return tree.treeFactory().newLeaf(transformTerminalLabel(tree)); }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { return subTree.copy(this, tree.treeFactory(), tree.label().labelFactory()).tree; } }
public Tree transformNonterminal(Tree tree) { List<Tree> children = new ArrayList<>(tree.children().length); for (Tree child : tree.children()) { children.add(transformHelper(child)); } return tree.treeFactory().newTreeNode(transformNonterminalLabel(tree), children); }
/** * Returns a deep copy of everything but the leaf labels. The leaf * labels are reused from the original tree. This is useful for * cases such as the dependency converter, which wants to finish * with the same labels in the dependencies as the parse tree. */ public Tree treeSkeletonConstituentCopy() { return treeSkeletonConstituentCopy(treeFactory(), label().labelFactory()); }
@Override public Tree transformTree(Tree tree) { Tree last = tree.lastChild(); if (last.label().value().equals(Lexicon.BOUNDARY_TAG) || last.label().value().equals(Lexicon.BOUNDARY)) { List<Tree> childList = tree.getChildrenAsList(); List<Tree> lastGoneList = childList.subList(0, childList.size() - 1); return tree.treeFactory().newTreeNode(tree.label(), lastGoneList); } else { return tree; } }
@Override public List<TaggedWord> next() { if (next == null) { throw new NoSuchElementException("Iterator exhausted."); } Tree t = next; if (normalizer != null) { t = normalizer.normalizeWholeTree(t, t.treeFactory()); } if (transformer != null) { t = t.transform(transformer); } findNext(); return t.taggedYield(); }
private Tree markHeadNodes(Tree t, Tree head) { if (t.isLeaf()) { return t; // don't worry about head-marking leaves } Label newLabel; if (t == head) { newLabel = headMark(t.label()); } else { newLabel = t.label(); } Tree newHead = hf.determineHead(t); return t.treeFactory().newTreeNode(newLabel, Arrays.asList(headMarkChildren(t, newHead))); }
/** * Normalize a whole tree. * TueBa-D/Z adaptation. Fixes trees with non-unary roots, does nothing else. */ @Override public Tree normalizeWholeTree(Tree tree, TreeFactory tf) { if (tree.label().value().equals(root) && tree.children().length > 1) { Tree underRoot = tree.treeFactory().newTreeNode(root, tree.getChildrenAsList()); tree.setChildren(new Tree[1]); tree.setChild(0, underRoot); } // we just want the non-unary root fixed. return tree; }
Tree prune(Tree tree, int start) { if (tree.isLeaf() || tree.isPreTerminal()) { return tree; } // check each node's children for deletion List<Tree> children = helper(tree.getChildrenAsList(), start); children = prune(children, tree.label(), start, start + tree.yield().size()); return tree.treeFactory().newTreeNode(tree.label(), children); }
/** * Remove everything but the skeleton, the predictions, and the labels */ private Tree simplifyTree(Tree tree) { CoreLabel newLabel = new CoreLabel(); newLabel.set(RNNCoreAnnotations.Predictions.class, RNNCoreAnnotations.getPredictions(tree)); newLabel.setValue(tree.label().value()); if (tree.isLeaf()) { return tree.treeFactory().newLeaf(newLabel); } List<Tree> children = Generics.newArrayList(tree.children().length); for (int i = 0; i < tree.children().length; ++i) { children.add(simplifyTree(tree.children()[i])); } return tree.treeFactory().newTreeNode(newLabel, children); }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { // find match Tree targetNode = childMatcher[0].evaluate(tree, tregex); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = adjunctionTree().copy(this, tree.treeFactory(), tree.label().labelFactory()); ft.foot.setChildren(targetNode.getChildrenAsList()); // put children of auxiliary tree under target. root of auxiliary tree is ignored. root of original is maintained. targetNode.setChildren(ft.tree.getChildrenAsList()); return tree; } }
public Tree transformTree(Tree tree) { if (tree.isPreTerminal() || tree.isLeaf()) { return tree.deepCopy(); } Label label = tree.label().labelFactory().newLabel(tree.label()); Tree[] children = tree.children(); while (children.length == 1 && !children[0].isLeaf()) { children = children[0].children(); } List<Tree> processedChildren = Generics.newArrayList(); for (Tree child : children) { processedChildren.add(transformTree(child)); } return tree.treeFactory().newTreeNode(label, processedChildren); } }