private void ensureAtLeastOneSubTree() { if (childTrees.isEmpty()) { childTrees.add(new Children(getParent())); } }
/** * Traverses all immediate children of all subtrees of this AlternateSpanList. * The ListIterator only supports iteration forwards, and the optional operations that are implemented are * remove() and set(). add() is not supported. * * @return a ListIterator which traverses all immediate children of this SpanNode * @see java.util.ListIterator */ @Override public ListIterator<SpanNode> childIterator() { List<ListIterator<SpanNode>> childIterators = new ArrayList<ListIterator<SpanNode>>(); for (Children ch : childTrees) { childIterators.add(ch.childIterator()); } return new SerialIterator(childIterators); }
/** * Recursively traverses all children (not only leaf nodes) of all subtrees of this AlternateSpanList, in a * depth-first fashion. * The ListIterator only supports iteration forwards, and the optional operations that are implemented are * remove() and set(). add() is not supported. * * @return a ListIterator which recursively traverses all children and their children etc. of all subtrees of this AlternateSpanList * @see java.util.ListIterator */ @Override public ListIterator<SpanNode> childIteratorRecursive() { List<ListIterator<SpanNode>> childIterators = new ArrayList<ListIterator<SpanNode>>(); for (Children ch : childTrees) { childIterators.add(ch.childIteratorRecursive()); } return new SerialIterator(childIterators); }
/** * 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 AlternateSpanList(AlternateSpanList otherSpanList) { super((List<SpanNode>) null); for (Children otherSubtree : otherSpanList.childTrees) { //create our own subtree: Children children = new Children(this); //copy nodes: for (SpanNode otherNode : otherSubtree.children()) { if (otherNode instanceof Span) { children.add(new Span((Span) otherNode)); } else if (otherNode instanceof AlternateSpanList) { children.add(new AlternateSpanList((AlternateSpanList) otherNode)); } else if (otherNode instanceof SpanList) { children.add(new SpanList((SpanList) otherNode)); } else if (otherNode instanceof DummySpanNode) { children.add(otherNode); //shouldn't really happen } else { throw new IllegalStateException("Cannot create copy of " + otherNode + " with class " + ((otherNode == null) ? "null" : otherNode.getClass())); } } //add this subtree to our subtrees: childTrees.add(children); } }
/** * 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; }
/** * 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 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(); }
/** * Recursively sorts all children in subtree i by occurrence in the text covered. * * @param i the index of the subtree to sort recursively */ public void sortChildrenRecursive(int i) { Children children = childTrees.get(i); for (SpanNode node : children.children()) { if (node instanceof SpanList) { ((SpanList) node).sortChildrenRecursive(); } } Collections.sort(children.children()); }
/** * Recursively traverses all children (not only leaf nodes) of the given subtree of this AlternateSpanList, in a * depth-first fashion. * The ListIterator only supports iteration forwards, and the optional operations that are implemented are * remove() and set(). add() is not supported. * * @param i the index of the subtree to iterate over * @return a ListIterator which recursively traverses all children and their children etc. of the given subtree of this AlternateSpanList. * @see java.util.ListIterator */ public ListIterator<SpanNode> childIteratorRecursive(int i) { return childTrees.get(i).childIteratorRecursive(); }
/** * 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); }
/** * Recursively sorts all children in <strong>all</strong> subtrees by occurrence in the text covered. */ public void sortChildrenRecursive() { for (Children children : childTrees) { for (SpanNode node : children.children()) { if (node instanceof SpanList) { ((SpanList) node).sortChildrenRecursive(); } } Collections.sort(children.children()); } }
/** * Returns the probability of the given subtree. * * @param i the subtree to return the probability of * @return the probability of the given subtree */ public double getProbability(int i) { return childTrees.get(i).getProbability(); }
/** * Returns the character index where this {@link SpanNode} ends (exclusive), i.e. the greatest {@link com.yahoo.document.annotation.SpanNode#getTo()} of all children in subtree i. * * @param i the index of the subtree to use * @return the greatest getTo() of all children, or -1 if this SpanList has no children in subtree i. * @throws IndexOutOfBoundsException if this AlternateSpanList has no subtree i */ public int getTo(int i) { return childTrees.get(i).getTo(); }
/** * Sorts children in <strong>all</strong> subtrees by occurrence in the text covered. * * @see SpanNode#compareTo(SpanNode) */ @Override public void sortChildren() { for (Children children : childTrees) { Collections.sort(children.children()); } }
/** * Clears a given subtree (the subtree itself is kept, but its contents are cleared and become invalidated). * * @param i the index of the subtree to clear */ public void clearChildren(int i) { Children c = childTrees.get(i); if (c != null) { c.clearChildren(); } }
/** Clears all subtrees (the subtrees themselves are kept, but their contents are cleared and become invalidated). */ @Override public void clearChildren() { for (Children c : childTrees) { c.clearChildren(); } }
/** * Returns the character index where this {@link SpanNode} starts (inclusive), i.e. the smallest {@link com.yahoo.document.annotation.SpanNode#getFrom()} of all children in subtree i. * * @param i the index of the subtree to use * @return the lowest getFrom() of all children in subtree i, or -1 if this SpanList has no children in subtree i. * @throws IndexOutOfBoundsException if this AlternateSpanList has no subtree i */ public int getFrom(int i) { return childTrees.get(i).getFrom(); }
/** * Traverses all immediate children of the given subtree of this AlternateSpanList. * The ListIterator returned supports all optional operations * specified in the ListIterator interface. * * @param i the index of the subtree to iterate over * @return a ListIterator which traverses all immediate children of this SpanNode * @see java.util.ListIterator */ public ListIterator<SpanNode> childIterator(int i) { return childTrees.get(i).childIterator(); }
/** Normalizes all probabilities between 0.0 (inclusive) and 1.0 (exclusive). */ public void normalizeProbabilities() { double sum = 0.0; for (Children c : childTrees) { sum += c.getProbability(); } double coeff = 1.0 / sum; for (Children childTree : childTrees) { double newProb = childTree.getProbability() * coeff; childTree.setProbability(newProb); } }