BeamSearch (Apache OpenNLP Tools 1.9.5 API)
Date: 2026-06-18

opennlp.tools.ml

## Class BeamSearch<T>

- java.lang.Object
- - opennlp.tools.ml.BeamSearch<T>

- All Implemented Interfaces:
    SequenceClassificationModel<T>

      public class BeamSearch<T>
      extends Object
      implements SequenceClassificationModel<T>

  Performs k-best search over sequence. This is based on the description in Ratnaparkhi (1998), PhD diss, Univ. of Pennsylvania.
  See Also:
    `Sequence`, `SequenceValidator`, `BeamSearchContextGenerator`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `BEAM_SIZE_PARAMETER`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BeamSearch(int size, MaxentModel model)` Creates new search object.
    `BeamSearch(int size, MaxentModel model, int cacheSize)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Sequence` | `bestSequence(T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator)` Finds the sequence with the highest probability.
    `Sequence[]` | `bestSequences(int numSequences, T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator)` Finds the n most probable sequences.
    `Sequence[]` | `bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator)` Returns the best sequence of outcomes based on model for this object.
    `String[]` | `getOutcomes()` Returns all possible outcomes.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### BEAM_SIZE_PARAMETER

          public static final String BEAM_SIZE_PARAMETER

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### BeamSearch

          public BeamSearch(int size,
                            MaxentModel model)

      Creates new search object.
      Parameters:
        `size` - The size of the beam (k).
        `model` - the model for assigning probabilities to the sequence outcomes.

    - #### BeamSearch

          public BeamSearch(int size,
                            MaxentModel model,
                            int cacheSize)

  - ### Method Detail

    - #### bestSequences

          public Sequence[] bestSequences(int numSequences,
                                          T[] sequence,
                                          Object[] additionalContext,
                                          double minSequenceScore,
                                          BeamSearchContextGenerator<T> cg,
                                          SequenceValidator<T> validator)

      Returns the best sequence of outcomes based on model for this object.
      Specified by:
        `bestSequences` in interface `SequenceClassificationModel<T>`
      Parameters:
        `sequence` - The input sequence.
        `additionalContext` - An Object[] of additional context. This is passed to the context generator blindly with the assumption that the context are appropiate.
      Returns:
        The top ranked sequence of outcomes or null if no sequence could be found

    - #### bestSequences

          public Sequence[] bestSequences(int numSequences,
                                          T[] sequence,
                                          Object[] additionalContext,
                                          BeamSearchContextGenerator<T> cg,
                                          SequenceValidator<T> validator)

      Description copied from interface: `SequenceClassificationModel`
      Finds the n most probable sequences.
      Specified by:
        `bestSequences` in interface `SequenceClassificationModel<T>`
      Returns:

    - #### bestSequence

          public Sequence bestSequence(T[] sequence,
                                       Object[] additionalContext,
                                       BeamSearchContextGenerator<T> cg,
                                       SequenceValidator<T> validator)

      Description copied from interface: `SequenceClassificationModel`
      Finds the sequence with the highest probability.
      Specified by:
        `bestSequence` in interface `SequenceClassificationModel<T>`
      Returns:

    - #### getOutcomes

          public String[] getOutcomes()

      Description copied from interface: `SequenceClassificationModel`
      Returns all possible outcomes.
      Specified by:
        `getOutcomes` in interface `SequenceClassificationModel<T>`
      Returns:

Copyright © 2026 The Apache Software Foundation. All rights reserved.