/** * 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()); }
/** * Applies the TreeVisitor, but only to the trees that pass the * filter. Applies the visitor to a copy of the tree. * * @param tv A class that can process trees. */ @Override public void apply(TreeVisitor tv) { if (VERBOSE) { System.out.println("Applying " + tv + " to treebank"); } for (Tree t : treebank) { if (!filter.test(t)) { if (VERBOSE) System.out.println(" Skipping " + t); continue; } Tree tmpT = t.deepCopy(); if (VERBOSE) System.out.println(" Applying to " + tmpT); tv.visitTree(tmpT); } }
public static Tree mapTree(Tree t) { if ( ! loaded) { load(); } if (operations == null) { return t; } return Tsurgeon.processPatternsOnTree(operations, t.deepCopy()); }
/** * Makes a deep copy of not only the Tree structure but of the labels as well. * The new tree will have nodes made by the given TreeFactory. * Each Label is copied using the labelFactory() returned * by the corresponding node's label. * It assumes that your labels give non-null labelFactory. * (Added by Aria Haghighi.) * * @param tf The TreeFactory used to make all nodes in the copied * tree structure * @return A Tree that is a deep copy of the tree structure and * Labels of the original tree. */ public Tree deepCopy(TreeFactory tf) { return deepCopy(tf, label().labelFactory()); }
/** * Applies the TreeVisitor to to all trees in the Treebank. * * @param tv A class that can process trees. */ @Override public void apply(TreeVisitor tv) { for (Tree t : tb) { if (VERBOSE) System.out.println("TfTbApply transforming " + t); Tree tmpT = t.deepCopy(); if (transformer != null) { tmpT = transformer.transformTree(tmpT); } if (VERBOSE) System.out.println(" to " + tmpT); tv.visitTree(tmpT); } }
/** Do the category splitting of the tree passed in. * This method defensively copies its argument, which is not changed. * * @param t The tree to be annotated. This can be any tree with a * {@code value()} stored in Labels. The tree is assumed to have * preterminals that are parts of speech. * @return The annotated version of the Tree (which is a completely * separate Tree with new tree structure and new labels). The * non-leaf nodes of the tree will be CategoryWordTag objects. */ @Override public Tree transformTree(Tree t) { // make a defensive copy which the helper method can then mangle Tree copy = t.deepCopy(tf); if (trainOptions.markStrahler) { markStrahler(copy); } return transformTreeHelper(copy, copy); }
private ModelDerivatives scoreDerivatives(List<Tree> trainingBatch) { // "final" makes this as fast as having separate maps declared in this function final ModelDerivatives derivatives = new ModelDerivatives(model); List<Tree> forwardPropTrees = Generics.newArrayList(); for (Tree tree : trainingBatch) { Tree trainingTree = tree.deepCopy(); // this will attach the error vectors and the node vectors // to each node in the tree forwardPropagateTree(trainingTree); forwardPropTrees.add(trainingTree); } for (Tree tree : forwardPropTrees) { backpropDerivativesAndError(tree, derivatives.binaryTD, derivatives.binaryCD, derivatives.binaryTensorTD, derivatives.unaryCD, derivatives.wordVectorD); derivatives.error += sumError(tree); } return derivatives; }
Tree backupCopy = tree.deepCopy(); tree = tt.transformTree(tree); if (tree.firstChild().children().length == 0) {
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { Tree nodeToInsert = childMatcher[0].evaluate(tree, tregex); Pair<Tree,Integer> position = locationMatcher.evaluate(tree, tregex); position.first().insertDtr(needsCopy ? nodeToInsert.deepCopy() : nodeToInsert, position.second()); return tree; } }
public Tree transformTree(Tree tree) { Tree treeCopy = tree.deepCopy(); for (Tree subtree : treeCopy) { if (subtree.depth() < 2) { continue; } String categoryLabel = subtree.label().toString(); Label label = subtree.label(); label.setFromString(categoryLabel+"-t1"); } return treeCopy; } }
public Tree transformTree(Tree tree) { Tree treeCopy = tree.deepCopy(); for (Tree subtree : treeCopy) { if (subtree.depth() < 2) { continue; } String categoryLabel = subtree.label().toString(); Label label = subtree.label(); label.setFromString(categoryLabel+"-t3"); } return treeCopy; } }
public Tree transformTree(Tree tree) { Tree treeCopy = tree.deepCopy(); for (Tree subtree : treeCopy) { if (subtree.depth() < 1) { continue; } String categoryLabel = subtree.label().toString(); Label label = subtree.label(); label.setFromString(categoryLabel+"-t2"); } return treeCopy; } }
/** * Makes a deep copy of not only the Tree structure but of the labels as well. * Each tree is copied with the given TreeFactory. * Each Label is copied using the given LabelFactory. * That is, the tree and label factories can transform the nature of the * data representation. * * @param tf The TreeFactory used to make all nodes in the copied * tree structure * @param lf The LabelFactory used to make all nodes in the copied * tree structure * @return A Tree that is a deep copy of the tree structure and * Labels of the original tree. */ @SuppressWarnings({"unchecked"}) public Tree deepCopy(TreeFactory tf, LabelFactory lf) { Label label = lf.newLabel(label()); if (isLeaf()) { return tf.newLeaf(label); } Tree[] kids = children(); // NB: The below list may not be of type Tree but TreeGraphNode, so we leave it untyped List newKids = new ArrayList(kids.length); for (Tree kid : kids) { newKids.add(kid.deepCopy(tf, lf)); } return tf.newTreeNode(label, newKids); }
/** Use the DependencyGrammar to score the tree. * * @param tr A binarized tree (as returned by the PCFG parser * @return The score for the tree according to the grammar */ private double depScoreTree(Tree tr) { // log.info("Here's our tree:"); // tr.pennPrint(); // log.info(Trees.toDebugStructureString(tr)); Tree cwtTree = tr.deepCopy(new LabeledScoredTreeFactory(), new CategoryWordTagFactory()); cwtTree.percolateHeads(binHeadFinder); // log.info("Here's what it went to:"); // cwtTree.pennPrint(); List<IntDependency> deps = MLEDependencyGrammar.treeToDependencyList(cwtTree, wordIndex, tagIndex); // log.info("Here's the deps:\n" + deps); return dg.scoreAll(deps); }
/** * Recognize candidate patterns for expansion in the given tree and * perform the expansions. See the class documentation for more * information. */ public Tree expandPhrases(Tree t, TreeNormalizer tn, TreeFactory tf) { // Keep running this sequence of patterns until no changes are // affected. We need this for nested expressions like "para tratar // de regresar al empleo." This first step produces lots of // "intermediate" tree structures which need to be cleaned up later. Tree oldTree; do { oldTree = t.deepCopy(); t = Tsurgeon.processPatternsOnTree(firstStepExpansions, t); } while (!t.equals(oldTree)); // Now clean up intermediate tree structures t = Tsurgeon.processPatternsOnTree(intermediateExpansions, t); // Normalize first to allow for contraction expansion, etc. t = tn.normalizeWholeTree(t, tf); // Final cleanup t = Tsurgeon.processPatternsOnTree(finalCleanup, t); return t; }
@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; } }
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); } }
Tree preProcessed = newTree.deepCopy(); newTree = Tsurgeon.processPattern(fixupTregex[i], fixupTsurgeon[i], newTree); if (!preProcessed.equals(newTree)) {
Tree tr2 = trTree.deepCopy(new LabeledScoredTreeFactory(), new StringLabelFactory()); Set<Tree> localTrees = tr2.localTrees(); for (Tree tr : localTrees) {