Search in sources :

Example 11 with AnnotationException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.

the class ArcAdapter method interalAddToCas.

/**
 * @param aWindowBegin
 *            begin offset of the first visible sentence
 * @param aWindowEnd
 *            end offset of the last visible sentence
 */
private AnnotationFS interalAddToCas(JCas aJCas, int aWindowBegin, int aWindowEnd, AnnotationFS aOriginFs, AnnotationFS aTargetFs) throws AnnotationException {
    Type type = getType(aJCas.getCas(), annotationTypeName);
    Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
    Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);
    Type spanType = getType(aJCas.getCas(), attachType);
    AnnotationFS dependentFs = null;
    AnnotationFS governorFs = null;
    // presently visible on screen is sufficient - we don't have to scan the whole CAS.
    for (AnnotationFS fs : selectCovered(aJCas.getCas(), type, aWindowBegin, aWindowEnd)) {
        if (attachFeatureName != null) {
            Feature arcSpanFeature = spanType.getFeatureByBaseName(attachFeatureName);
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        } else {
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
        }
        if (dependentFs == null || governorFs == null) {
            log.warn("Relation [" + getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - ignoring during while checking for duplicates.");
            continue;
        }
        // update the label of the existing arc
        if (!allowStacking && isDuplicate(governorFs, aOriginFs, dependentFs, aTargetFs)) {
            throw new AnnotationException("Cannot create another annotation of layer [" + getLayer().getUiName() + "] at this location - stacking is not " + "enabled for this layer.");
        }
    }
    // It is new ARC annotation, create it
    dependentFs = aTargetFs;
    governorFs = aOriginFs;
    // for POS annotation, since custom span layers do not have attach feature
    if (attachFeatureName != null) {
        dependentFs = selectCovered(aJCas.getCas(), spanType, dependentFs.getBegin(), dependentFs.getEnd()).get(0);
        governorFs = selectCovered(aJCas.getCas(), spanType, governorFs.getBegin(), governorFs.getEnd()).get(0);
    }
    if (dependentFs == null || governorFs == null) {
        throw new AnnotationException("Relation must have a source and a target!");
    }
    // Set the relation offsets in DKPro Core style - the relation recieves the offsets from
    // the dependent
    // If origin and target spans are multiple tokens, dependentFS.getBegin will be the
    // the begin position of the first token and dependentFS.getEnd will be the End
    // position of the last token.
    AnnotationFS newAnnotation = aJCas.getCas().createAnnotation(type, dependentFs.getBegin(), dependentFs.getEnd());
    newAnnotation.setFeatureValue(dependentFeature, dependentFs);
    newAnnotation.setFeatureValue(governorFeature, governorFs);
    aJCas.addFsToIndexes(newAnnotation);
    return newAnnotation;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 12 with AnnotationException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.

the class SpanAdapter method createAnnotation.

/**
 * A Helper method to add annotation to CAS
 */
private Integer createAnnotation(AnnotatorState aState, CAS aCas, int aBegin, int aEnd) throws AnnotationException {
    // If stacking is not allowed and there already is an annotation, then return the address
    // of the existing annotation.
    Type type = CasUtil.getType(aCas, getAnnotationTypeName());
    for (AnnotationFS fs : CasUtil.selectCovered(aCas, type, aBegin, aEnd)) {
        if (fs.getBegin() == aBegin && fs.getEnd() == aEnd) {
            if (!allowStacking) {
                return getAddr(fs);
            }
        }
    }
    AnnotationFS newAnnotation = aCas.createAnnotation(type, aBegin, aEnd);
    // created annotation.
    if (getAttachFeatureName() != null) {
        Type theType = CasUtil.getType(aCas, getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (CasUtil.selectCovered(aCas, theType, aBegin, aEnd).isEmpty()) {
            throw new AnnotationException("No annotation of type [" + getAttachTypeName() + "] to attach to at location [" + aBegin + "-" + aEnd + "].");
        }
        CasUtil.selectCovered(aCas, theType, aBegin, aEnd).get(0).setFeatureValue(attachFeature, newAnnotation);
    }
    aCas.addFsToIndexes(newAnnotation);
    publishEvent(new SpanCreatedEvent(this, aState.getDocument(), aState.getUser().getUsername(), newAnnotation));
    return getAddr(newAnnotation);
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) SpanCreatedEvent(de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanCreatedEvent)

Example 13 with AnnotationException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.

the class AnnotationDetailEditorPanel method loadFeatureEditorModels.

public void loadFeatureEditorModels(JCas aJCas, AjaxRequestTarget aTarget) throws AnnotationException {
    LOG.trace("loadFeatureEditorModels()");
    AnnotatorState state = getModelObject();
    Selection selection = state.getSelection();
    List<FeatureState> featureStates = state.getFeatureStates();
    for (FeatureState featureState : featureStates) {
        if (StringUtils.isNotBlank(featureState.feature.getLinkTypeName())) {
            featureState.value = new ArrayList<>();
        }
    }
    try {
        if (selection.isSpan()) {
            annotationFeatureForm.updateLayersDropdown();
        }
        if (selection.getAnnotation().isSet()) {
            // If an existing annotation was selected, take the feature editor model values from
            // there
            AnnotationFS annoFs = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
            // Try obtaining the layer from the feature structure
            AnnotationLayer layer;
            try {
                layer = annotationService.getLayer(state.getProject(), annoFs);
                state.setSelectedAnnotationLayer(layer);
                LOG.trace(String.format("loadFeatureEditorModels() selectedLayer set from selection: %s", state.getSelectedAnnotationLayer().getUiName()));
            } catch (NoResultException e) {
                clearFeatureEditorModels(aTarget);
                throw new IllegalStateException("Unknown layer [" + annoFs.getType().getName() + "]", e);
            }
            // selected span annotation
            if (!selection.isArc() && !state.getPreferences().isRememberLayer()) {
                state.setSelectedAnnotationLayer(layer);
            }
            loadFeatureEditorModelsCommon(aTarget, aJCas, layer, annoFs, null);
        } else {
            if (selection.isArc()) {
                // Avoid creation of arcs on locked layers
                if (state.getSelectedAnnotationLayer() != null && state.getSelectedAnnotationLayer().isReadonly()) {
                    state.setSelectedAnnotationLayer(new AnnotationLayer());
                } else {
                    loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedArcFeatures());
                }
            } else {
                loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedSpanFeatures());
            }
        }
        annotationFeatureForm.updateRememberLayer();
        if (aTarget != null) {
            aTarget.add(annotationFeatureForm);
        }
    } catch (Exception e) {
        throw new AnnotationException(e);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) NoResultException(javax.persistence.NoResultException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 14 with AnnotationException

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException in project webanno by webanno.

the class MergeCas method addSlotArcAnnotation.

private static void addSlotArcAnnotation(SpanAdapter aAdapter, JCas aJcas, String aFSArcaddress, JCas aClickedJCas, AnnotationFS aClickedFS) throws AnnotationException {
    List<AnnotationFS> merges = MergeCas.getMergeFS(aClickedFS, aJcas).collect(Collectors.toList());
    AnnotationFS targetFs;
    if (merges.size() == 0) {
        throw new AnnotationException("The base annotation do not exist. Please add it first. ");
    }
    AnnotationFS mergeFs = merges.get(0);
    int fiIndex = Integer.parseInt(aFSArcaddress.split("\\.")[1]);
    int liIndex = Integer.parseInt(aFSArcaddress.split("\\.")[2]);
    AnnotationFeature slotFeature = null;
    LinkWithRoleModel linkRole = null;
    int fi = 0;
    f: for (AnnotationFeature feat : aAdapter.listFeatures()) {
        if (MultiValueMode.ARRAY.equals(feat.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(feat.getLinkMode())) {
            List<LinkWithRoleModel> links = aAdapter.getFeatureValue(feat, aClickedFS);
            for (int li = 0; li < links.size(); li++) {
                LinkWithRoleModel link = links.get(li);
                if (fi == fiIndex && li == liIndex) {
                    slotFeature = feat;
                    List<AnnotationFS> targets = checkAndGetTargets(aJcas, aClickedJCas, selectByAddr(aClickedJCas, link.targetAddr));
                    targetFs = targets.get(0);
                    link.targetAddr = getAddr(targetFs);
                    linkRole = link;
                    break f;
                }
            }
        }
        fi++;
    }
    List<LinkWithRoleModel> links = aAdapter.getFeatureValue(slotFeature, mergeFs);
    // 
    LinkWithRoleModel duplicateLink = null;
    for (LinkWithRoleModel lr : links) {
        if (lr.targetAddr == linkRole.targetAddr) {
            duplicateLink = lr;
            break;
        }
    }
    links.add(linkRole);
    links.remove(duplicateLink);
    setFeature(mergeFs, slotFeature, links);
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) LinkedList(java.util.LinkedList) List(java.util.List) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)14 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)11 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)7 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)6 IOException (java.io.IOException)6 UIMAException (org.apache.uima.UIMAException)6 JCas (org.apache.uima.jcas.JCas)6 NoResultException (javax.persistence.NoResultException)5 Feature (org.apache.uima.cas.Feature)5 Type (org.apache.uima.cas.Type)5 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)4 FeatureState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)4 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)3 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)3 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)3 WebAnnoCasUtil.setFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature)3 AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)2 DocumentService (de.tudarmstadt.ukp.clarin.webanno.api.DocumentService)2 ProjectService (de.tudarmstadt.ukp.clarin.webanno.api.ProjectService)2