/** * Says whether a node is a leaf. Can be used on an arbitrary * {@code Tree}. Being a leaf is defined as having no * children. This must be implemented as returning a zero-length * Tree[] array for children(). * * @return true if this object is a leaf */ public boolean isLeaf() { return numChildren() == 0; }
/** * Says whether the current node has only one child. * Can be used on an arbitrary {@code Tree}. * * @return Whether the node heads a unary rewrite */ public boolean isUnaryRewrite() { return numChildren() == 1; }
@Override void initialize() { int childNum = ithChildOf.childNum; if (t.numChildren() >= Math.abs(childNum)) { if (childNum > 0) { next = t.getChild(childNum - 1); } else { next = t.getChild(t.numChildren() + childNum); } } } };
@Override void advance() { if (searchStack.isEmpty()) { next = null; } else { next = searchStack.pop(); if (pathMatchesNode(next)) { for (int i = next.numChildren() - 1; i >= 0; i--) { searchStack.push(next.getChild(i)); } } } } };
@Override public void initialize() { searchStack = new Stack<>(); for (int i = t.numChildren() - 1; i >= 0; i--) { searchStack.push(t.getChild(i)); } if (!searchStack.isEmpty()) { advance(); } }
@Override void initialize() { if (t != matcher.getRoot()) { next = matcher.getParent(t); if (childNum > 0 && (next.numChildren() < childNum || next .getChild(childNum - 1) != t) || childNum < 0 && (next.numChildren() < -childNum || next.getChild(next .numChildren() + childNum) != t)) { next = null; } } } };
public boolean test(Tree t) { return ! (t.numChildren() == 1 && "X".equals(t.firstChild().value())); }
private static String treeToRuleString(Tree tree) { StringBuilder sb = new StringBuilder(); sb.append(tree.value()).append(":").append(tree.firstChild().value()); for (int i = 1; i < tree.numChildren(); ++i) { Tree kid = tree.children()[i]; sb.append("-").append(kid.value()); } return sb.toString(); }
private static void localTreeString(Tree t, StringBuilder sb, int level) { sb.append('\n'); for (int i = 0; i < level; i++) { sb.append(" "); } sb.append('(').append(t.label()); if (level == 0 || isSynthetic(t.label().value())) { // if it is synthetic, recurse for (int c = 0; c < t.numChildren(); c++) { localTreeString(t.getChild(c), sb, level + 1); } } sb.append(')'); }
/** 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 Tree normalizeWholeTree(Tree tree, TreeFactory tf) { tree = tree.prune(hebrewEmptyFilter, tf).spliceOut(aOverAFilter, tf); //Add start symbol so that the root has only one sub-state. Escape any enclosing brackets. //If the "tree" consists entirely of enclosing brackets e.g. ((())) then this method //will return null. In this case, readers e.g. PennTreeReader will try to read the next tree. while(tree != null && (tree.value() == null || tree.value().equals("")) && tree.numChildren() <= 1) tree = tree.firstChild(); if(tree != null && !tree.value().equals(tlp.startSymbol())) tree = tf.newTreeNode(tlp.startSymbol(), Collections.singletonList(tree)); return tree; }
/** * 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); }
private Tree determineBinaryHead(Tree t) { if (t.numChildren() == 1) { return t.firstChild(); } else { String lval = t.firstChild().label().value(); if (lval != null && lval.startsWith("@")) { return t.firstChild(); } else { String rval = t.lastChild().label().value(); if (rval.startsWith("@") || rval.equals(Lexicon.BOUNDARY_TAG)) { return t.lastChild(); } } } return null; }
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; }
/** Doesn't accept nodes that are A over A nodes (perhaps due to * empty removal or are EDITED nodes). */ @SuppressWarnings("SimplifiableIfStatement") @Override public boolean test(Tree t) { if (t.isLeaf() || t.isPreTerminal()) { return true; } // The special switchboard non-terminals clause if ("EDITED".equals(t.label().value()) || "CODE".equals(t.label().value())) { return false; } if (t.numChildren() != 1) { return true; } return ! (t.label() != null && t.label().value() != null && t.label().value().equals(t.getChild(0).label().value())); }
public Tree helper(Tree t) { if (t == null) { return null; } else if (t.isLeaf()) { return tf.newLeaf(t.label().value()); } else if (t.isPreTerminal()) { return tf.newTreeNode(t.label().value(), Collections.singletonList(helper(t.children()[0]))); } else { int numKids = t.numChildren(); List<Tree> children = new ArrayList<>(numKids); for (int k = 0; k < numKids; k++) { children.add(helper(t.children()[k])); } return tf.newTreeNode(t.label().value(), children); } }
public Tree apply(Tree t) { if (t == null) { return null; } else if (t.isLeaf()) { return tf.newLeaf(t.label().toString()); } else if (t.isPreTerminal()) { return tf.newTreeNode(t.label().toString(), Collections.singletonList(apply(t.children()[0]))); } else { int numKids = t.numChildren(); List<Tree> children = new ArrayList<>(numKids); for (int k = 0; k < numKids; k++) { children.add(apply(t.children()[k])); } return tf.newTreeNode(t.label().toString(), children); } } }
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; }
@Override public boolean test(Tree t) { if (t.isLeaf() || t.isPreTerminal()) { return true; } // The special switchboard non-terminals clause. Try keeping EDITED for now.... // if ("EDITED".equals(t.label().value())) { // return false; // } if (t.numChildren() != 1) { return true; } if (t.label() != null && t.label().value() != null && t.label().value().equals(t.children()[0].label().value())) { return false; } return true; } };
public Tree helper(Tree t) { if (t == null) { return null; } if (t.isLeaf()) { return tf.newLeaf(new StringLabel(t.label().value())); } if (t.isPreTerminal()) { return tf.newTreeNode(new StringLabel(t.label().value()), Collections.singletonList(helper(t.children()[0]))); } int numKids = t.numChildren(); List<Tree> children = new ArrayList<>(numKids); for (int k = 0; k < numKids; k++) { children.add(helper(t.children()[k])); } return tf.newTreeNode(new StringLabel(t.label().value()), children); }