Пример #1
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);
 }
Пример #2
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));
 }
Пример #3
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);
   }
 }
Пример #4
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);
 }
Пример #5
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;
 }
Пример #6
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;
 }
Пример #7
0
 public Tree<String> transformTree(Tree<String> tree) {
   String transformedLabel = transformLabel(tree);
   if (tree.isLeaf()) {
     return new Tree<String>(transformedLabel);
   }
   List<Tree<String>> transformedChildren = new ArrayList<Tree<String>>();
   for (Tree<String> child : tree.getChildren()) {
     transformedChildren.add(transformTree(child));
   }
   return new Tree<String>(transformedLabel, transformedChildren);
 }
Пример #8
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(')');
 }
Пример #9
0
 public void toStringBuilder(StringBuilder sb) {
   if (!isLeaf()) sb.append('(');
   if (getLabel() != null) {
     sb.append(getLabel());
   }
   if (!isLeaf()) {
     for (Tree<L> child : getChildren()) {
       sb.append(' ');
       child.toStringBuilder(sb);
     }
     sb.append(')');
   }
 }
Пример #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);
 }
Пример #11
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;
   }
 }
Пример #12
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);
 }
Пример #13
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);
 }
Пример #14
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);
 }
Пример #15
0
 /**
  * Recursively transforms each node of <code>tree</code> parameter according to the passed in
  * <code>LabelFactory</code>. None of the structure of the <code>Tree</code> is altered only the
  * information stored in the Label at each node.
  *
  * @param <S> Original Tree label type
  * @param <T> Output tree label type
  * @param tree
  * @param labelFactory
  * @return Tree with transformed labels according to <code>labelFactory</code>
  */
 public static <S, T> Tree<T> transformTreeLabels(Tree<S> tree, LabelFactory<S, T> labelFactory) {
   T newLabel = labelFactory.newLabel(tree);
   List<Tree<T>> newChildren = new ArrayList<Tree<T>>();
   for (Tree<S> node : tree.getChildren()) {
     Tree<T> newNode = transformTreeLabels(node, labelFactory);
     newChildren.add(newNode);
   }
   return new Tree<T>(newLabel, newChildren);
 }
Пример #16
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(')');
 }