Package com.yahoo.document.annotation
Class AlternateSpanList
java.lang.Object
com.yahoo.document.annotation.SpanNode
com.yahoo.document.annotation.SpanList
com.yahoo.document.annotation.AlternateSpanList
- All Implemented Interfaces:
SpanNodeParent
,Comparable<SpanNode>
A node in a
SpanNode
tree that can have a multiple trees of child nodes, each with its own probability.
This class has quite a few convenience methods for accessing the first subtree.- Author:
- Einar M R Rosenvinge
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a new AlternateSpanList instance, having a single subtree with probability 1.0.AlternateSpanList
(AlternateSpanList otherSpanList) Deep-copies another AlternateSpanList.AlternateSpanList
(SpanNodeReader reader) -
Method Summary
Modifier and TypeMethodDescriptionConvenience method to add a span node to the child tree at index i.Adds a child node to the first subtree of this AlternateSpanList.void
addChildren
(int i, List<SpanNode> subtree, double probability) Adds a possible subtree of this AlternateSpanList, with the given probability, at index i.boolean
addChildren
(List<SpanNode> subtree, double probability) Adds a possible subtree of this AlternateSpanList, with the given probability.Traverses all immediate children of all subtrees of this AlternateSpanList.childIterator
(int i) Traverses all immediate children of the given subtree of this AlternateSpanList.Recursively traverses all children (not only leaf nodes) of all subtrees of this AlternateSpanList, in a depth-first fashion.childIteratorRecursive
(int i) Recursively traverses all children (not only leaf nodes) of the given subtree of this AlternateSpanList, in a depth-first fashion.children()
Returns a modifiableList
of child nodes of first subtree.children
(int i) Returns a modifiableList
of child nodes of the specified subtree.void
Clears all subtrees (the subtrees themselves are kept, but their contents are cleared and become invalidated).void
clearChildren
(int i) Clears a given subtree (the subtree itself is kept, but its contents are cleared and become invalidated).boolean
int
getFrom
(int i) Returns the character index where thisSpanNode
starts (inclusive), i.e. the smallestSpanNode.getFrom()
of all children in subtree i.int
getLength
(int i) Returns the length of this span according to subtree i, i.e. getFrom(i) - getTo(i).int
Returns the number of subtrees under this node.double
getProbability
(int i) Returns the probability of the given subtree.getText
(int i, CharSequence text) Returns the text covered by this span as given by subtree i, or null if subtree i is empty.int
getTo
(int i) Returns the character index where thisSpanNode
ends (exclusive), i.e. the greatestSpanNode.getTo()
of all children in subtree i.int
hashCode()
void
move
(int i, int nodeNum, AlternateSpanList target, int targetSubTree) Moves a child of this SpanList to another SpanList.void
Moves a child of this SpanList to another SpanList.void
move
(int i, SpanNode node, AlternateSpanList target, int targetSubTree) Moves a child of this SpanList to another SpanList.void
Moves a child of this SpanList to another SpanList.void
Normalizes all probabilities between 0.0 (inclusive) and 1.0 (exclusive).int
numChildren
(int i) void
Removes all subtrees (both the subtrees themselves and their contents, which become invalidated).removeChildren
(int i) Removes the subtree at index i (both the subtree itself and its contents, which become invalidated).setChildren
(int i, List<SpanNode> subtree, double probability) Sets the subtree at index i.void
setProbability
(int i, double probability) Sets the probability of the given subtree.void
Sorts children in all subtrees by occurrence in the text covered.void
sortChildren
(int i) Sorts children in subtree i by occurrence in the text covered.void
Recursively sorts all children in all subtrees by occurrence in the text covered.void
sortChildrenRecursive
(int i) Recursively sorts all children in subtree i by occurrence in the text covered.void
Sorts the subtrees under this AlternateSpanList by descending probability, such that the most probable subtree becomes the first subtree, and so on.toString()
Methods inherited from class com.yahoo.document.annotation.SpanList
getFrom, getLength, getText, getTo, isLeafNode, move, move, move, move, numChildren, remove, remove, span
Methods inherited from class com.yahoo.document.annotation.SpanNode
annotate, annotate, annotate, annotate, annotate, compareTo, contains, getParent, getScratchId, getSpanTree, getStringFieldValue, isValid, overlaps, setScratchId
-
Field Details
-
ID
public static final byte ID- See Also:
-
-
Constructor Details
-
AlternateSpanList
public AlternateSpanList()Create a new AlternateSpanList instance, having a single subtree with probability 1.0. -
AlternateSpanList
Deep-copies another AlternateSpanList.- Parameters:
otherSpanList
- the instance to deep-copy.
-
AlternateSpanList
-
-
Method Details
-
add
Adds a child node to the first subtree of this AlternateSpanList. Note that it might be a good idea to callsortSubTreesByProbability()
first. -
sortSubTreesByProbability
public void sortSubTreesByProbability()Sorts the subtrees under this AlternateSpanList by descending probability, such that the most probable subtree becomes the first subtree, and so on. -
children
Returns a modifiableList
of child nodes of first subtree. -
getNumSubTrees
public int getNumSubTrees()Returns the number of subtrees under this node.- Returns:
- the number of subtrees under this node.
-
clearChildren
public void clearChildren()Clears all subtrees (the subtrees themselves are kept, but their contents are cleared and become invalidated).- Overrides:
clearChildren
in classSpanList
-
clearChildren
public void clearChildren(int i) Clears a given subtree (the subtree itself is kept, but its contents are cleared and become invalidated).- Parameters:
i
- the index of the subtree to clear
-
sortChildren
public void sortChildren()Sorts children in all subtrees by occurrence in the text covered.- Overrides:
sortChildren
in classSpanList
- See Also:
-
sortChildren
public void sortChildren(int i) Sorts children in subtree i by occurrence in the text covered.- Parameters:
i
- the index of the subtree to sort- See Also:
-
sortChildrenRecursive
public void sortChildrenRecursive()Recursively sorts all children in all subtrees by occurrence in the text covered.- Overrides:
sortChildrenRecursive
in classSpanList
-
sortChildrenRecursive
public void sortChildrenRecursive(int i) Recursively sorts all children in subtree i by occurrence in the text covered.- Parameters:
i
- the index of the subtree to sort recursively
-
move
Moves a child of this SpanList to another SpanList.- Parameters:
i
- the index of the subtree to remove the node fromnode
- the node to movetarget
- the SpanList to add the node to- Throws:
IllegalArgumentException
- if the given node is not a child of this SpanList
-
move
Moves a child of this SpanList to another SpanList.- Parameters:
i
- the index of the subtree to remove the node fromnodeNum
- the index of the node to movetarget
- the SpanList to add the node to- Throws:
IndexOutOfBoundsException
- if the given index is out of range
-
move
Moves a child of this SpanList to another SpanList.- Parameters:
i
- the index of the subtree to remove the node fromnode
- the node to movetarget
- the SpanList to add the node totargetSubTree
- 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 SpanListIndexOutOfBoundsException
- if the given index is out of range, or if the target subtree index is out of range
-
move
Moves a child of this SpanList to another SpanList.- Parameters:
i
- the index of the subtree to remove the node fromnodeNum
- the index of the node to movetarget
- the SpanList to add the node totargetSubTree
- 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
-
childIterator
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.- Overrides:
childIterator
in classSpanList
- Returns:
- a ListIterator which traverses all immediate children of this SpanNode
- See Also:
-
childIteratorRecursive
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.- Overrides:
childIteratorRecursive
in classSpanList
- Returns:
- a ListIterator which recursively traverses all children and their children etc. of all subtrees of this AlternateSpanList
- See Also:
-
childIterator
Traverses all immediate children of the given subtree of this AlternateSpanList. The ListIterator returned supports all optional operations specified in the ListIterator interface.- Parameters:
i
- the index of the subtree to iterate over- Returns:
- a ListIterator which traverses all immediate children of this SpanNode
- See Also:
-
childIteratorRecursive
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.- Parameters:
i
- the index of the subtree to iterate over- Returns:
- a ListIterator which recursively traverses all children and their children etc. of the given subtree of this AlternateSpanList.
- See Also:
-
numChildren
public int numChildren(int i) -
children
Returns a modifiableList
of child nodes of the specified subtree.- Parameters:
i
- the index of the subtree to search- Returns:
- a modifiable
List
of child nodes of the specified subtree
-
addChildren
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.- Parameters:
subtree
- the subtree to addprobability
- the probability of this subtree- Returns:
- true if successful
- See Also:
-
addChildren
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.- Parameters:
i
- the index of where to insert the subtreesubtree
- the subtree to addprobability
- the probability of this subtree- See Also:
-
removeChildren
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.- Parameters:
i
- the index of the subtree to remove- Returns:
- the subtree removed, if any (note: invalidated)
-
removeChildren
public void removeChildren()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. -
setChildren
Sets the subtree at index i.- Parameters:
i
- the index of where to set the subtreesubtree
- the subtree to setprobability
- the probability to set- Returns:
- the overwritten subtree, if any
-
getFrom
public int getFrom(int i) Returns the character index where thisSpanNode
starts (inclusive), i.e. the smallestSpanNode.getFrom()
of all children in subtree i.- Parameters:
i
- the index of the subtree to use- Returns:
- 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
-
getTo
public int getTo(int i) Returns the character index where thisSpanNode
ends (exclusive), i.e. the greatestSpanNode.getTo()
of all children in subtree i.- Parameters:
i
- the index of the subtree to use- Returns:
- 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
-
getLength
public int getLength(int i) Returns the length of this span according to subtree i, i.e. getFrom(i) - getTo(i).- Parameters:
i
- the index of the subtree to use- Returns:
- the length of this span according to subtree i
-
getText
Returns the text covered by this span as given by subtree i, or null if subtree i is empty.- Parameters:
i
- the index of the subtree to usetext
- the text to get a substring from- Returns:
- the text covered by this span as given by subtree i, or null if subtree i is empty
-
getProbability
public double getProbability(int i) Returns the probability of the given subtree.- Parameters:
i
- the subtree to return the probability of- Returns:
- the probability of the given subtree
-
setProbability
public void setProbability(int i, double probability) Sets the probability of the given subtree.- Parameters:
i
- the subtree to set the probability ofprobability
- the probability to set
-
normalizeProbabilities
public void normalizeProbabilities()Normalizes all probabilities between 0.0 (inclusive) and 1.0 (exclusive). -
add
Convenience method to add a span node to the child tree at index i. This is equivalent to callingAlternateSpanList.children(i).add(node);
- Parameters:
i
- indexnode
- span node
-
equals
-
hashCode
public int hashCode() -
toString
-