Exemplo n.º 1
0
 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);
   }
 }
Exemplo n.º 2
0
    private static Tree<String> binarizeTree(Tree<String> tree) {
      String label = tree.getLabel();
      if (tree.isLeaf()) {
        return new Tree<String>(label);
      }

      /*
        // [Average]    P: 48.53   R: 47.67   F1: 48.10   EX:  4.85
        if (tree.getChildren().size() == 1) {
            return new Tree<String>(label, Collections.singletonList(binarizeTree(tree.getChildren().get(0))));
        }
      */

      //  [Average]    P: 48.53   R: 47.67   F1: 48.10   EX:  4.85
      if (tree.getChildren().size() <= 2) {
        List<Tree<String>> children = new ArrayList<Tree<String>>(2);
        for (Tree<String> child : tree.getChildren()) {
          children.add(binarizeTree(child));
        }
        return new Tree<String>(label, children);
      }

      // otherwise, it's a binary-or-more local tree,
      // so decompose it into a sequence of binary and unary trees.
      String intermediateLabel = "@" + label + "->";
      Tree<String> intermediateTree = binarizeTreeHelper(tree, 0, intermediateLabel);
      return new Tree<String>(label, intermediateTree.getChildren());
    }
Exemplo n.º 3
0
 private static Tree<String> binarizeTreeHelper(
     Tree<String> tree, int numChildrenGenerated, String intermediateLabel) {
   Tree<String> leftTree = tree.getChildren().get(numChildrenGenerated);
   List<Tree<String>> children = new ArrayList<Tree<String>>();
   children.add(binarizeTree(leftTree));
   if (numChildrenGenerated < tree.getChildren().size() - 1) {
     Tree<String> rightTree =
         binarizeTreeHelper(
             tree, numChildrenGenerated + 1, intermediateLabel + "_" + leftTree.getLabel());
     children.add(rightTree);
   }
   return new Tree<String>(intermediateLabel, children);
 }
Exemplo n.º 4
0
 private static <L> Tree<L> deepCopy(Tree<L> tree) {
   List<Tree<L>> childrenCopies = new ArrayList<Tree<L>>();
   for (Tree<L> child : tree.getChildren()) {
     childrenCopies.add(deepCopy(child));
   }
   return new Tree<L>(tree.getLabel(), childrenCopies);
 }
Exemplo n.º 5
0
 private static <L> void traversalHelper(Tree<L> tree, List<Tree<L>> traversal, boolean preOrder) {
   if (preOrder) traversal.add(tree);
   for (Tree<L> child : tree.getChildren()) {
     traversalHelper(child, traversal, preOrder);
   }
   if (!preOrder) traversal.add(tree);
 }
Exemplo n.º 6
0
 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);
   }
 }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
 private BinaryRule makeBinaryRule(Tree<String> tree) {
   return new BinaryRule(
       tree.getLabel(),
       tree.getChildren().get(0).getLabel(),
       tree.getChildren().get(1).getLabel());
 }