/** * Do a breadth first traversal over the tree. * <p> * This does a standard queue based implemenation of the traversal * * @return An iterable object over the subtrees in breadth first order */ public static <T> List<Tree<T>> breadthFirstTraversal(Tree<T> tree) { Queue<Tree<T>> nodeQueue = new LinkedList<>(); nodeQueue.offer(tree); ArrayList<Tree<T>> bfs = new ArrayList<>(); while (!nodeQueue.isEmpty()) { Tree<T> t = nodeQueue.remove(); bfs.add(t); for (Tree<T> child : t.childrenIterator()) { nodeQueue.offer(child); } } return bfs; }
/** * Do a breadth first traversal over the tree. * <p> * This does a standard queue based implemenation of the traversal * * @return An iterable object over the subtrees in breadth first order */ public static <T> List<Tree<T>> breadthFirstTraversal(Tree<T> tree) { Queue<Tree<T>> nodeQueue = new LinkedList<>(); nodeQueue.offer(tree); ArrayList<Tree<T>> bfs = new ArrayList<>(); while (!nodeQueue.isEmpty()) { Tree<T> t = nodeQueue.remove(); bfs.add(t); for (Tree<T> child : t.childrenIterator()) { nodeQueue.offer(child); } } return bfs; }
/** * Get the nodes of the tree in depth first order. * * @return Iterator over subtrees in dfs */ public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); // We want to do a left to right traversal of the children. Adding // the children to the stack just gives a left to right ordering. To fix it, // there is a temporary stack. Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }
/** * Get the nodes of the tree in depth first order. * * @return Iterator over subtrees in dfs */ public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); // We want to do a left to right traversal of the children. Adding // the children to the stack just gives a left to right ordering. To fix it, // there is a temporary stack. Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }
/** * Assuming that the tree comes with lexical items and POS tags, the subcat frame for the verb * can be found by going to the parent of the POS tag (which is probably a VP) and listing its * children. */ public static String getSubcatFrame(Tree<String> yieldNode) { StringBuilder sb = new StringBuilder(); Tree<String> node = yieldNode.getParent().getParent(); for (Tree<String> t : node.childrenIterator()) { sb.append(t.getLabel()).append(" "); } return sb.toString(); }
/** * Assuming that the tree comes with lexical items and POS tags, the subcat frame for the verb * can be found by going to the parent of the POS tag (which is probably a VP) and listing its * children. */ public static String getSubcatFrame(Tree<String> yieldNode) { StringBuilder sb = new StringBuilder(); Tree<String> node = yieldNode.getParent().getParent(); for (Tree<String> t : node.childrenIterator()) { sb.append(t.getLabel()).append(" "); } return sb.toString(); }
/** * Assuming that the tree comes with lexical items and POS tags, the subcat frame for the verb * can be found by going to the parent of the POS tag (which is probably a VP) and listing its * children. */ public static String getSubcatFrame(Tree<String> yieldNode) { StringBuilder sb = new StringBuilder(); Tree<String> node = yieldNode.getParent().getParent(); for (Tree<String> t : node.childrenIterator()) { sb.append(t.getLabel()).append(" "); } return sb.toString(); }
/** * Assuming that the tree comes with lexical items and POS tags, the subcat frame for the verb * can be found by going to the parent of the POS tag (which is probably a VP) and listing its * children. */ public static String getSubcatFrame(Tree<String> yieldNode) { StringBuilder sb = new StringBuilder(); Tree<String> node = yieldNode.getParent().getParent(); for (Tree<String> t : node.childrenIterator()) { sb.append(t.getLabel()).append(" "); } return sb.toString(); }
/** * Get the labels of all siblings of a given tree node. * * @param node The node whose siblings are required. * @return The labels of the siblings of the input node, as an array of strings. If the input is * the root of the tree, then a string array of size zero is returned to indicate that * the corresponding parse tree node does not have any siblings. */ public static String[] getAllSiblingLabels(Tree<String> node) { List<String> siblings = new ArrayList<>(); if (node.isRoot()) return new String[0]; Tree<String> parent = node.getParent(); for (Tree<String> child : parent.childrenIterator()) { siblings.add(child.getLabel()); } return siblings.toArray(new String[siblings.size()]); }
/** * Get the labels of all siblings of a given tree node. * * @param node The node whose siblings are required. * @return The labels of the siblings of the input node, as an array of strings. If the input is * the root of the tree, then a string array of size zero is returned to indicate that * the corresponding parse tree node does not have any siblings. */ public static String[] getAllSiblingLabels(Tree<String> node) { List<String> siblings = new ArrayList<>(); if (node.isRoot()) return new String[0]; Tree<String> parent = node.getParent(); for (Tree<String> child : parent.childrenIterator()) { siblings.add(child.getLabel()); } return siblings.toArray(new String[siblings.size()]); }
/** * Get all the nodes of the tree in depth first fashion, limited to a given depth. * <p> * The depth computation is a little bit ugly, because the tree gives height instead of depths. */ public static <T> List<Tree<T>> depthLimitedTraversal(Tree<T> tree, int depth) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); int treeHeight = tree.getHeight(); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); // We want to do a left to right traversal of the children. Adding // the children to the stack just gives a left to right ordering. To fix it, // there is a temporary stack. Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { Tree<T> tmpTree = tmpStack.pop(); if (treeHeight - tmpTree.getHeight() < depth) treeStack.add(tmpStack.pop()); } } return dfs; }
/** * Traverse the tree counting the terminal nodes as we go along, adding the index as the key, trhe * tree node as the value to a hashmap. * @param tree the tree we are looking at. * @param data the map we are populating. * @param offset the current index. * @return the index of the next terminal node. */ private int traverseTree(Tree<Constituent> tree, HashMap<Integer, Tree<Constituent>> data, int offset) { if (tree.isLeaf()) { data.put(offset++, tree); if (tree.getNumberOfChildren() > 0) throw new RuntimeException("There is a leaf with kids???"); } else { for (Iterator<Tree<Constituent>> iterator = tree.childrenIterator().iterator() ; iterator.hasNext(); ) { offset = traverseTree(iterator.next(), data, offset); } } return offset; }
/** * Get all the nodes of the tree in depth first fashion, limited to a given depth. * <p> * The depth computation is a little bit ugly, because the tree gives height instead of depths. */ public static <T> List<Tree<T>> depthLimitedTraversal(Tree<T> tree, int depth) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); int treeHeight = tree.getHeight(); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); // We want to do a left to right traversal of the children. Adding // the children to the stack just gives a left to right ordering. To fix it, // there is a temporary stack. Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { Tree<T> tmpTree = tmpStack.pop(); if (treeHeight - tmpTree.getHeight() < depth) treeStack.add(tmpStack.pop()); } } return dfs; }
/** * Get the labels of all siblings of a given tree node. * * @param node The node whose siblings are required. * @return The labels of the siblings of the input node, as an array of strings. If the input is * the root of the tree, then a string array of size zero is returned to indicate that * the corresponding parse tree node does not have any siblings. */ public static String[] getAllSiblingLabels(Tree<String> node) { List<String> siblings = new ArrayList<>(); if (node.isRoot()) return new String[0]; Tree<String> parent = node.getParent(); for (Tree<String> child : parent.childrenIterator()) { siblings.add(child.getLabel()); } return siblings.toArray(new String[siblings.size()]); }
/** * Traverse the tree counting the terminal nodes as we go along, adding the index as the key, trhe * tree node as the value to a hashmap. * @param tree the tree we are looking at. * @param data the map we are populating. * @param offset the current index. * @return the index of the next terminal node. */ private int traverseTree(Tree<Constituent> tree, HashMap<Integer, Tree<Constituent>> data, int offset) { if (tree.isLeaf()) { data.put(offset++, tree); if (tree.getNumberOfChildren() > 0) throw new RuntimeException("There is a leaf with kids???"); } else { for (Iterator<Tree<Constituent>> iterator = tree.childrenIterator().iterator() ; iterator.hasNext(); ) { offset = traverseTree(iterator.next(), data, offset); } } return offset; }
/** * Get the labels of all siblings of a given tree node. * * @param node The node whose siblings are required. * @return The labels of the siblings of the input node, as an array of strings. If the input is * the root of the tree, then a string array of size zero is returned to indicate that * the corresponding parse tree node does not have any siblings. */ public static String[] getAllSiblingLabels(Tree<String> node) { List<String> siblings = new ArrayList<>(); if (node.isRoot()) return new String[0]; Tree<String> parent = node.getParent(); for (Tree<String> child : parent.childrenIterator()) { siblings.add(child.getLabel()); } return siblings.toArray(new String[siblings.size()]); }
boolean onlyNP = true; int numNP = 0; for(Tree<String> child : subtree.childrenIterator()){ if(child.getLabel().equals("CC")){ hasCC = true; int minNPSize = Integer.MAX_VALUE; Tree<String> minNP = null; for(Tree<String> child : subtree.childrenIterator()){ if(child.getLabel().equals("NP")){ int NPSize = 0;