예제 #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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 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
파일: Trees.java 프로젝트: elendil326/UWNLP
 /** 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(')');
 }