Class AlternateSpanList

All Implemented Interfaces:
SpanNodeParent, Comparable<SpanNode>

public class AlternateSpanList extends SpanList
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 Details

  • Constructor Details

    • AlternateSpanList

      public AlternateSpanList()
      Create a new AlternateSpanList instance, having a single subtree with probability 1.0.
    • AlternateSpanList

      public AlternateSpanList(AlternateSpanList otherSpanList)
      Deep-copies another AlternateSpanList.
      Parameters:
      otherSpanList - the instance to deep-copy.
    • AlternateSpanList

      public AlternateSpanList(SpanNodeReader reader)
  • Method Details

    • 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 call sortSubTreesByProbability() first.
      Overrides:
      add in class SpanList
      Parameters:
      node - the node to add.
      Returns:
      this, for call chaining
    • 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 List<SpanNode> children()
      Returns a modifiable List of child nodes of first subtree.
      Overrides:
      children in class SpanList
      Returns:
      a modifiable List 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 class SpanList
    • 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 class SpanList
      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 class SpanList
    • 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 from
      node - the node to move
      target - the SpanList to add the node to
      Throws:
      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 from
      nodeNum - the index of the node to move
      target - the SpanList to add the node to
      Throws:
      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 from
      node - the node to move
      target - the SpanList to add the node to
      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
      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 from
      nodeNum - the index of the node to move
      target - the SpanList to add the node to
      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
    • childIterator

      public 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 class SpanList
      Returns:
      a ListIterator which traverses all immediate children of this SpanNode
      See Also:
    • childIteratorRecursive

      public 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 class SpanList
      Returns:
      a ListIterator which recursively traverses all children and their children etc. of all subtrees of this AlternateSpanList
      See Also:
    • childIterator

      public 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:
    • childIteratorRecursive

      public 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:
    • numChildren

      public int numChildren(int i)
    • children

      protected List<SpanNode> children(int i)
      Returns a modifiable List 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(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 add
      probability - the probability of this subtree
      Returns:
      true if successful
      See Also:
    • addChildren

      public void addChildren(int i, 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 subtree
      subtree - the subtree to add
      probability - the probability of this subtree
      See Also:
    • removeChildren

      public 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 List<SpanNode> setChildren(int i, List<SpanNode> subtree, double probability)
      Sets the subtree at index i.
      Parameters:
      i - the index of where to set the subtree
      subtree - the subtree to set
      probability - the probability to set
      Returns:
      the overwritten subtree, if any
    • getFrom

      public int getFrom(int i)
      Returns the character index where this SpanNode starts (inclusive), i.e. the smallest SpanNode.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 this SpanNode ends (exclusive), i.e. the greatest SpanNode.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

      public CharSequence getText(int i, 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 use
      text - 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 of
      probability - 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 calling AlternateSpanList.children(i).add(node);
      Parameters:
      i - index
      node - span node
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class SpanList
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class SpanList
    • toString

      public String toString()
      Overrides:
      toString in class SpanList