/** * 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); } }
/** * Convenience method to add a span node to the child tree at index i. This is equivalent to calling * <code> * AlternateSpanList.children(i).add(node); * </code> * * @param i index * @param node span node */ public AlternateSpanList add(int i, SpanNode node) { checkValidity(node, children(i)); node.setParent(this); children(i).add(node); return this; }
public void write(AlternateSpanList altSpanList) { buf.put(AlternateSpanList.ID); buf.putInt1_2_4Bytes(altSpanList.getNumSubTrees()); for (int i = 0; i < altSpanList.getNumSubTrees(); i++) { buf.putDouble(altSpanList.getProbability(i)); buf.putInt1_2_4Bytes(altSpanList.numChildren(i)); Iterator<SpanNode> children = altSpanList.childIterator(i); while (children.hasNext()) { write(children.next()); } } }
/** * 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; }
public void read(AlternateSpanList altSpanList) { byte type = buf.get(); if ((type & AlternateSpanList.ID) != AlternateSpanList.ID) { throw new DeserializationException("Cannot deserialize AlternateSpanList with type " + type); } int numSubTrees = buf.getInt1_2_4Bytes(); for (int i = 0; i < numSubTrees; i++) { double prob = buf.getDouble(); List<SpanNode> list = readSpanList(altSpanList); if (i == 0) { for (SpanNode node : list) { altSpanList.add(node); } altSpanList.setProbability(0, prob); } else { altSpanList.addChildren(i, list, prob); } } }
/** * 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); }
/** * 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 * @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 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 the given index is out of range, or the target subtree index is out of range */ public void move(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().remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } }
private SpanNode copySpan(SpanNode spanTree) { if (spanTree instanceof Span) { return new Span((Span) spanTree); } else if (spanTree instanceof AlternateSpanList) { return new AlternateSpanList((AlternateSpanList) spanTree); } else if (spanTree instanceof SpanList) { return new SpanList((SpanList) spanTree); } else if (spanTree instanceof DummySpanNode) { return spanTree; //shouldn't really happen } else { throw new IllegalStateException("Cannot create copy of " + spanTree + " with class " + ((spanTree == null) ? "null" : spanTree.getClass())); } }
/** * Returns a modifiable {@link List} of child nodes of <strong>first</strong> subtree. * * @return a modifiable {@link List} of child nodes of <strong>first</strong> subtree */ @Override protected List<SpanNode> children() { return children(0); }
/** Create a new AlternateSpanList instance, having a single subtree with probability 1.0. */ public AlternateSpanList() { super((List<SpanNode>) null); ensureAtLeastOneSubTree(); }
/** * Adds a child node to the <strong>first</strong> subtree of this AlternateSpanList. Note * that it might be a good idea to call {@link #sortSubTreesByProbability()} first. * * @param node the node to add. * @return this, for call chaining */ @Override public AlternateSpanList add(SpanNode node) { return add(0, node); }
@Override public String toString() { return "AlternateSpanList, num subtrees=" + getNumSubTrees(); }
private void ensureAtLeastOneSubTree() { if (childTrees.isEmpty()) { childTrees.add(new Children(getParent())); } }
public void read(AlternateSpanList altSpanList) { byte type = buf.get(); if ((type & AlternateSpanList.ID) != AlternateSpanList.ID) { throw new DeserializationException("Cannot deserialize AlternateSpanList with type " + type); } int numSubTrees = buf.getInt1_2_4Bytes(); for (int i = 0; i < numSubTrees; i++) { double prob = buf.getDouble(); List<SpanNode> list = readSpanList(altSpanList); if (i == 0) { for (SpanNode node : list) { altSpanList.add(node); } altSpanList.setProbability(0, prob); } else { altSpanList.addChildren(i, list, prob); } } }
/** * 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); }
/** * 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; }
/** * 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."); } }