@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
DoccatModel model = aContext.get(KEY_MODEL).orElseThrow(() ->
new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
DocumentCategorizerME finder = new DocumentCategorizerME(model);
Type sentenceType = getType(aCas, Sentence.class);
Type predictionType = getAnnotationType(aCas, PredictedSpan.class);
Type tokenType = getType(aCas, Token.class);
Feature confidenceFeature = predictionType.getFeatureByBaseName("score");
Feature labelFeature = predictionType.getFeatureByBaseName("label");
for (AnnotationFS sentence : select(aCas, sentenceType)) {
List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence);
String[] tokens = tokenAnnotations.stream()
.map(AnnotationFS::getCoveredText)
.toArray(String[]::new);
double[] outcome = finder.categorize(tokens);
String label = finder.getBestCategory(outcome);
AnnotationFS annotation = aCas.createAnnotation(predictionType, sentence.getBegin(),
sentence.getEnd());
annotation.setDoubleValue(confidenceFeature, NumberUtils.max(outcome));
annotation.setStringValue(labelFeature, label);
aCas.addFsToIndexes(annotation);
}
}