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);
}
}
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);
}
}
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);
}
}
}
}
}
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);
}
}
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);
}
}
}
Aggregations