Package 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
ALanguageModel
based on aNGramModel
using Stupid Backoff to get the probabilities of the ngrams.
-
-
Constructor Summary
Constructors Constructor Description NGramLanguageModel()
Initializes anNGramLanguageModel
withDEFAULT_N
.NGramLanguageModel(int n)
Initializes anNGramLanguageModel
with the givenn
for the ngram size.NGramLanguageModel(InputStream in)
Initializes aNGramLanguageModel
instance via a validInputStream
.NGramLanguageModel(InputStream in, int n)
Initializes aNGramLanguageModel
instance via a validInputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods 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.String[]
predictNextTokens(String... tokens)
Predict the most probable output sequence of tokens, given an input sequence oftokens
.StringList
predictNextTokens(StringList tokens)
Deprecated.-
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 interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
NGramLanguageModel
public NGramLanguageModel()
Initializes anNGramLanguageModel
withDEFAULT_N
.
-
NGramLanguageModel
public NGramLanguageModel(int n)
Initializes anNGramLanguageModel
with the givenn
for the ngram size.- Parameters:
n
- The size of the ngrams to be used. Must be greater than0
.- Throws:
IllegalArgumentException
- Thrown if one of the arguments was invalid.
-
NGramLanguageModel
public NGramLanguageModel(InputStream in) throws IOException
Initializes aNGramLanguageModel
instance via a validInputStream
.- Parameters:
in
- TheInputStream
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 aNGramLanguageModel
instance via a validInputStream
.- Parameters:
in
- TheInputStream
used for loading the model.n
- The size of the ngrams to be used. Must be greater than0
.- Throws:
IOException
- Thrown if IO errors occurred during initialization.IllegalArgumentException
- Thrown if one of the arguments was invalid.
-
-
Method Detail
-
add
public void add(String... tokens)
Adds further tokens.- Parameters:
tokens
- Text elements to add to theNGramLanguageModel
.
-
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 interfaceLanguageModel
- Parameters:
tokens
- the text tokens to calculate theprobability
for.- Returns:
- the probability of the given text tokens in the vocabulary
-
predictNextTokens
@Deprecated public StringList predictNextTokens(StringList tokens)
Deprecated.Description copied from interface:LanguageModel
Predict the most probable output sequence of tokens, given an input sequence oftokens
.- Specified by:
predictNextTokens
in interfaceLanguageModel
- 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 oftokens
.- Specified by:
predictNextTokens
in interfaceLanguageModel
- Parameters:
tokens
- a sequence of tokens- Returns:
- the most probable subsequent token sequence
-
-