Package opennlp.tools.languagemodel

# Class NGramLanguageModel

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

All Implemented Interfaces:
  `Iterable<StringList>`, `opennlp.tools.languagemodel.LanguageModel`

public class NGramLanguageModel extends NGramModel implements opennlp.tools.languagemodel.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)`
  `String[]`
  `predictNextTokens(String... 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 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)
    Specified by:
      `calculateProbability` in interface `opennlp.tools.languagemodel.LanguageModel`

  - ### predictNextTokens

    public String[] predictNextTokens(String... tokens)
    Specified by:
      `predictNextTokens` in interface `opennlp.tools.languagemodel.LanguageModel`