Search in sources :

Example 1 with SentenceModel

use of opennlp.tools.sentdetect.SentenceModel in project stanbol by apache.

the class OpenNlpSentenceDetectionEngine method getSentenceDetector.

/**
     * Obtains the {@link SentenceDetectorME} model for the given
     * language form the {@link #openNLP} service. If a custom
     * model is configured for the parsed language than it is
     * loaded by using {@link OpenNLP#getModel(Class, String, Map)}
     * otherwise the default model {@link OpenNLP#getSentenceDetector(String)}
     * is retrieved
     * @param language the language
     * @return the model of <code>null</code> if non is available or
     * an exception was encountered while loading
     */
private SentenceDetector getSentenceDetector(String language) {
    SentenceModel model;
    String modelName = languageConfig.getParameter(language, MODEL_NAME_PARAM);
    if (modelName == null) {
        try {
            model = openNLP.getSentenceModel(language);
        } catch (Exception e) {
            log.warn("Unable to load default Sentence Detection model for language '" + language + "'!", e);
            return null;
        }
    } else {
        try {
            model = openNLP.getModel(SentenceModel.class, modelName, null);
        } catch (Exception e) {
            log.warn("Unable to load Sentence Detection model for language '" + language + "' from the configured model '" + modelName + "'!", e);
            return null;
        }
    }
    if (model != null) {
        log.debug("Sentence Detection Model {} for lanugage '{}' version: {}", new Object[] { model.getClass().getSimpleName(), model.getLanguage(), model.getVersion() != null ? model.getVersion() : "undefined" });
        return new SentenceDetectorME(model);
    }
    log.debug("Sentence Detection Model for Language '{}' not available.", language);
    return null;
}
Also used : SentenceModel(opennlp.tools.sentdetect.SentenceModel) SentenceDetectorME(opennlp.tools.sentdetect.SentenceDetectorME) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 2 with SentenceModel

use of opennlp.tools.sentdetect.SentenceModel in project stanbol by apache.

the class OpenNLPTest method testLoadModelByName.

@Test
public void testLoadModelByName() throws IOException {
    TokenizerModel tokenModel = openNLP.getModel(TokenizerModel.class, "en-token.bin", null);
    Assert.assertNotNull(tokenModel);
    SentenceModel sentModel = openNLP.getModel(SentenceModel.class, "en-sent.bin", null);
    Assert.assertNotNull(sentModel);
    POSModel posModel = openNLP.getModel(POSModel.class, "en-pos-maxent.bin", null);
    Assert.assertNotNull(posModel);
    ChunkerModel chunkModel = openNLP.getModel(ChunkerModel.class, "en-chunker.bin", null);
    Assert.assertNotNull(chunkModel);
    TokenNameFinderModel nerModel = openNLP.getModel(TokenNameFinderModel.class, "en-ner-person.bin", null);
    Assert.assertNotNull(nerModel);
    //unavailable model
    tokenModel = openNLP.getModel(TokenizerModel.class, "ru-token.bin", null);
    Assert.assertNull(tokenModel);
}
Also used : TokenNameFinderModel(opennlp.tools.namefind.TokenNameFinderModel) ChunkerModel(opennlp.tools.chunker.ChunkerModel) SentenceModel(opennlp.tools.sentdetect.SentenceModel) POSModel(opennlp.tools.postag.POSModel) TokenizerModel(opennlp.tools.tokenize.TokenizerModel) Test(org.junit.Test)

Example 3 with SentenceModel

use of opennlp.tools.sentdetect.SentenceModel in project stanbol by apache.

the class OpenNLPTest method testLoadIncompatibleModelByName.

@Test(expected = IllegalStateException.class)
public void testLoadIncompatibleModelByName() throws IOException {
    SentenceModel sentModel = openNLP.getModel(SentenceModel.class, "en-token.bin", null);
    Assert.assertNotNull(sentModel);
}
Also used : SentenceModel(opennlp.tools.sentdetect.SentenceModel) Test(org.junit.Test)

Example 4 with SentenceModel

use of opennlp.tools.sentdetect.SentenceModel in project stanbol by apache.

the class TextAnalyzer method getSentenceDetector.

protected final SentenceDetector getSentenceDetector() {
    if (!config.enableSentenceDetector) {
        return null;
    }
    if (sentenceDetector == null && !sentenceDetectorNotAvailable) {
        try {
            SentenceModel sentModel = openNLP.getSentenceModel(language);
            if (sentModel != null) {
                sentenceDetector = new SentenceDetectorME(sentModel);
            } else {
                log.debug("No Sentence Detection Model for language '{}'", language);
                sentenceDetectorNotAvailable = true;
            }
        } catch (IOException e) {
            log.info("Unable to load Sentence Detection Model for language '" + language + "'", e);
            sentenceDetectorNotAvailable = true;
        }
    }
    return sentenceDetector;
}
Also used : SentenceModel(opennlp.tools.sentdetect.SentenceModel) SentenceDetectorME(opennlp.tools.sentdetect.SentenceDetectorME) IOException(java.io.IOException)

Example 5 with SentenceModel

use of opennlp.tools.sentdetect.SentenceModel in project useful-java-links by Vedenin.

the class OpenNLPSentenceDetectors method testOpenNLPPosition.

private Span[] testOpenNLPPosition(String text) throws Exception {
    try (InputStream modelIn = this.getClass().getResourceAsStream(RESOURCES_EN_SENT_BIN)) {
        SentenceModel model = new SentenceModel(modelIn);
        SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
        return sentenceDetector.sentPosDetect(text);
    }
}
Also used : InputStream(java.io.InputStream) SentenceModel(opennlp.tools.sentdetect.SentenceModel) SentenceDetectorME(opennlp.tools.sentdetect.SentenceDetectorME)

Aggregations

SentenceModel (opennlp.tools.sentdetect.SentenceModel)9 SentenceDetectorME (opennlp.tools.sentdetect.SentenceDetectorME)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 SentenceDetector (opennlp.tools.sentdetect.SentenceDetector)2 ChunkerModel (opennlp.tools.chunker.ChunkerModel)1 TokenNameFinderModel (opennlp.tools.namefind.TokenNameFinderModel)1 POSModel (opennlp.tools.postag.POSModel)1 TokenizerModel (opennlp.tools.tokenize.TokenizerModel)1 InvalidFormatException (opennlp.tools.util.InvalidFormatException)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1