Search in sources :

Example 1 with RecommenderContext

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext in project inception by inception-project.

the class LappsGridRecommender method predict.

@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException {
    try {
        Container container = new Container();
        new DKPro2Lif().convert(aCas.getJCas(), container);
        String request = new Data<>(Discriminators.Uri.LIF, container).asJson();
        String response = client.execute(request);
        DataContainer result = Serializer.parse(response, DataContainer.class);
        aCas.reset();
        new Lif2DKPro().convert(result.getPayload(), aCas.getJCas());
        Feature isPredictionFeature = getIsPredictionFeature(aCas);
        for (AnnotationFS predictedAnnotation : select(aCas, getPredictedType(aCas))) {
            predictedAnnotation.setBooleanValue(isPredictionFeature, true);
        }
        // Drop the tokens we got from the remote service since their boundaries might not
        // match ours.
        select(aCas, getType(aCas, Token.class)).forEach(aCas::removeFsFromIndexes);
        // If the remote service did not return tokens (or if we didn't find them...), then
        // let's just re-add the tokens that we originally sent. We need the tokens later
        // when extracting the predicted annotations
        Type tokenType = getType(aCas, Token.class);
        if (select(aCas, getType(aCas, Token.class)).isEmpty()) {
            container.getView(0).getAnnotations().stream().filter(a -> Discriminators.Uri.TOKEN.equals(a.getAtType())).forEach(token -> {
                AnnotationFS t = aCas.createAnnotation(tokenType, token.getStart().intValue(), token.getEnd().intValue());
                aCas.addFsToIndexes(t);
            });
        }
    } catch (Exception e) {
        throw new RecommendationException("Cannot predict", e);
    }
}
Also used : Serializer(org.lappsgrid.serialization.Serializer) DKPro2Lif(org.dkpro.core.io.lif.internal.DKPro2Lif) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Data(org.lappsgrid.serialization.Data) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) LoggerFactory(org.slf4j.LoggerFactory) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) RecommendationEngine(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine) Discriminators(org.lappsgrid.discriminator.Discriminators) RecommendationEngineCapability(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineCapability) Type(org.apache.uima.cas.Type) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) ServiceClient(org.lappsgrid.client.ServiceClient) Logger(org.slf4j.Logger) CasUtil.select(org.apache.uima.fit.util.CasUtil.select) DataContainer(org.lappsgrid.serialization.DataContainer) Recommender(de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender) LappsGridRecommenderTraits(de.tudarmstadt.ukp.inception.recommendation.imls.lapps.traits.LappsGridRecommenderTraits) DataSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) List(java.util.List) Container(org.lappsgrid.serialization.lif.Container) Lif2DKPro(org.dkpro.core.io.lif.internal.Lif2DKPro) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) Lif2DKPro(org.dkpro.core.io.lif.internal.Lif2DKPro) DKPro2Lif(org.dkpro.core.io.lif.internal.DKPro2Lif) Feature(org.apache.uima.cas.Feature) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) DataContainer(org.lappsgrid.serialization.DataContainer) Container(org.lappsgrid.serialization.lif.Container) DataContainer(org.lappsgrid.serialization.DataContainer) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)

Example 2 with RecommenderContext

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext in project inception by inception-project.

the class LappsRecommenderIntegrationTest method thatPredictingPosWorks.

@Test
@Disabled
public void thatPredictingPosWorks() throws Exception {
    RecommenderContext context = new RecommenderContext();
    CAS cas = loadData();
    sut.predict(context, cas);
    Collection<POS> predictions = JCasUtil.select(cas.getJCas(), POS.class);
    assertThat(predictions).as("There should be some predictions").isNotEmpty();
}
Also used : RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) CAS(org.apache.uima.cas.CAS) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with RecommenderContext

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext in project inception by inception-project.

the class OpenNlpDoccatRecommenderTest method setUp.

@BeforeEach
public void setUp() {
    context = new RecommenderContext();
    recommender = buildRecommender();
    traits = new OpenNlpDoccatRecommenderTraits();
    traits.setNumThreads(2);
    traits.setTrainingSetSizeLimit(250);
    traits.setPredictionLimit(250);
}
Also used : RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with RecommenderContext

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext in project inception by inception-project.

the class StringMatchingRecommenderTest method setUp.

@BeforeEach
public void setUp() {
    casStorageSession = CasStorageSession.open();
    context = new RecommenderContext();
    recommender = buildRecommender();
    traits = new StringMatchingRecommenderTraits();
}
Also used : RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with RecommenderContext

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext in project inception by inception-project.

the class DL4JSequenceRecommenderTest method setUp.

@BeforeEach
public void setUp() {
    // By default, ND4J will use a value equal to the number of physical CPU cores (not logical
    // cores) as this will give optimal performance
    // Nd4jBlas nd4jBlas = (Nd4jBlas) Nd4j.factory().blas();
    // nd4jBlas.setMaxThreads(2);
    // NativeOpsHolder instance = NativeOpsHolder.getInstance();
    // NativeOps deviceNativeOps = instance.getDeviceNativeOps();
    // deviceNativeOps.setOmpNumThreads(2);
    context = new RecommenderContext();
    traits = new DL4JSequenceRecommenderTraits();
    traits.setTrainingSetSizeLimit(250);
    traits.setPredictionLimit(250);
    traits.setBatchSize(50);
    casSession = CasStorageSession.open();
}
Also used : RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RecommenderContext (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext)16 BeforeEach (org.junit.jupiter.api.BeforeEach)9 RecommendationEngine (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine)6 CAS (org.apache.uima.cas.CAS)6 Recommender (de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender)5 RecommendationEngineFactory (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngineFactory)4 List (java.util.List)4 Type (org.apache.uima.cas.Type)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)3 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)3 EvaluatedRecommender (de.tudarmstadt.ukp.inception.recommendation.api.model.EvaluatedRecommender)3 RecommendationException (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)3 RecommenderTaskEvent (de.tudarmstadt.ukp.inception.recommendation.event.RecommenderTaskEvent)3 AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)2 AUTO_CAS_UPGRADE (de.tudarmstadt.ukp.clarin.webanno.api.CasUpgradeMode.AUTO_CAS_UPGRADE)2 DocumentService (de.tudarmstadt.ukp.clarin.webanno.api.DocumentService)2 Range (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Range)2 Predictions (de.tudarmstadt.ukp.inception.recommendation.api.model.Predictions)2