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);
}
}
Aggregations