Class BeamSearch.SearchNode

java.lang.Object
opennlp.tools.ml.BeamSearch.SearchNode
All Implemented Interfaces:
Comparable<BeamSearch.SearchNode>
Enclosing class:
BeamSearch

private static final class BeamSearch.SearchNode extends Object implements Comparable<BeamSearch.SearchNode>
Immutable node in a backward-linked chain of outcome candidates, used only inside BeamSearch.bestSequences(int, Object[], Object[], double, BeamSearchContextGenerator, SequenceValidator). A node stores its own outcome, its probability, the accumulated score parent.score + StrictMath.log(prob) and a parent link, so extending a candidate is O(1); the full outcome array is materialized only when a candidate is expanded. Nodes are ordered by compareTo(SearchNode).
  • Field Details

    • parent

      private final BeamSearch.SearchNode parent
    • outcome

      private final String outcome
    • prob

      private final double prob
    • score

      private final double score
    • size

      private final int size
  • Constructor Details

    • SearchNode

      private SearchNode()
      Creates the root node: the empty candidate with score 0.
    • SearchNode

      private SearchNode(BeamSearch.SearchNode parent, String outcome, double prob)
      Creates a candidate extending parent by one outcome.
      Parameters:
      parent - The candidate to extend. Must not be null.
      outcome - The outcome to append.
      prob - The probability of outcome.
  • Method Details

    • outcomes

      private String[] outcomes()
      Returns:
      The outcomes on the path from the root to this node, in sequence order.
    • toSequence

      private Sequence toSequence()
      Returns:
      The Sequence of the outcomes and probabilities on the path from the root to this node, in sequence order.
    • collect

      private void collect(String[] outcomes, double[] probs)
      Fills the outcomes, and the probabilities if requested, on the path from the root to this node, in sequence order.
      Parameters:
      outcomes - The array to fill, of length size.
      probs - The array to fill, of length size, or null to skip them.
    • compareTo

      public int compareTo(BeamSearch.SearchNode other)
      Orders nodes by descending score, mirroring Sequence.compareTo(Sequence). Exact score ties are broken lexicographically by the outcome chain from the root, so tied candidates expand and are returned in the same canonical order on every run and JVM. PriorityQueue gives no ordering guarantee for elements whose comparison returns 0, and ties decide both which candidates survive beam truncation and the order of equal-scored winners.
      Specified by:
      compareTo in interface Comparable<BeamSearch.SearchNode>