Exemple #1
0
 private static <L> List<Tree<L>> spliceNodesHelper(Tree<L> tree, Filter<L> filter) {
   List<Tree<L>> splicedChildren = new ArrayList<Tree<L>>();
   for (Tree<L> child : tree.getChildren()) {
     List<Tree<L>> splicedChildList = spliceNodesHelper(child, filter);
     splicedChildren.addAll(splicedChildList);
   }
   if (filter.accept(tree.getLabel())) return splicedChildren;
   return Collections.singletonList(new Tree<L>(tree.getLabel(), splicedChildren));
 }
Exemple #2
0
 private static <L> Tree<L> pruneNodesHelper(Tree<L> tree, Filter<L> filter) {
   if (filter.accept(tree.getLabel())) return null;
   List<Tree<L>> prunedChildren = new ArrayList<Tree<L>>();
   for (Tree<L> child : tree.getChildren()) {
     Tree<L> prunedChild = pruneNodesHelper(child, filter);
     if (prunedChild != null) prunedChildren.add(prunedChild);
   }
   if (prunedChildren.isEmpty() && !tree.isLeaf()) return null;
   return new Tree<L>(tree.getLabel(), prunedChildren);
 }
Exemple #3
0
 private static <L> void renderFlat(Tree<L> tree, StringBuilder sb) {
   if (tree.isLeaf()) {
     sb.append(tree.getLabel().toString());
     return;
   }
   sb.append('(');
   sb.append(tree.getLabel().toString());
   sb.append(' ');
   sb.append(tree.getChildren().get(0).getLabel().toString());
   sb.append(')');
 }
Exemple #4
0
 private static <L> void renderChildren(
     List<Tree<L>> children, int indent, boolean parentLabelNull, StringBuilder sb) {
   boolean firstSibling = true;
   boolean leftSibIsPreTerm = true; // counts as true at beginning
   for (Tree<L> child : children) {
     renderTree(child, indent, parentLabelNull, firstSibling, leftSibIsPreTerm, false, sb);
     leftSibIsPreTerm = child.isPreTerminal();
     // CC is a special case
     if (child.getLabel() != null && child.getLabel().toString().startsWith("CC")) {
       leftSibIsPreTerm = false;
     }
     firstSibling = false;
   }
 }
Exemple #5
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);
 }
Exemple #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);
   }
 }
Exemple #7
0
 public static String transformLabel(Tree<String> tree) {
   String transformedLabel = tree.getLabel();
   int cutIndex = transformedLabel.indexOf('-');
   int cutIndex2 = transformedLabel.indexOf('=');
   if (cutIndex2 > 0 && (cutIndex2 < cutIndex || cutIndex == -1)) cutIndex = cutIndex2;
   if (cutIndex > 0 && !tree.isLeaf()) {
     transformedLabel = transformedLabel.substring(0, cutIndex);
   }
   return transformedLabel;
 }
Exemple #8
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;
 }
Exemple #9
0
 /** Display a node, implementing Penn Treebank style layout */
 private static <L> void renderTree(
     Tree<L> tree,
     int indent,
     boolean parentLabelNull,
     boolean firstSibling,
     boolean leftSiblingPreTerminal,
     boolean topLevel,
     StringBuilder sb) {
   // the condition for staying on the same line in Penn Treebank
   boolean suppressIndent =
       (parentLabelNull
           || (firstSibling && tree.isPreTerminal())
           || (leftSiblingPreTerminal
               && tree.isPreTerminal()
               && (tree.getLabel() == null || !tree.getLabel().toString().startsWith("CC"))));
   if (suppressIndent) {
     sb.append(' ');
   } else {
     if (!topLevel) {
       sb.append('\n');
     }
     for (int i = 0; i < indent; i++) {
       sb.append("  ");
     }
   }
   if (tree.isLeaf() || tree.isPreTerminal()) {
     renderFlat(tree, sb);
     return;
   }
   sb.append('(');
   sb.append(tree.getLabel());
   renderChildren(
       tree.getChildren(),
       indent + 1,
       tree.getLabel() == null || tree.getLabel().toString() == null,
       sb);
   sb.append(')');
 }
Exemple #10
0
 public Tree<E> transformTree(Tree<E> tree) {
   E label = tree.getLabel();
   List<Tree<E>> children = tree.getChildren();
   while (children.size() == 1
       && !children.get(0).isLeaf()
       && label.equals(children.get(0).getLabel())) {
     children = children.get(0).getChildren();
   }
   List<Tree<E>> transformedChildren = new ArrayList<Tree<E>>();
   for (Tree<E> child : children) {
     transformedChildren.add(transformTree(child));
   }
   return new Tree<E>(label, transformedChildren);
 }
Exemple #11
0
 public Tree<String> transformTree(Tree<String> tree) {
   String label = tree.getLabel();
   if (label.equals("-NONE-")) {
     return null;
   }
   if (tree.isLeaf()) {
     return new Tree<String>(label);
   }
   List<Tree<String>> children = tree.getChildren();
   List<Tree<String>> transformedChildren = new ArrayList<Tree<String>>();
   for (Tree<String> child : children) {
     Tree<String> transformedChild = transformTree(child);
     if (transformedChild != null) transformedChildren.add(transformedChild);
   }
   if (transformedChildren.size() == 0) return null;
   return new Tree<String>(label, transformedChildren);
 }
Exemple #12
0
 public Tree<String> transformTree(Tree<String> tree) {
   String label = tree.getLabel();
   Matcher matcher = punctuationPattern.matcher(label);
   if (matcher.matches()) {
     return null;
   }
   if (tree.isLeaf()) {
     return new Tree<String>(label);
   }
   List<Tree<String>> children = tree.getChildren();
   List<Tree<String>> transformedChildren = new ArrayList<Tree<String>>();
   for (Tree<String> child : children) {
     Tree<String> transformedChild = transformTree(child);
     if (transformedChild != null) transformedChildren.add(transformedChild);
   }
   if (transformedChildren.size() == 0) return null;
   return new Tree<String>(label, transformedChildren);
 }