private void tallyTree( Tree<String> tree, Counter<String> symbolCounter, Counter<UnaryRule> unaryRuleCounter, Counter<BinaryRule> binaryRuleCounter) { if (tree.isLeaf()) return; if (tree.isPreTerminal()) return; if (tree.getChildren().size() == 1) { UnaryRule unaryRule = makeUnaryRule(tree); symbolCounter.incrementCount(tree.getLabel(), 1.0); unaryRuleCounter.incrementCount(unaryRule, 1.0); } if (tree.getChildren().size() == 2) { BinaryRule binaryRule = makeBinaryRule(tree); symbolCounter.incrementCount(tree.getLabel(), 1.0); binaryRuleCounter.incrementCount(binaryRule, 1.0); } if (tree.getChildren().size() < 1 || tree.getChildren().size() > 2) { throw new RuntimeException( "Attempted to construct a Grammar with an illegal tree: " + tree); } for (Tree<String> child : tree.getChildren()) { tallyTree(child, symbolCounter, unaryRuleCounter, binaryRuleCounter); } }
private static <L> void appendPreTerminalYield(Tree<L> tree, List<L> yield) { if (tree.isPreTerminal()) { yield.add(tree.getLabel()); return; } for (Tree<L> child : tree.getChildren()) { appendPreTerminalYield(child, yield); } }
private static <L> int toConstituentCollectionHelper( Tree<L> tree, int start, List<Constituent<L>> constituents) { if (tree.isLeaf() || tree.isPreTerminal()) return 1; int span = 0; for (Tree<L> child : tree.getChildren()) { span += toConstituentCollectionHelper(child, start + span, constituents); } constituents.add(new Constituent<L>(tree.getLabel(), start, start + span)); return span; }
private int tallySpans(Tree<String> tree, int start) { if (tree.isLeaf() || tree.isPreTerminal()) return 1; int end = start; for (Tree<String> child : tree.getChildren()) { int childSpan = tallySpans(child, end); end += childSpan; } String category = tree.getLabel(); if (!category.equals("ROOT")) spanToCategories.incrementCount(end - start, category, 1.0); return end - start; }