Class AlternateSpanList

  • All Implemented Interfaces:
    SpanNodeParent, java.lang.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:
    SpanList
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte ID  
    • 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 modifiable List of child nodes of first subtree.
      protected java.util.List<SpanNode> children​(int i)
      Returns a modifiable List 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 this SpanNode starts (inclusive), i.e. the smallest SpanNode.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 this SpanNode ends (exclusive), i.e. the greatest SpanNode.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 java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AlternateSpanList

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

        public AlternateSpanList​(SpanNodeReader reader)
    • Method Detail

      • 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 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​(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 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:
        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 from
        nodeNum - the index of the node to move
        target - 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 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:
        java.lang.IllegalArgumentException - if the given node is not a child of this SpanList
        java.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 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:
        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 class SpanList
        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 class SpanList
        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 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​(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 add
        probability - 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 subtree
        subtree - the subtree to add
        probability - 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 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:
        java.lang.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:
        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 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​(java.lang.Object o)
        Overrides:
        equals in class SpanList
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class SpanList