Search in sources :

Example 1 with RecommendationException

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

the class OpenNlpDoccatRecommender method predict.

@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 sampleUnitType = getType(aCas, SAMPLE_UNIT);
    Type predictedType = getPredictedType(aCas);
    Type tokenType = getType(aCas, Token.class);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);
    int predictionCount = 0;
    for (AnnotationFS sampleUnit : select(aCas, sampleUnitType)) {
        if (predictionCount >= traits.getPredictionLimit()) {
            break;
        }
        predictionCount++;
        List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sampleUnit);
        String[] tokens = tokenAnnotations.stream().map(AnnotationFS::getCoveredText).toArray(String[]::new);
        double[] outcome = finder.categorize(tokens);
        String label = finder.getBestCategory(outcome);
        AnnotationFS annotation = aCas.createAnnotation(predictedType, sampleUnit.getBegin(), sampleUnit.getEnd());
        annotation.setStringValue(predictedFeature, label);
        annotation.setDoubleValue(scoreFeature, NumberUtils.max(outcome));
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) DocumentCategorizerME(opennlp.tools.doccat.DocumentCategorizerME) DoccatModel(opennlp.tools.doccat.DoccatModel) Feature(org.apache.uima.cas.Feature) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)

Example 2 with RecommendationException

use of de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException 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 3 with RecommendationException

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

the class StringMatchingRelationRecommender method predict.

@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException {
    MultiValuedMap<Pair<String, String>, String> model = aContext.get(KEY_MODEL).orElseThrow(() -> new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
    Type sampleUnitType = getType(aCas, SAMPLE_UNIT);
    Type predictedType = getPredictedType(aCas);
    Feature governorFeature = predictedType.getFeatureByBaseName(FEAT_REL_SOURCE);
    Feature dependentFeature = predictedType.getFeatureByBaseName(FEAT_REL_TARGET);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);
    Type attachType = getAttachType(aCas);
    Feature attachFeature = getAttachFeature(aCas);
    Feature scoreFeature = getScoreFeature(aCas);
    for (AnnotationFS sampleUnit : select(aCas, sampleUnitType)) {
        Collection<AnnotationFS> baseAnnotations = selectCovered(attachType, sampleUnit);
        for (AnnotationFS governor : baseAnnotations) {
            for (AnnotationFS dependent : baseAnnotations) {
                if (governor.equals(dependent)) {
                    continue;
                }
                String governorLabel = governor.getStringValue(attachFeature);
                String dependentLabel = dependent.getStringValue(attachFeature);
                Pair<String, String> key = Pair.of(governorLabel, dependentLabel);
                Collection<String> occurrences = model.get(key);
                Map<String, Long> numberOfOccurrencesPerLabel = // 
                occurrences.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
                double totalNumberOfOccurrences = occurrences.size();
                for (String relationLabel : occurrences) {
                    double score = numberOfOccurrencesPerLabel.get(relationLabel) / totalNumberOfOccurrences;
                    AnnotationFS prediction = aCas.createAnnotation(predictedType, governor.getBegin(), governor.getEnd());
                    prediction.setFeatureValue(governorFeature, governor);
                    prediction.setFeatureValue(dependentFeature, dependent);
                    prediction.setStringValue(predictedFeature, relationLabel);
                    prediction.setBooleanValue(isPredictionFeature, true);
                    prediction.setDoubleValue(scoreFeature, score);
                    aCas.addFsToIndexes(prediction);
                }
            }
        }
    }
}
Also used : Feature(org.apache.uima.cas.Feature) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) Pair(org.apache.commons.lang3.tuple.Pair) LabelPair(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair)

Example 4 with RecommendationException

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

the class WeblichtRecommender method predict.

