private List<Tree> spliceOutHelper(Predicate<Tree> nodeFilter, TreeFactory tf) { // recurse over all children first Tree[] kids = children(); List<Tree> l = new ArrayList<>(); for (Tree kid : kids) { l.addAll(kid.spliceOutHelper(nodeFilter, tf)); } // check if this node is being spliced out if (nodeFilter.test(this)) { // no, so add our children and return Tree t; if ( ! l.isEmpty()) { t = tf.newTreeNode(label(), l); } else { t = tf.newLeaf(label()); } l = new ArrayList<>(1); l.add(t); return l; } // we're out, so return our children return l; }
private static void preTerminals(Tree t, List<Tree> l) { if (t.isPreTerminal()) { l.add(t); } else { for (Tree kid : t.children()) { preTerminals(kid, l); } } }
protected Tree findMarkedHead(Tree t) { Tree[] kids = t.children(); for (Tree kid : kids) { if (headMarkedPattern.matcher(kid.label().value()).find() || headMarkedPattern2.matcher(kid.label().value()).find()) { //log.info("found manually-labeled head " + kids[i] + " for tree " + t); return kid; } } return null; }
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); } }
public String apply(TregexMatcher m) { if(mwCounter == null) throw new RuntimeException("Cannot enable POSSequence features without POS sequence map. Use option -frenchMWMap."); Tree t = m.getMatch(); StringBuilder sb = new StringBuilder(); for(Tree kid : t.children()) { if( ! kid.isPreTerminal()) throw new RuntimeException("Not POS sequence for tree: " + t.toString()); String tag = doBasicCat ? tlp.basicCategory(kid.value()) : kid.value(); sb.append(tag).append(" "); } if(mwCounter.getCount(t.value(), sb.toString().trim()) > cutoff) return annotationMark + sb.toString().replaceAll("\\s+", "").toLowerCase(); else return ""; }
/** * returns the syntactic category of the tree as a list of the syntactic categories of the mother and the daughters */ public static List<String> localTreeAsCatList(Tree t) { List<String> l = new ArrayList<>(t.children().length + 1); l.add(t.label().value()); for (int i = 0; i < t.children().length; i++) { l.add(t.children()[i].label().value()); } return l; }
private static String localTreeToRule(Tree localTree) { StringBuilder sb = new StringBuilder(); sb.append(localTree.value()).append(" -> "); for (int i = 0; i < localTree.children().length - 1; i++) { sb.append(localTree.children()[i].value()).append(" "); } sb.append(localTree.children()[localTree.children().length - 1].value()); return sb.toString(); }
/** Returns a 0-based index of the right leaf of the tree. Assumes the leaves had been indexed from 1 */ static int rightIndex(Tree tree) { if (tree.isLeaf()) { CoreLabel label = ErasureUtils.uncheckedCast(tree.label()); return label.index() - 1; } return rightIndex(tree.children()[tree.children().length - 1]); }
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)); }
/** 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()); }
public SimpleMatrix getWForNode(Tree node) { if (node.children().length == 2) { String leftLabel = node.children()[0].value(); String leftBasic = basicCategory(leftLabel); String rightLabel = node.children()[1].value(); String rightBasic = basicCategory(rightLabel); return binaryTransform.get(leftBasic, rightBasic); } else if (node.children().length == 1) { throw new AssertionError("No unary transform matrices, only unary classification"); } else { throw new AssertionError("Unexpected tree children size of " + node.children().length); } }