NGramLanguageModel (Apache OpenNLP Tools 2.5.9 API)
Description: declaration: package: opennlp.tools.languagemodel, class: NGramLanguageModel

# 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
  Description
  `NGramLanguageModel()`
  Initializes an `NGramLanguageModel` with `DEFAULT_N`.
  `NGramLanguageModel(int n)`
  Initializes an `NGramLanguageModel` with the given `n` for the ngram size.
  `NGramLanguageModel(InputStream in)`
  Initializes a `NGramLanguageModel` instance via a valid `InputStream`.
  `NGramLanguageModel(InputStream in, int n)`
  Initializes a `NGramLanguageModel` instance via a valid `InputStream`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(String... tokens)`
  Adds further tokens.
  `double`
  `calculateProbability(String... tokens)`
  Calculate the probability of a series of tokens (e.g. a sentence), given a vocabulary.
  `String[]`
  `predictNextTokens(String... tokens)`
  Predict the most probable output sequence of tokens, given an input sequence of `tokens`.

  ### Methods inherited from class NGramModel

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

  ### Methods inherited from class Object

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

  ### Methods inherited from interface Iterable

  `forEach, spliterator`

- ## Constructor Details

  - ### NGramLanguageModel

    public NGramLanguageModel()
    Initializes an `NGramLanguageModel` with `DEFAULT_N`.

  - ### NGramLanguageModel

    public NGramLanguageModel(int n)
    Initializes an `NGramLanguageModel` with the given `n` for the ngram size.
    Parameters:
      `n` - The size of the ngrams to be used. Must be greater than `0`.
    Throws:
      `IllegalArgumentException` - Thrown if one of the arguments was invalid.

  - ### NGramLanguageModel

    public NGramLanguageModel(InputStream in) throws IOException
    Initializes a `NGramLanguageModel` instance via a valid `InputStream`.
    Parameters:
      `in` - The `InputStream` used for loading the model.
    Throws:
      `IOException` - Thrown if IO errors occurred during initialization.
      `IllegalArgumentException` - Thrown if one of the arguments was invalid.

  - ### NGramLanguageModel

    public NGramLanguageModel(InputStream in, int n) throws IOException
    Initializes a `NGramLanguageModel` instance via a valid `InputStream`.
    Parameters:
      `in` - The `InputStream` used for loading the model.
      `n` - The size of the ngrams to be used. Must be greater than `0`.
    Throws:
      `IOException` - Thrown if IO errors occurred during initialization.
      `IllegalArgumentException` - Thrown if one of the arguments was invalid.

- ## Method Details

  - ### add

    public void add(String... tokens)
    Adds further tokens.
    Parameters:
      `tokens` - Text elements to add to the `NGramLanguageModel`.

  - ### 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 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