@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException {
    if (!chainService.existsChain(getRecommender())) {
        return;
    }
    try {
        String documentText = aCas.getDocumentText();
        String documentLanguage = aCas.getDocumentLanguage();
        // build http request and assign multipart upload data
        MultipartEntityBuilder builder = // 
        MultipartEntityBuilder.create().setMode(// 
        HttpMultipartMode.BROWSER_COMPATIBLE).addTextBody("apikey", traits.getApiKey(), // 
        MULTIPART_FORM_DATA).addBinaryBody("chains", getChainFile(), XML, "chains.xml");
        TextCorpusStored textCorpus;
        switch(traits.getChainInputFormat()) {
            case PLAIN_TEXT:
                {
                    // Create TextCorpus object, specifying the language set in the recommender,
                    // although it doesn't really matter here because we only send plain text to
                    // WebLicht, so it won't ever see the language.
                    textCorpus = new TextCorpusStored(traits.getChainInputLanguage());
                    // Create text annotation layer and add the string of the text into the layer
                    textCorpus.createTextLayer().addText(aCas.getDocumentText());
                    builder.addBinaryBody("content", aCas.getDocumentText().getBytes(UTF_8), TEXT, "content.txt");
                    // Copy the tokens because we will clear the CAS later but then we want to restore
                    // the original tokens...
                    new DKPro2Tcf().writeTokens(aCas.getJCas(), textCorpus);
                    break;
                }
            case TCF:
                // Create TextCorpus object, specifying the language set in the recommender
                textCorpus = new TextCorpusStored(traits.getChainInputLanguage());
                // Create text annotation layer and add the string of the text into the layer
                textCorpus.createTextLayer().addText(aCas.getDocumentText());
                // Convert tokens and sentences and leave the rest to the chain
                DKPro2Tcf dkpro2tcf = new DKPro2Tcf();
                Map<Integer, eu.clarin.weblicht.wlfxb.tc.api.Token> tokensBeginPositionMap;
                tokensBeginPositionMap = dkpro2tcf.writeTokens(aCas.getJCas(), textCorpus);
                dkpro2tcf.writeSentence(aCas.getJCas(), textCorpus, tokensBeginPositionMap);
                // write the annotated data object into the output stream
                byte[] bodyData;
                try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
                    WLData wldata = new WLData(textCorpus);
                    WLDObjector.write(wldata, os);
                    bodyData = os.toByteArray();
                }
                builder.addBinaryBody("content", bodyData, TCF, "content.tcf");
                break;
            default:
                throw new IllegalArgumentException("Unknown format [" + traits.getChainInputFormat() + "]");
        }
        HttpUriRequest request = // 
        RequestBuilder.post(traits.getUrl()).addHeader("Accept", "*/*").setEntity(// 
        builder.build()).build();
        HttpResponse response = sendRequest(request);
        // then it does not make sense to go on and try to decode the XMI
        if (response.getStatusLine().getStatusCode() >= 400) {
            int code = response.getStatusLine().getStatusCode();
            String responseBody = getResponseBody(response);
            String msg = format("Request was not successful: [%d] - [%s]", code, responseBody);
            throw new RecommendationException(msg);
        }
        aCas.reset();
        aCas.setDocumentText(documentText);
        aCas.setDocumentLanguage(documentLanguage);
        WLData wldata = deserializePredictionResponse(response);
        new Tcf2DKPro().convert(wldata.getTextCorpus(), aCas.getJCas());
        // Drop the tokens we got from the remote service since their boundaries might not
        // match ours. Then let's just re-add the tokens that we originally sent. We need the
        // tokens later when extracting the predicted annotations
        select(aCas, getType(aCas, Token.class)).forEach(aCas::removeFsFromIndexes);
        new Tcf2DKPro().convertTokens(aCas.getJCas(), textCorpus);
        // Mark predicted results
        Feature isPredictionFeature = getIsPredictionFeature(aCas);
        for (AnnotationFS predictedAnnotation : select(aCas, getPredictedType(aCas))) {
            predictedAnnotation.setBooleanValue(isPredictionFeature, true);
        }
    } catch (Exception e) {
        throw new RecommendationException("Cannot predict", e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) TextCorpusStored(eu.clarin.weblicht.wlfxb.tc.xb.TextCorpusStored) HttpResponse(org.apache.http.HttpResponse) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Feature(org.apache.uima.cas.Feature) IOException(java.io.IOException) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Tcf2DKPro(de.tudarmstadt.ukp.inception.recommendation.imls.weblicht.converter.Tcf2DKPro) WLData(eu.clarin.weblicht.wlfxb.xb.WLData) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) DKPro2Tcf(de.tudarmstadt.ukp.inception.recommendation.imls.weblicht.converter.DKPro2Tcf)

Example 5 with RecommendationException

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

the class ElgRecommender method predict.

@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException {
    ElgServiceResponse response;
    try {
        response = elgService.invokeService(session, traits.getServiceUrlSync(), aCas);
    } catch (IOException e) {
        throw new RecommendationException("Error invoking ELG service: " + ExceptionUtils.getRootCauseMessage(e), e);
    }
    Type predictedType = getPredictedType(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);
    Feature explanationFeature = getScoreExplanationFeature(aCas);
    for (Entry<String, List<ElgAnnotation>> group : getAnnotationGroups(response).entrySet()) {
        String tag = group.getKey();
        for (ElgAnnotation elgAnn : group.getValue()) {
            AnnotationFS ann = aCas.createAnnotation(predictedType, elgAnn.getStart(), elgAnn.getEnd());
            ann.setStringValue(predictedFeature, tag);
            ann.setBooleanValue(isPredictionFeature, true);
            if (!elgAnn.getFeatures().isEmpty()) {
                try {
                    ann.setStringValue(explanationFeature, JSONUtil.toPrettyJsonString(elgAnn.getFeatures()));
                } catch (CASRuntimeException | IOException e) {
                    ann.setStringValue(explanationFeature, "Unable to display ELG annotation features: " + getRootCauseMessage(e));
                    log.error("Unable to display ELG annotation features", e);
                }
            }
            aCas.addFsToIndexes(ann);
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CASRuntimeException(org.apache.uima.cas.CASRuntimeException) ElgServiceResponse(de.tudarmstadt.ukp.inception.recommendation.imls.elg.model.ElgServiceResponse) ElgAnnotation(de.tudarmstadt.ukp.inception.recommendation.imls.elg.model.ElgAnnotation) List(java.util.List) IOException(java.io.IOException) Feature(org.apache.uima.cas.Feature) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)

Aggregations

RecommendationException (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)31 Feature (org.apache.uima.cas.Feature)24 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)24 Type (org.apache.uima.cas.Type)22 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)16 Range (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Range)15 IOException (java.io.IOException)12 List (java.util.List)9 ArrayList (java.util.ArrayList)8 CAS (org.apache.uima.cas.CAS)8 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)7 EvaluationResult (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult)7 Recommender (de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender)7 RecommendationEngine (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 LabelPair (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair)6 RecommenderContext (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext)6 DataSplitter (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter)5 Map (java.util.Map)5