/** * Return whether this node is a preterminal or not. A preterminal is * defined to be a node with one child which is itself a leaf. * * @return true if the node is a preterminal; false otherwise */ public boolean isPreTerminal() { Tree[] kids = children(); return (kids.length == 1) && (kids[0].isLeaf()); }
/** Doesn't accept nodes that only cover an empty. */ @Override public boolean test(Tree t) { Tree[] kids = t.children(); Label l = t.label(); // Delete (return false for) empty/trace nodes (ones marked '-NONE-') return ! ((l != null) && "-NONE-".equals(l.value()) && !t.isLeaf() && kids.length == 1 && kids[0].isLeaf()); }
private static List<FactoredLexiconEvent> getTuningSet(Treebank devTreebank, FactoredLexicon lexicon, TreebankLangParserParams tlpp) { List<Tree> devTrees = new ArrayList<>(3000); for (Tree tree : devTreebank) { for (Tree subTree : tree) { if (!subTree.isLeaf()) { tlpp.transformTree(subTree, tree); } } devTrees.add(tree); } List<FactoredLexiconEvent> tuningSet = treebankToLexiconEvents(devTrees, lexicon); return tuningSet; }
public void recurseOutside(Tree tree, IdentityHashMap<Tree, double[]> probIn, IdentityHashMap<Tree, double[]> probOut) { if (tree.isLeaf() || tree.isPreTerminal()) { return; } if (tree.children().length == 1) { recountOutside(tree.children()[0], tree, probIn, probOut); } else { // length == 2 recountOutside(tree.children()[0], tree.children()[1], tree, probIn, probOut); } }
private int numEmbeddedNps(Tree mentionSubTree) { int embeddedNps = 0; for (Tree subtree : mentionSubTree.postOrderNodeList()) { if (!subtree.isLeaf() && !subtree.isPreTerminal()) { String label = ((CoreLabel) subtree.label()) .get(CoreAnnotations.ValueAnnotation.class); if (label.equals("NP")) { embeddedNps++; } } } return embeddedNps; }
private static Tree getTreeNonTerminal(Tree root, int startToken, int endToken, boolean acceptPreTerminals) { Tree t = getLowestCommonAncestor(root, startToken, endToken); if (t.isLeaf()) { t = t.parent(root); } if (!acceptPreTerminals && t.isPreTerminal()) { t = t.parent(root); } return t; }
/** * Gets the leaves of the tree. * * @param list The list in which the leaves of the tree will be * placed. Normally, this will be empty when the routine is called, * but if not, the new yield is added to the end of the list. * @return a {@code List} of the leaves. */ @SuppressWarnings("unchecked") public <T extends Tree> List<T> getLeaves(List<T> list) { if (isLeaf()) { list.add((T)this); } else { for (Tree kid : children()) { kid.getLeaves(list); } } return list; }
/** * 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); }
private static int treeToLatexHelper(Tree t, StringBuilder c, StringBuilder h, int n, int nextN, int indent) { StringBuilder sb = new StringBuilder(); for (int i=0; i<indent; i++) sb.append(" "); h.append('\n').append(sb); h.append("{\\").append(t.isLeaf() ? "" : "n").append("tnode{z").append(n).append("}{").append(t.label()).append('}'); if (!t.isLeaf()) { for (int k=0; k<t.children().length; k++) { h.append(", "); c.append("\\nodeconnect{z").append(n).append("}{z").append(nextN).append("}\n"); nextN = treeToLatexHelper(t.children()[k],c,h,nextN,nextN+1,indent+1); } } h.append('}'); return nextN; }
public static void outputTreeMatrices(BufferedWriter bout, Tree tree, IdentityHashMap<Tree, SimpleMatrix> vectors) throws IOException { if (tree.isPreTerminal() || tree.isLeaf()) { return; } for (int i = tree.children().length - 1; i >= 0; i--) { outputTreeMatrices(bout, tree.children()[i], vectors); } outputMatrix(bout, vectors.get(tree)); }
public Tree maximalNp(Tree mentionSubTree) { Tree maximalSubtree = mentionSubTree; for (Tree subtree : mentionSubTree.postOrderNodeList()) { if (!subtree.isLeaf() && !subtree.isPreTerminal()) { String label = ((CoreLabel) subtree.label()) .get(CoreAnnotations.ValueAnnotation.class); if (label.equals("NP")) { maximalSubtree = subtree; } } } return maximalSubtree; }
/** * Return whether this node is a phrasal node or not. A phrasal node * is defined to be a node which is not a leaf or a preterminal. * Worded positively, this means that it must have two or more children, * or one child that is not a leaf. * * @return {@code true} if the node is phrasal; {@code false} otherwise */ public boolean isPhrasal() { Tree[] kids = children(); return !(kids == null || kids.length == 0 || (kids.length == 1 && kids[0].isLeaf())); }
protected void tallyLocalTree(Tree lt, double weight) { // printTrainTree(null, "Tallying local tree:", lt); if (lt.isLeaf()) { // System.out.println("it's a leaf"); tallyLeaf(lt, weight); } else if (lt.isPreTerminal()) { // System.out.println("it's a preterminal"); tallyPreTerminal(lt, weight); } else { // System.out.println("it's a internal node"); tallyInternalNode(lt, weight); } }
/** * 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); }