Example usage for opennlp.tools.languagemodel NGramLanguageModel NGramLanguageModel

List of usage examples for opennlp.tools.languagemodel NGramLanguageModel NGramLanguageModel

Introduction

In this page you can find the example usage for opennlp.tools.languagemodel NGramLanguageModel NGramLanguageModel.

Prototype

public NGramLanguageModel(InputStream in, int n) throws IOException 

Source Link

Usage

From source file:opennlp.tools.languagemodel.NgramLanguageModelTest.java

@Test
public void testBigramProbabilityNoSmoothing() throws Exception {
    NGramLanguageModel model = new NGramLanguageModel(2, 0);
    model.add(new StringList("<s>", "I", "am", "Sam", "</s>"), 1, 2);
    model.add(new StringList("<s>", "Sam", "I", "am", "</s>"), 1, 2);
    model.add(new StringList("<s>", "I", "do", "not", "like", "green", "eggs", "and", "ham", "</s>"), 1, 2);
    double probability = model.calculateProbability(new StringList("<s>", "I"));
    Assert.assertEquals(0.666d, probability, 0.001);
    probability = model.calculateProbability(new StringList("Sam", "</s>"));
    Assert.assertEquals(0.5d, probability, 0.001);
    probability = model.calculateProbability(new StringList("<s>", "Sam"));
    Assert.assertEquals(0.333d, probability, 0.001);
    probability = model.calculateProbability(new StringList("am", "Sam"));
    Assert.assertEquals(0.5d, probability, 0.001);
    probability = model.calculateProbability(new StringList("I", "am"));
    Assert.assertEquals(0.666d, probability, 0.001);
    probability = model.calculateProbability(new StringList("I", "do"));
    Assert.assertEquals(0.333d, probability, 0.001);
    probability = model.calculateProbability(new StringList("I", "am", "Sam"));
    Assert.assertEquals(0.333d, probability, 0.001);
}

From source file:opennlp.tools.languagemodel.NgramLanguageModelTest.java

@Test
public void testSerializedNGramLanguageModel() throws Exception {
    NGramLanguageModel languageModel = new NGramLanguageModel(
            getClass().getResourceAsStream("/opennlp/tools/ngram/ngram-model.xml"), 3);
    double probability = languageModel.calculateProbability(new StringList("The", "brown", "fox", "jumped"));
    Assert.assertTrue("a probability measure should be between 0 and 1 [was " + probability + "]",
            probability >= 0 && probability <= 1);
    StringList tokens = languageModel.predictNextTokens(new StringList("fox"));
    Assert.assertNotNull(tokens);//w  w  w . j  a  v a  2s  . co m
    Assert.assertEquals(new StringList("jumped"), tokens);
}