SentenceDetectorME (Apache OpenNLP :: Core :: Runtime 3.0.0-M4 API)
Description: declaration: package: opennlp.tools.sentdetect, class: SentenceDetectorME

Package opennlp.tools.sentdetect

# Class SentenceDetectorME

java.lang.Object
opennlp.tools.sentdetect.SentenceDetectorME

All Implemented Interfaces:
  `opennlp.tools.ml.Probabilistic`, `opennlp.tools.sentdetect.SentenceDetector`

@ThreadSafe public class SentenceDetectorME extends Object implements opennlp.tools.sentdetect.SentenceDetector, opennlp.tools.ml.Probabilistic
A sentence detector for splitting up raw text into sentences.

A maximum entropy model is used to evaluate end-of-sentence characters in a string to determine if they signify the end of a sentence.

A sentence detector instance is thread-safe. One instance can be shared across multiple threads to save memory.

Note: In container environments with classloader isolation (e.g. Jakarta EE), ensure instances do not outlive the application's lifecycle, as underlying components use `ThreadLocal` state that may pin the classloader.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `NO_SPLIT`
  Constant indicates no sentence split.
  `static final String`
  `SPLIT`
  Constant indicates a sentence split.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SentenceDetectorME(String language)`
  Initializes the sentence detector by downloading a default model.
  `SentenceDetectorME(SentenceModel model)`
  Initializes the current instance.
  `SentenceDetectorME(SentenceModel model, Dictionary abbDict)`
  Instantiates a `SentenceDetectorME` with an existing `SentenceModel`.
  `SentenceDetectorME(SentenceModel model, Factory factory)`
  Deprecated.
  Use a `SentenceDetectorFactory` to extend SentenceDetector functionality.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clearThreadLocalState()`
  Removes thread-local state to prevent classloader leaks in container environments.
  `double[]`
  `getSentenceProbabilities()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Use `probs()` instead.
  `double[]`
  `probs()`
  The sequence was determined based on the previous call to `sentDetect(CharSequence)`.
  `String[]`
  `sentDetect(CharSequence s)`
  Detects sentences in given input `CharSequence`..
  `opennlp.tools.util.Span[]`
  `sentPosDetect(CharSequence s)`
  Detects the position of the first words of sentences in a `CharSequence`.
  `static SentenceModel`
  `train(String languageCode, opennlp.tools.util.ObjectStream<opennlp.tools.sentdetect.SentenceSample> samples, SentenceDetectorFactory sdFactory, opennlp.tools.util.TrainingParameters mlParams)`
  Starts a training of a `SentenceModel` with the given parameters.

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

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

- ## Field Details

  - ### SPLIT

    public static final String SPLIT
    Constant indicates a sentence split.
    See Also:
      - Constant Field Values

  - ### NO_SPLIT

    public static final String NO_SPLIT
    Constant indicates no sentence split.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### SentenceDetectorME

    public SentenceDetectorME(String language) throws IOException
    Initializes the sentence detector by downloading a default model.
    Parameters:
      `language` - The language of the sentence detector.
    Throws:
      `IOException` - Thrown if the model cannot be downloaded or saved.

  - ### SentenceDetectorME

    public SentenceDetectorME(SentenceModel model)
    Initializes the current instance.
    Parameters:
      `model` - the `SentenceModel`

  - ### SentenceDetectorME

    public SentenceDetectorME(SentenceModel model, Dictionary abbDict)
    Instantiates a `SentenceDetectorME` with an existing `SentenceModel`.
    Parameters:
      `model` - The `SentenceModel` to be used.
      `abbDict` - The `Dictionary` to be used. It must fit the language of the `model`.

  - ### SentenceDetectorME

    @Deprecated public SentenceDetectorME(SentenceModel model, Factory factory)
    Deprecated.
    Use a `SentenceDetectorFactory` to extend SentenceDetector functionality.

- ## Method Details

  - ### sentDetect

    public String[] sentDetect(CharSequence s)
    Detects sentences in given input `CharSequence`..
    Specified by:
      `sentDetect` in interface `opennlp.tools.sentdetect.SentenceDetector`
    Parameters:
      `s` - The `CharSequence`. to be processed.
    Returns:
      A string array containing individual sentences as elements.

  - ### sentPosDetect

    public opennlp.tools.util.Span[] sentPosDetect(CharSequence s)
    Detects the position of the first words of sentences in a `CharSequence`.
    Specified by:
      `sentPosDetect` in interface `opennlp.tools.sentdetect.SentenceDetector`
    Parameters:
      `s` - The `CharSequence` to be processed.
    Returns:
      An `span array` containing the positions of the end index of every sentence.

  - ### probs

    public double[] probs()
    The sequence was determined based on the previous call to `sentDetect(CharSequence)`.
    Specified by:
      `probs` in interface `opennlp.tools.ml.Probabilistic`
    Returns:
      an array with the same number of probabilities as for the last `sentDetect(CharSequence)` call; if not applicable, an empty array

  - ### clearThreadLocalState

    public void clearThreadLocalState()
    Removes thread-local state to prevent classloader leaks in container environments. Call when the thread is returned to a pool or the sentence detector is no longer needed.

  - ### getSentenceProbabilities

    @Deprecated(forRemoval=true, since="2.5.5") public double[] getSentenceProbabilities()
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use `probs()` instead.
    Returns:
      The probability for each sentence returned for the most recent call to `sentDetect(CharSequence)`; if not applicable, an empty array

  - ### train

    public static SentenceModel train(String languageCode, opennlp.tools.util.ObjectStream<opennlp.tools.sentdetect.SentenceSample> samples, SentenceDetectorFactory sdFactory, opennlp.tools.util.TrainingParameters mlParams) throws IOException
    Starts a training of a `SentenceModel` with the given parameters.
    Parameters:
      `languageCode` - The ISO language code to train the model. Must not be `null`.
      `samples` - The `ObjectStream` of `SentenceSample` used as input for training.
      `sdFactory` - The `SentenceDetectorFactory` for creating related objects as defined via `mlParams`.
      `mlParams` - The `TrainingParameters` for the context of the training process.
    Returns:
      A valid, trained `SentenceModel` instance.
    Throws:
      `IOException` - Thrown if IO errors occurred.