/** * Set the children of this tree node to the given list. This * method is implemented in the {@code Tree} class by * converting the {@code List} into a tree array and calling * the array-based method. Subclasses which use a * {@code List}-based representation of tree children should * override this method. This implementation allows the case * that the {@code List} is {@code null}: it yields a * node with no children (represented by a canonical zero-length * children() array). * * @param childTreesList A list of trees to become children of the node. * This method does not retain the List that you pass it (copying * is done), but it will retain the individual children (they are * not copied). * @see #setChildren(Tree[]) */ public void setChildren(List<? extends Tree> childTreesList) { if (childTreesList == null || childTreesList.isEmpty()) { setChildren(EMPTY_TREE_ARRAY); } else { Tree[] childTrees = new Tree[childTreesList.size()]; childTreesList.toArray(childTrees); setChildren(childTrees); } }
/** * insert {@code dtr} after {@code position} existing * daughters in {@code this}. */ public void insertDtr(Tree dtr, int position) { Tree[] kids = children(); if (position > kids.length) { throw new IllegalArgumentException("Can't insert tree after the " + position + "th daughter in " + this + "; only " + kids.length + " daughters exist!"); } Tree[] newKids = new Tree[kids.length + 1]; int i = 0; for (; i < position; i++) { newKids[i] = kids[i]; } newKids[i] = dtr; for (; i < kids.length; i++) { newKids[i + 1] = kids[i]; } setChildren(newKids); }
/** * Adds the tree t at the index position among the daughters. Note * that this method will throw an {@link ArrayIndexOutOfBoundsException} if * the daughter index is too big for the list of daughters. * * @param i the index position at which to add the new daughter * @param t the new daughter */ public void addChild(int i, Tree t) { Tree[] kids = children(); Tree[] newKids = new Tree[kids.length + 1]; if (i != 0) { System.arraycopy(kids, 0, newKids, 0, i); } newKids[i] = t; if (i != kids.length) { System.arraycopy(kids, i, newKids, i + 1, kids.length - i); } setChildren(newKids); }
static Tree removeDependencyRoot(Tree tree) { List<Tree> childList = tree.getChildrenAsList(); Tree last = childList.get(childList.size() - 1); if (!last.label().value().equals(Lexicon.BOUNDARY_TAG)) { return tree; } List<Tree> lastGoneList = childList.subList(0, childList.size() - 1); tree.setChildren(lastGoneList); return tree; }
/** * Destructively removes the child at some daughter index and returns it. * Note * that this method will throw an {@link ArrayIndexOutOfBoundsException} if * the daughter index is too big for the list of daughters. * * @param i The daughter index * @return The tree at that daughter index */ public Tree removeChild(int i) { Tree[] kids = children(); Tree kid = kids[i]; Tree[] newKids = new Tree[kids.length - 1]; for (int j = 0; j < newKids.length; j++) { if (j < i) { newKids[j] = kids[j]; } else { newKids[j] = kids[j + 1]; } } setChildren(newKids); return kid; }
/** * 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<>(kids.length); for (Tree kid : kids) { if (kid != node) { newKids.add(kid); replaceNode(node, node1, kid); } else { newKids.add(node1); } } t.setChildren(newKids); }
@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; } }
private Tree fixNonUnaryRoot(Tree t, TreeFactory tf) { List<Tree> kids = t.getChildrenAsList(); if(kids.size() == 2 && t.firstChild().isPhrasal() && tlp.isSentenceFinalPunctuationTag(t.lastChild().value())) { List<Tree> grandKids = t.firstChild().getChildrenAsList(); grandKids.add(t.lastChild()); t.firstChild().setChildren(grandKids); kids.remove(kids.size() - 1); t.setChildren(kids); t.setValue(tlp.startSymbol()); } else { t.setValue(nonUnaryRoot); t = tf.newTreeNode(tlp.startSymbol(), Collections.singletonList(t)); } return t; }
/** * 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; }
treeCopy.foot.setChildren(innerChildren); children.add(treeCopy.tree); parent.setChildren(children);
public 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; }
private void insertNPinPP(Tree t) { if (tlp.basicCategory(t.label().value()).equals("PP")) { Tree[] kids = t.children(); int i = 0; int j = kids.length - 1; while (i < j && prepositionTags.contains(tlp.basicCategory(kids[i].label().value()))) { i++; } // i now indexes first dtr of new NP while (i < j && postpositionTags.contains(tlp.basicCategory(kids[j].label().value()))) { j--; } // j now indexes last dtr of new NP if (i > j) { log.info("##### Warning -- no NP material here!"); return; // there is no NP material! } int npKidsLength = j - i + 1; Tree[] npKids = new Tree[npKidsLength]; System.arraycopy(kids, i, npKids, 0, npKidsLength); Tree np = t.treeFactory().newTreeNode(t.label().labelFactory().newLabel("NP"), Arrays.asList(npKids)); Tree[] newPPkids = new Tree[kids.length - npKidsLength + 1]; System.arraycopy(kids, 0, newPPkids, 0, i + 1); newPPkids[i] = np; System.arraycopy(kids, j + 1, newPPkids, i + 1, kids.length - j - 1); t.setChildren(newPPkids); System.out.println("#### inserted NP in PP"); t.pennPrint(); } }
/** * Changes the ROOT label, and adds a Lexicon.BOUNDARY daughter to it. * This is needed for the dependency parser. * <i>Note:</i> This is a destructive operation on the tree passed in!! * * @param t The current tree into which a boundary is inserted */ public void addRoot(Tree t) { if (t.isLeaf()) { log.info("Warning: tree is leaf: " + t); t = tf.newTreeNode(tlp.startSymbol(), Collections.singletonList(t)); } t.setLabel(new CategoryWordTag(tlp.startSymbol(), Lexicon.BOUNDARY, Lexicon.BOUNDARY_TAG)); List<Tree> preTermChildList = new ArrayList<>(); Tree boundaryTerm = tf.newLeaf(new Word(Lexicon.BOUNDARY));//CategoryWordTag(Lexicon.BOUNDARY,Lexicon.BOUNDARY,"")); preTermChildList.add(boundaryTerm); Tree boundaryPreTerm = tf.newTreeNode(new CategoryWordTag(Lexicon.BOUNDARY_TAG, Lexicon.BOUNDARY, Lexicon.BOUNDARY_TAG), preTermChildList); List<Tree> childList = t.getChildrenAsList(); childList.add(boundaryPreTerm); t.setChildren(childList); }
/** * Perform (possibly destructive) operations on the tree. Do a top-down DFS on the tree. */ public void visitTree(Tree tree) { if (tree == null) return; String yield = SentenceUtils.listToString(tree.yield()); if (mweDictionary.contains(yield)) { List<Tree> children = getPreterminalSubtrees(tree); String newLabel = "MW" + tree.value(); tree.setValue(newLabel); tree.setChildren(children); // Bottom out of the recursion return; } else { for (Tree subTree : tree.children()) { if (subTree.isPhrasal()) { // Only phrasal trees can have yields > 1!! visitTree(subTree); } } } }
public Tree arabicAoverAFilter(Tree t) { if(t == null || t.isLeaf() || t.isPreTerminal()) return t; //Specific nodes to filter out if(t.numChildren() == 1) { final Tree fc = t.firstChild(); //A over A nodes i.e. from BobChrisTreeNormalizer if(t.label() != null && fc.label() != null && t.value().equals(fc.value())) { t.setChildren(fc.children()); } } for(Tree kid : t.getChildrenAsList()) arabicAoverAFilter(kid); return t; }
public Tree transformTree(Tree t) { TregexMatcher matcher = tregexMonthYear.matcher(t); while (matcher.find()) { Tree root = matcher.getNode("root"); Tree month = matcher.getNode("month"); Tree year = matcher.getNode("year"); Tree[] children = new Tree[] {month, year}; root.setChildren(children); matcher = tregexMonthYear.matcher(t); } matcher = tregexMonthDayYear.matcher(t); while (matcher.find()) { Tree root = matcher.getNode("root"); Tree month = matcher.getNode("month"); Tree day = matcher.getNode("day"); Tree comma = matcher.getNode("comma"); Tree year = matcher.getNode("year"); Tree[] children = new Tree[] {month, day, comma, year}; root.setChildren(children); matcher = tregexMonthDayYear.matcher(t); } return t; } }
@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; } } }
newKids.add(transformTreeHelper(kid, root, tf)); result.setChildren(newKids); return result;
t.setChildren(newNodes); t.setValue(phraseValue); } else {
tree.setChildren(tree.children()[0].children());