Search in sources :

Example 1 with SentimentModel

use of opennlp.tools.sentiment.SentimentModel in project tika by apache.

the class SentimentParser method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    LOG.debug("Initializing...");
    if (modelPath == null) {
        throw new TikaConfigException("Parameter 'modelPath' is required but it is not set");
    }
    try {
        URL resolvedUrl = null;
        if (modelPath.startsWith("http://") || modelPath.startsWith("https://")) {
            resolvedUrl = new URL(modelPath);
        } else {
            resolvedUrl = getClass().getClassLoader().getResource(modelPath);
            File file = new File(modelPath);
            if (file.exists()) {
                // file on filesystem gets higher priority
                resolvedUrl = file.toURI().toURL();
            }
        }
        if (resolvedUrl == null) {
            throw new TikaConfigException("Model doesn't exists :" + modelPath);
        }
        LOG.info("Sentiment Model is at {}", resolvedUrl);
        long st = System.currentTimeMillis();
        SentimentModel model = new SentimentModel(resolvedUrl);
        long time = System.currentTimeMillis() - st;
        LOG.debug("time taken to load model {}", time);
        classifier = new SentimentME(model);
    } catch (Exception e) {
        LOG.warn("Failed to load sentiment model from {}" + modelPath);
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : SentimentModel(opennlp.tools.sentiment.SentimentModel) SentimentME(opennlp.tools.sentiment.SentimentME) TikaConfigException(org.apache.tika.exception.TikaConfigException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) TikaException(org.apache.tika.exception.TikaException) SAXException(org.xml.sax.SAXException) TikaConfigException(org.apache.tika.exception.TikaConfigException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 SentimentME (opennlp.tools.sentiment.SentimentME)1 SentimentModel (opennlp.tools.sentiment.SentimentModel)1 TikaConfigException (org.apache.tika.exception.TikaConfigException)1 TikaException (org.apache.tika.exception.TikaException)1 SAXException (org.xml.sax.SAXException)1