コード例 #1
0
ファイル: Tree.java プロジェクト: jogonzal/UWNLPAssignment3
 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
ファイル: Tree.java プロジェクト: jogonzal/UWNLPAssignment3
 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(')');
 }