Пример #1
0
 private static <L> void appendYield(Tree<L> tree, List<L> yield) {
   if (tree.isLeaf()) {
     yield.add(tree.getLabel());
     return;
   }
   for (Tree<L> child : tree.getChildren()) {
     appendYield(child, yield);
   }
 }
Пример #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);
 }
Пример #3
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;
 }
Пример #4
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;
 }
Пример #5
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);
 }
Пример #6
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(')');
 }
Пример #7
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);
 }
Пример #8
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);
 }
Пример #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(')');
 }