use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory in project inception by inception-project.
the class BasicRelationRecommenderInitializer method configure.
@Override
public void configure(Project aProject) throws IOException {
AnnotationLayer relationLayer = annotationService.findLayer(aProject, BASIC_RELATION_LAYER_NAME);
AnnotationFeature labelFeature = annotationService.getFeature(BASIC_RELATION_LABEL_FEATURE_NAME, relationLayer);
Recommender recommender = new Recommender(getName(), relationLayer);
recommender.setFeature(labelFeature);
recommender.setMaxRecommendations(3);
recommender.setThreshold(0.0d);
recommender.setTool(recommenderFactory.getId());
//
RecommendationEngineFactory<StringMatchingRelationRecommenderTraits> factory = (RecommendationEngineFactory) recommendationService.getRecommenderFactory(recommender).get();
StringMatchingRelationRecommenderTraits traits = factory.readTraits(recommender);
traits.setAdjunctFeature(BASIC_SPAN_LABEL_FEATURE_NAME);
factory.writeTraits(recommender, traits);
recommendationService.createOrUpdateRecommender(recommender);
}
use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory in project inception by inception-project.
the class RecommenderEditorPanel method generateName.
private String generateName(Recommender aRecommender) {
if (aRecommender.getFeature() == null || aRecommender.getLayer() == null || aRecommender.getTool() == null) {
return null;
}
RecommendationEngineFactory factory = recommenderRegistry.getFactory(aRecommender.getTool());
String factoryName = factory != null ? factory.getName() : "NO FACTORY!";
return String.format(Locale.US, "[%s@%s] %s", aRecommender.getLayer().getUiName(), aRecommender.getFeature().getUiName(), factoryName);
}
use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory in project inception by inception-project.
the class RecommendationServiceImpl method computePredictions.
private void computePredictions(LazyCas aOriginalCas, EvaluatedRecommender aEvaluatedRecommender, Predictions predictions, CAS predictionCas, SourceDocument aDocument, User aUser, int aPredictionBegin, int aPredictionEnd) throws IOException {
Project project = aDocument.getProject();
Predictions activePredictions = getPredictions(aUser, project);
int predictionBegin = aPredictionBegin;
int predictionEnd = aPredictionEnd;
// Make sure we have the latest recommender config from the DB - the one
// from the active recommenders list may be outdated
Recommender recommender = aEvaluatedRecommender.getRecommender();
try {
recommender = getRecommender(recommender.getId());
} catch (NoResultException e) {
predictions.log(LogMessage.info(recommender.getName(), "Recommender no longer available... skipping"));
log.info("{}[{}]: Recommender no longer available... skipping", aUser, recommender.getName());
return;
}
if (!recommender.isEnabled()) {
predictions.log(LogMessage.info(recommender.getName(), "Recommender disabled... skipping"));
log.debug("{}[{}]: Disabled - skipping", aUser, recommender.getName());
return;
}
Optional<RecommenderContext> context = getContext(aUser, recommender);
if (!context.isPresent()) {
predictions.log(LogMessage.info(recommender.getName(), "Recommender has no context... skipping"));
log.info(//
"No context available for recommender {} for user {} on document {} in " + "project {} - skipping recommender", recommender, aUser, aDocument, aDocument.getProject());
return;
}
RecommenderContext ctx = context.get();
ctx.setUser(aUser);
Optional<RecommendationEngineFactory<?>> maybeFactory = getRecommenderFactory(recommender);
if (maybeFactory.isEmpty()) {
log.warn("{}[{}]: No factory found - skipping recommender", aUser, recommender.getName());
return;
}
RecommendationEngineFactory<?> factory = maybeFactory.get();
// by this type of recommender
if (!factory.accepts(recommender.getLayer(), recommender.getFeature())) {
predictions.log(LogMessage.info(recommender.getName(), "Recommender configured with invalid layer or feature... skipping"));
log.info("{}[{}]: Recommender configured with invalid layer or feature " + "- skipping recommender", aUser, recommender.getName());
return;
}
// We lazily load the CAS only at this point because that allows us to skip
// loading the CAS entirely if there is no enabled layer or recommender.
// If the CAS cannot be loaded, then we skip to the next document.
CAS originalCas = aOriginalCas.get();
predictionBegin = aPredictionBegin < 0 ? 0 : aPredictionBegin;
predictionEnd = aPredictionEnd < 0 ? originalCas.getDocumentText().length() : aPredictionEnd;
try {
RecommendationEngine engine = factory.build(recommender);
if (!engine.isReadyForPrediction(ctx)) {
predictions.log(LogMessage.info(recommender.getName(), "Recommender context is not ready... skipping"));
log.info(//
"Recommender context {} for user {} in project {} is not ready for " + "prediction - skipping recommender", recommender, aUser, aDocument.getProject());
// the recommender is still busy
if (activePredictions != null) {
inheritSuggestionsAtRecommenderLevel(predictions, originalCas, recommender, activePredictions, aDocument, aUser);
}
return;
}
cloneAndMonkeyPatchCAS(project, originalCas, predictionCas);
// we can actually re-use the predictions.
if (TRAINING_NOT_SUPPORTED == engine.getTrainingCapability() && activePredictions != null && activePredictions.hasRunPredictionOnDocument(aDocument)) {
inheritSuggestionsAtRecommenderLevel(predictions, originalCas, engine.getRecommender(), activePredictions, aDocument, aUser);
} else {
generateSuggestions(predictions, ctx, engine, activePredictions, aDocument, originalCas, predictionCas, aUser, predictionBegin, predictionEnd);
}
}// execution even if a particular recommender fails.
catch (Throwable e) {
predictions.log(LogMessage.error(recommender.getName(), "Failed: %s", e.getMessage()));
log.error(//
"Error applying recommender {} for user {} to document {} in project {} - " + "skipping recommender", recommender, aUser, aDocument, aDocument.getProject(), e);
applicationEventPublisher.publishEvent(new RecommenderTaskEvent(this, aUser.getUsername(), e.getMessage(), recommender));
// simply disappear.
if (activePredictions != null) {
inheritSuggestionsAtRecommenderLevel(predictions, originalCas, recommender, activePredictions, aDocument, aUser);
}
return;
}
}
use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory in project inception by inception-project.
the class RecommendationServiceImplIntegrationTest method getNumOfEnabledRecommenders_WithOneEnabledRecommender.
@Test
public void getNumOfEnabledRecommenders_WithOneEnabledRecommender() {
RecommendationEngineFactory recFactory = mock(RecommendationEngineFactory.class);
when(recommenderFactoryRegistry.getFactory(any(String.class))).thenReturn(recFactory);
when(recFactory.accepts(any(AnnotationLayer.class), any(AnnotationFeature.class))).thenReturn(true);
assertThat(recommenderFactoryRegistry.getFactory("nummy")).isNotNull();
sut.createOrUpdateRecommender(rec);
long numOfRecommenders = sut.countEnabledRecommenders();
assertThat(numOfRecommenders).isEqualTo(1);
}
use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory in project inception by inception-project.
the class RecommenderInfoPanel method exportModel.
private IResourceStream exportModel(User aUser, Recommender aRecommender) {
Optional<RecommendationEngineFactory<?>> maybeEngine = recommendationService.getRecommenderFactory(aRecommender);
if (maybeEngine.isEmpty()) {
error("No factory found for " + aRecommender.getName());
return null;
}
RecommendationEngine engine = maybeEngine.get().build(aRecommender);
Optional<RecommenderContext> context = recommendationService.getContext(aUser, aRecommender);
if (context.isEmpty()) {
error("No model trained yet.");
return null;
}
return new TempFileResource((os) -> engine.exportModel(context.get(), os));
}
Aggregations