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
,java.lang.Comparable<SpanNode>
public class AlternateSpanList extends SpanList
A node in aSpanNode
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:
SpanList
-
-
Field Summary
Fields Modifier and Type Field Description static byte
ID
-
Constructor Summary
Constructors Constructor Description AlternateSpanList()
Create a new AlternateSpanList instance, having a single subtree with probability 1.0.AlternateSpanList(AlternateSpanList otherSpanList)
AlternateSpanList(SpanNodeReader reader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AlternateSpanList
add(int i, SpanNode node)
Convenience method to add a span node to the child tree at index i.AlternateSpanList
add(SpanNode node)
Adds a child node to the first subtree of this AlternateSpanList.void
addChildren(int i, java.util.List<SpanNode> subtree, double probability)
Adds a possible subtree of this AlternateSpanList, with the given probability, at index i.boolean
addChildren(java.util.List<SpanNode> subtree, double probability)
Adds a possible subtree of this AlternateSpanList, with the given probability.java.util.ListIterator<SpanNode>
childIterator()
Traverses all immediate children of all subtrees of this AlternateSpanList.java.util.ListIterator<SpanNode>
childIterator(int i)
Traverses all immediate children of the given subtree of this AlternateSpanList.java.util.ListIterator<SpanNode>
childIteratorRecursive()
Recursively traverses all children (not only leaf nodes) of all subtrees of this AlternateSpanList, in a depth-first fashion.java.util.ListIterator<SpanNode>
childIteratorRecursive(int i)
Recursively traverses all children (not only leaf nodes) of the given subtree of this AlternateSpanList, in a depth-first fashion.protected java.util.List<SpanNode>
children()
Returns a modifiableList
of child nodes of first subtree.protected java.util.List<SpanNode>
children(int i)
Returns a modifiableList
of child nodes of the specified subtree.void
clearChildren()
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
equals(java.lang.Object o)
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
getNumSubTrees()
Returns the number of subtrees under this node.double
getProbability(int i)
Returns the probability of the given subtree.java.lang.CharSequence
getText(int i, java.lang.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
move(int i, int nodeNum, SpanList target)
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
move(int i, SpanNode node, SpanList target)
Moves a child of this SpanList to another SpanList.void
normalizeProbabilities()
Normalizes all probabilities between 0.0 (inclusive) and 1.0 (exclusive).int
numChildren(int i)
void
removeChildren()
Removes all subtrees (both the subtrees themselves and their contents, which become invalidated).java.util.List<SpanNode>
removeChildren(int i)
Removes the subtree at index i (both the subtree itself and its contents, which become invalidated).java.util.List<SpanNode>
setChildren(int i, java.util.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
sortChildren()
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
sortChildrenRecursive()
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
sortSubTreesByProbability()
Sorts the subtrees under this AlternateSpanList by descending probability, such that the most probable subtree becomes the first subtree, and so on.java.lang.String
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 Detail
-
ID
public static final byte ID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AlternateSpanList
public AlternateSpanList()
Create a new AlternateSpanList instance, having a single subtree with probability 1.0.
-
AlternateSpanList
public AlternateSpanList(AlternateSpanList otherSpanList)
-
AlternateSpanList
public AlternateSpanList(SpanNodeReader reader)
-
-
Method Detail
-
add
public AlternateSpanList add(SpanNode node)
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
protected java.util.List<SpanNode> 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:
SpanNode.compareTo(SpanNode)
-
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:
SpanNode.compareTo(SpanNode)
-
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
public void move(int i, SpanNode node, SpanList target)
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:
java.lang.IllegalArgumentException
- if the given node is not a child of this SpanList
-
move
public void move(int i, int nodeNum, SpanList target)
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:
java.lang.IndexOutOfBoundsException
- if the given index is out of range
-
move
public void move(int i, SpanNode node, AlternateSpanList target, int targetSubTree)
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:
java.lang.IllegalArgumentException
- if the given node is not a child of this SpanListjava.lang.IndexOutOfBoundsException
- if the given index is out of range, or if the target subtree index is out of range
-
move
public void move(int i, int nodeNum, AlternateSpanList target, int targetSubTree)
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:
java.lang.IndexOutOfBoundsException
- if any of the given indeces are out of range, or the target subtree index is out of range
-
childIterator
public java.util.ListIterator<SpanNode> 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:
ListIterator
-
childIteratorRecursive
public java.util.ListIterator<SpanNode> 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:
ListIterator
-
childIterator
public java.util.ListIterator<SpanNode> childIterator(int i)
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:
ListIterator
-
childIteratorRecursive
public java.util.ListIterator<SpanNode> childIteratorRecursive(int i)
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:
ListIterator
-
numChildren
public int numChildren(int i)
-
children
protected java.util.List<SpanNode> children(int i)
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
public boolean addChildren(java.util.List<SpanNode> subtree, double probability)
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:
children()
-
addChildren
public void addChildren(int i, java.util.List<SpanNode> subtree, double probability)
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:
children()
-
removeChildren
public java.util.List<SpanNode> removeChildren(int i)
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
public java.util.List<SpanNode> setChildren(int i, java.util.List<SpanNode> subtree, double probability)
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:
java.lang.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:
java.lang.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
public java.lang.CharSequence getText(int i, java.lang.CharSequence text)
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
public AlternateSpanList add(int i, SpanNode node)
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
-
-