/** * 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; }
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; }
@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; } }
private static Tree findCCparent(Tree t, Tree root) { if (t.isPreTerminal()) { if (t.value().startsWith("CC")) { Tree parent = t.parent(root); if (parent != null && parent.value().startsWith("NP")) { List<Tree> children = parent.getChildrenAsList(); //System.out.println(children); int ccIndex = children.indexOf(t); if (children.size() > ccIndex + 2 && notNP(children, ccIndex) && ccIndex != 0 && (ccIndex == children.size() - 1 || !children.get(ccIndex+1).value().startsWith("CC"))) { transformCC(parent, ccIndex); if (VERBOSE) { log.info("After transformCC: " + root); } return root; } } } } else { for (Tree child : t.getChildrenAsList()) { Tree cur = findCCparent(child, root); if (cur != null) { return cur; } } } return null; }
@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; } }
return tf.newTreeNode(tree.label(), Collections.singletonList(tf.newLeaf(tree.children()[0].label()))); List<Tree> children = tree.getChildrenAsList(); LinkedList<Tree> newChildren = new LinkedList<>();
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; }
protected void stripTag(Tree t) { if ( ! t.isLeaf()) { String label = cleanUpLabel(t.value()); t.setValue(label); for (Tree child : t.getChildrenAsList()) { stripTag(child); } } }
private Tree flatten(Tree tree) { if (tree.isLeaf() || tree.isPreTerminal()) { return tree; } List<Tree> newChildren = new ArrayList<>(); Tree[] children = tree.children(); for (Tree child : children) { Tree newChild = flatten(child); if (!newChild.isPreTerminal() && newChild.label().toString().equals(tree.label().toString())) { newChildren.addAll(newChild.getChildrenAsList()); } else { newChildren.add(newChild); } } return tf.newTreeNode(tree.label(), newChildren); }
/** * 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 newChild = child.flatten(tf); if (label().equals(newChild.label())) { newChildren.addAll(newChild.getChildrenAsList()); } else { newChildren.add(newChild);
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); }
/** * 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); }
protected Tree transformTreeHelper(Tree t) { if (t.isLeaf()) { Tree leaf = tf.newLeaf(t.label()); leaf.setScore(t.score()); return leaf; } List<Tree> newChildren = new ArrayList<>(); for (int childNum = 0, numKids = t.numChildren(); childNum < numKids; childNum++) { Tree child = t.getChild(childNum); Tree newChild = transformTreeHelper(child); if ((!newChild.isLeaf()) && newChild.label().value().indexOf('@') >= 0) { newChildren.addAll(newChild.getChildrenAsList()); } else { newChildren.add(newChild); } } Tree node = tf.newTreeNode(t.label(), newChildren); node.setScore(t.score()); return node; }
log.info("Pruning: " + child.label() + " from " + (childStart + start) + " to " + (childEnd + start)); log.info("Was: " + testTree + " vs " + pcfgTree); prunedChildren.addAll(child.getChildrenAsList()); } else { prunedChildren.add(child);
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; }
for (Tree child : tree.getChildrenAsList()) { Tree newChild = transformTree(child); children.add(newChild);
@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; } } }
public void printRuleCounts() { log.info(); for (Tree t : annotatedRuleCounts.keySet()) { log.info(annotatedRuleCounts.getCount(t) + "\t" + t.label().value() + " -->"); for (Tree dtr : t.getChildrenAsList()) { log.info(" "); log.info(dtr.label().value()); } log.info(); } }
private int taggedLabeledYield(List<CoreLabel> ty, int termIdx) { if (isPreTerminal()) { // usually this will fill in all the usual keys for a token CoreLabel taggedWord = new CoreLabel(firstChild().label()); // but in case this just came from reading a tree that just has a value for words if (taggedWord.word() == null) { taggedWord.setWord(firstChild().value()); } final String tag = (value() == null) ? "" : value(); // set value and tag to the tag taggedWord.setValue(tag); taggedWord.setTag(tag); taggedWord.setIndex(termIdx); ty.add(taggedWord); return termIdx + 1; } else { for (Tree kid : getChildrenAsList()) termIdx = kid.taggedLabeledYield(ty, termIdx); } return termIdx; }