/** * Sorts the subtrees under this AlternateSpanList by descending probability, such that the most probable * subtree becomes the first subtree, and so on. */ public void sortSubTreesByProbability() { resetCachedFromAndTo(); Collections.sort(childTrees, childComparator); }
/** * Adds a possible subtree of this AlternateSpanList, with the given probability, at index i. Note that the first subtree is * always available through the use of children(), so this method is only used for adding the second or higher * subtree. * * @param i the index of where to insert the subtree * @param subtree the subtree to add * @param probability the probability of this subtree * @see #children() */ public void addChildren(int i, List<SpanNode> subtree, double probability) { Children childTree = new Children(getParent(), subtree, probability); resetCachedFromAndTo(); childTrees.add(i, childTree); }
/** * Adds a possible subtree of this AlternateSpanList, with the given probability. Note that the first subtree is * always available through the use of children(), so this method is only used for adding the second or higher * subtree. * * @param subtree the subtree to add * @param probability the probability of this subtree * @return true if successful * @see #children() */ public boolean addChildren(List<SpanNode> subtree, double probability) { Children childTree = new Children(getParent(), subtree, probability); resetCachedFromAndTo(); return childTrees.add(childTree); }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param node the node to move * @param target the SpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList */ public void move(int i, SpanNode node, SpanList target) { boolean removed = children(i).remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @throws IndexOutOfBoundsException if the given index is out of range */ public void move(int i, int nodeNum, SpanList target) { SpanNode node = children(i).remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param node the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList * @throws IndexOutOfBoundsException if the given index is out of range, or if the target subtree index is out of range */ public void move(int i, SpanNode node, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } boolean removed = children(i).remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Removes all subtrees (both the subtrees themselves and their contents, which become invalidated). * Note that a new empty subtree is automatically added at index 0, since an AlternateSpanList always has at * least one subtree. */ public void removeChildren() { for (Children ch : childTrees) { ch.setInvalid(); ch.setParent(null); ch.clearChildren(); } childTrees.clear(); resetCachedFromAndTo(); ensureAtLeastOneSubTree(); }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IndexOutOfBoundsException if any of the given indeces are out of range, or the target subtree index is out of range */ public void move(int i, int nodeNum, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } SpanNode node = children(i).remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } }
/** * Removes the subtree at index i (both the subtree itself and its contents, which become invalidated). * Note that if this AlternateSpanList has only one subtree and index 0 is given, * a new empty subtree is automatically added, since an AlternateSpanList always has at least one subtree. * * @param i the index of the subtree to remove * @return the subtree removed, if any (note: invalidated) */ public List<SpanNode> removeChildren(int i) { Children retval = childTrees.remove(i); ensureAtLeastOneSubTree(); resetCachedFromAndTo(); if (retval != null) { retval.setInvalid(); retval.setParent(null); for (SpanNode node : retval.children()) { node.setParent(null); } return retval.children(); } return null; }
/** * Sets the subtree at index i. * * @param i the index of where to set the subtree * @param subtree the subtree to set * @param probability the probability to set * @return the overwritten subtree, if any */ public List<SpanNode> setChildren(int i, List<SpanNode> subtree, double probability) { resetCachedFromAndTo(); if (childTrees.size() == 1 && i == 0) { //replace the first subtree Children sub = new Children(getParent(), subtree, probability); Children retval = childTrees.set(i, sub); if (retval == null) { return null; } else { retval.setParent(null); for (SpanNode node : retval.children()) { node.setParent(null); } return retval.children(); } } List<SpanNode> retval = removeChildren(i); addChildren(i, subtree, probability); return retval; }