Search in sources :

Example 1 with RecommendationEngineFactory

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);
}
Also used : RecommendationEngineFactory(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory) Recommender(de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) StringMatchingRelationRecommenderTraits(de.tudarmstadt.ukp.inception.recommendation.imls.stringmatch.relation.StringMatchingRelationRecommenderTraits) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 2 with RecommendationEngineFactory

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);
}
Also used : RecommendationEngineFactory(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory)

Example 3 with RecommendationEngineFactory

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;
    }
}
Also used : RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) NoResultException(javax.persistence.NoResultException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) RecommendationEngineFactory(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory) CAS(org.apache.uima.cas.CAS) Predictions(de.tudarmstadt.ukp.inception.recommendation.api.model.Predictions) EvaluatedRecommender(de.tudarmstadt.ukp.inception.recommendation.api.model.EvaluatedRecommender) Recommender(de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender) RecommenderTaskEvent(de.tudarmstadt.ukp.inception.recommendation.event.RecommenderTaskEvent) RecommendationEngine(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine)

Example 4 with RecommendationEngineFactory

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);
}
Also used : RecommendationEngineFactory(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.jupiter.api.Test)

Example 5 with RecommendationEngineFactory

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));
}
Also used : RecommendationEngineFactory(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory) RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) RecommendationEngine(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine) TempFileResource(de.tudarmstadt.ukp.clarin.webanno.support.wicket.TempFileResource)

Aggregations

RecommendationEngineFactory (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory)9 Recommender (de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender)5 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)4 RecommendationEngine (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine)4 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)3 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)3 CAS (org.apache.uima.cas.CAS)3 AUTO_CAS_UPGRADE (de.tudarmstadt.ukp.clarin.webanno.api.CasUpgradeMode.AUTO_CAS_UPGRADE)2 DocumentService (de.tudarmstadt.ukp.clarin.webanno.api.DocumentService)2 SHARED_READ_ONLY_ACCESS (de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasAccessMode.SHARED_READ_ONLY_ACCESS)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)2 EvaluatedRecommender (de.tudarmstadt.ukp.inception.recommendation.api.model.EvaluatedRecommender)2 RecommenderContext (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 ExceptionUtils.getRootCauseMessage (org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage)2 Logger (org.slf4j.Logger)2