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

opennlp.tools.languagemodel

## Class NGramLanguageModel

- java.lang.Object
- - opennlp.tools.ngram.NGramModel
  - - opennlp.tools.languagemodel.NGramLanguageModel

- All Implemented Interfaces:
    Iterable<StringList>, LanguageModel

      public class NGramLanguageModel
      extends NGramModel
      implements LanguageModel

  A `LanguageModel` based on a `NGramModel` using Stupid Backoff to get the probabilities of the ngrams.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `NGramLanguageModel()`
    `NGramLanguageModel(InputStream in)`
    `NGramLanguageModel(InputStream in, int n)`
    `NGramLanguageModel(int n)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `add(String... tokens)`
    `double` | `calculateProbability(String... tokens)` Calculate the probability of a series of tokens (e.g.
    `double` | `calculateProbability(StringList tokens)` Calculate the probability of a series of tokens (e.g.
    `String[]` | `predictNextTokens(String... tokens)` Predict the most probable output sequence of tokens, given an input sequence of tokens.
    `StringList` | `predictNextTokens(StringList tokens)` Predict the most probable output sequence of tokens, given an input sequence of tokens.

    - ### Methods inherited from class opennlp.tools.ngram.NGramModel

      `add, add, add, contains, cutoff, equals, getCount, hashCode, iterator, numberOfGrams, remove, serialize, setCount, size, toDictionary, toDictionary, toString`

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

      `getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach, spliterator`

- - ### Constructor Detail

    - #### NGramLanguageModel

          public NGramLanguageModel()

    - #### NGramLanguageModel

          public NGramLanguageModel(int n)

    - #### NGramLanguageModel

          public NGramLanguageModel(InputStream in)
                             throws IOException

      Throws:
        `IOException`

    - #### NGramLanguageModel

          public NGramLanguageModel(InputStream in,
                                    int n)
                             throws IOException

      Throws:
        `IOException`

  - ### Method Detail

    - #### add

          public void add(String... tokens)

    - #### calculateProbability

          public double calculateProbability(StringList tokens)

      Description copied from interface: `LanguageModel`
      Calculate the probability of a series of tokens (e.g. a sentence), given a vocabulary.
      Specified by:
        `calculateProbability` in interface `LanguageModel`
      Parameters:
        `tokens` - the text tokens to calculate the probability for
      Returns:
        the probability of the given text tokens in the vocabulary

    - #### calculateProbability

          public double calculateProbability(String... tokens)

      Description copied from interface: `LanguageModel`
      Calculate the probability of a series of tokens (e.g. a sentence), given a vocabulary.
      Specified by:
        `calculateProbability` in interface `LanguageModel`
      Parameters:
        `tokens` - the text tokens to calculate the probability for
      Returns:
        the probability of the given text tokens in the vocabulary

    - #### predictNextTokens

          public StringList predictNextTokens(StringList tokens)

      Description copied from interface: `LanguageModel`
      Predict the most probable output sequence of tokens, given an input sequence of tokens.
      Specified by:
        `predictNextTokens` in interface `LanguageModel`
      Parameters:
        `tokens` - a sequence of tokens
      Returns:
        the most probable subsequent token sequence

    - #### predictNextTokens

          public String[] predictNextTokens(String... tokens)

      Description copied from interface: `LanguageModel`
      Predict the most probable output sequence of tokens, given an input sequence of tokens.
      Specified by:
        `predictNextTokens` in interface `LanguageModel`
      Parameters:
        `tokens` - a sequence of tokens
      Returns:
        the most probable subsequent token sequence

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