Search in sources :

Example 11 with RelationAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter in project webanno by webanno.

the class CasMerge method mergeRelationAnnotation.

public CasMergeOperationResult mergeRelationAnnotation(SourceDocument aDocument, String aUsername, AnnotationLayer aAnnotationLayer, CAS aTargetCas, AnnotationFS aSourceFs, boolean aAllowStacking) throws AnnotationException {
    RelationAdapter relationAdapter = (RelationAdapter) adapterCache.get(aAnnotationLayer);
    if (silenceEvents) {
        relationAdapter.silenceEvents();
    }
    if (existsEquivalentAt(aTargetCas, relationAdapter, aSourceFs)) {
        throw new AlreadyMergedException("The annotation already exists in the target document.");
    }
    AnnotationFS originFsClicked = getFeature(aSourceFs, relationAdapter.getSourceFeatureName(), AnnotationFS.class);
    AnnotationFS targetFsClicked = getFeature(aSourceFs, relationAdapter.getTargetFeatureName(), AnnotationFS.class);
    SpanAdapter spanAdapter = (SpanAdapter) adapterCache.get(aAnnotationLayer.getAttachType());
    List<AnnotationFS> candidateOrigins = getCandidateAnnotations(aTargetCas, spanAdapter, originFsClicked);
    List<AnnotationFS> candidateTargets = getCandidateAnnotations(aTargetCas, spanAdapter, targetFsClicked);
    // check if target/source exists in the mergeview
    if (candidateOrigins.isEmpty() || candidateTargets.isEmpty()) {
        throw new UnfulfilledPrerequisitesException("Both the source and target annotation" + " must exist in the target document. Please first merge/create them");
    }
    if (candidateOrigins.size() > 1) {
        throw new MergeConflictException("Stacked sources exist in the target document. Cannot merge this relation.");
    }
    if (candidateTargets.size() > 1) {
        throw new MergeConflictException("Stacked targets exist in the target document. Cannot merge this relation.");
    }
    AnnotationFS originFs = candidateOrigins.get(0);
    AnnotationFS targetFs = candidateTargets.get(0);
    if (relationAdapter.getAttachFeatureName() != null) {
        AnnotationFS originAttachAnnotation = FSUtil.getFeature(originFs, relationAdapter.getAttachFeatureName(), AnnotationFS.class);
        AnnotationFS targetAttachAnnotation = FSUtil.getFeature(targetFs, relationAdapter.getAttachFeatureName(), AnnotationFS.class);
        if (originAttachAnnotation == null || targetAttachAnnotation == null) {
            throw new UnfulfilledPrerequisitesException("No annotation to attach to. Cannot merge this relation.");
        }
    }
    List<AnnotationFS> existingAnnos = selectCandidateRelationsAt(aTargetCas, aSourceFs, originFs, targetFs);
    if (existingAnnos.isEmpty() || aAllowStacking) {
        AnnotationFS mergedRelation = relationAdapter.add(aDocument, aUsername, originFs, targetFs, aTargetCas);
        try {
            copyFeatures(aDocument, aUsername, relationAdapter, mergedRelation, aSourceFs);
        } catch (AnnotationException e) {
            // If there was an error while setting the features, then we skip the entire
            // annotation
            relationAdapter.delete(aDocument, aUsername, aTargetCas, new VID(mergedRelation));
        }
        return new CasMergeOperationResult(CasMergeOperationResult.ResultState.CREATED, getAddr(mergedRelation));
    } else {
        AnnotationFS mergeTargetFS = existingAnnos.get(0);
        copyFeatures(aDocument, aUsername, relationAdapter, mergeTargetFS, aSourceFs);
        return new CasMergeOperationResult(CasMergeOperationResult.ResultState.UPDATED, getAddr(mergeTargetFS));
    }
}
Also used : VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)

Example 12 with RelationAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter in project webanno by webanno.

the class CasDiff method getDiffAdapters.

public static List<DiffAdapter> getDiffAdapters(AnnotationSchemaService schemaService, Iterable<AnnotationLayer> aLayers) {
    List<DiffAdapter> adapters = new ArrayList<>();
    nextLayer: for (AnnotationLayer layer : aLayers) {
        if (!layer.isEnabled()) {
            continue nextLayer;
        }
        Set<String> labelFeatures = new LinkedHashSet<>();
        nextFeature: for (AnnotationFeature f : schemaService.listSupportedFeatures(layer)) {
            if (!f.isEnabled()) {
                continue nextFeature;
            }
            // Link features are treated separately from primitive label features
            if (!NONE.equals(f.getLinkMode())) {
                continue nextFeature;
            }
            labelFeatures.add(f.getName());
        }
        DiffAdapter_ImplBase adapter;
        switch(layer.getType()) {
            case SPAN_TYPE:
                {
                    adapter = new SpanDiffAdapter(layer.getName(), labelFeatures);
                    break;
                }
            case RELATION_TYPE:
                {
                    RelationAdapter typeAdpt = (RelationAdapter) schemaService.getAdapter(layer);
                    adapter = new RelationDiffAdapter(layer.getName(), typeAdpt.getSourceFeatureName(), typeAdpt.getTargetFeatureName(), labelFeatures);
                    break;
                }
            default:
                LOG.debug("Curation for layer type [{}] not supported - ignoring", layer.getType());
                continue nextLayer;
        }
        adapters.add(adapter);
        nextFeature: for (AnnotationFeature f : schemaService.listSupportedFeatures(layer)) {
            if (!f.isEnabled()) {
                continue nextFeature;
            }
            switch(f.getLinkMode()) {
                case NONE:
                    // Nothing to do here
                    break;
                case SIMPLE:
                    adapter.addLinkFeature(f.getName(), f.getLinkTypeRoleFeatureName(), null);
                    break;
                case WITH_ROLE:
                    adapter.addLinkFeature(f.getName(), f.getLinkTypeRoleFeatureName(), f.getLinkTypeTargetFeatureName());
                    break;
                default:
                    throw new IllegalStateException("Unknown link mode [" + f.getLinkMode() + "]");
            }
            labelFeatures.add(f.getName());
        }
    }
    return adapters;
}
Also used : RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.span.SpanDiffAdapter) RelationDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.relation.RelationDiffAdapter) ArrayList(java.util.ArrayList) SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.span.SpanDiffAdapter) RelationDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.relation.RelationDiffAdapter) DiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.api.DiffAdapter) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) DiffAdapter_ImplBase(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.api.DiffAdapter_ImplBase) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 13 with RelationAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter in project webanno by webanno.

the class AutomationUtil method repeatRelationAnnotation.

public static void repeatRelationAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
        CAS cas = aCorrectionDocumentService.readCorrectionCas(d);
        RelationAdapter adapter = (RelationAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
        String sourceFName = adapter.getSourceFeatureName();
        String targetFName = adapter.getTargetFeatureName();
        Type type = getType(cas, aFeature.getLayer().getName());
        Type spanType = getType(cas, adapter.getAttachTypeName());
        Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
        Feature dependentFeature = type.getFeatureByBaseName(targetFName);
        Feature governorFeature = type.getFeatureByBaseName(sourceFName);
        AnnotationFS dependentFs = null;
        AnnotationFS governorFs = null;
        if (adapter.getAttachFeatureName() != null) {
            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 (adapter.getLayer().isCrossSentence()) {
            List<AnnotationFS> mSpanAnnos = new ArrayList<>(getAllAnnoFss(cas, governorFs.getType()));
            repeatRelation(aState, 0, cas.getDocumentText().length() - 1, aFeature, aValue, cas, adapter, dependentFs, governorFs, mSpanAnnos);
        } else {
            for (AnnotationFS sent : selectSentences(cas)) {
                List<AnnotationFS> spanAnnos = selectCovered(governorFs.getType(), sent);
                repeatRelation(aState, sent.getBegin(), sent.getEnd(), aFeature, aValue, cas, adapter, dependentFs, governorFs, spanAnnos);
            }
        }
        aCorrectionDocumentService.writeCorrectionCas(cas, d);
    }
}
Also used : RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) CAS(org.apache.uima.cas.CAS) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 14 with RelationAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter in project webanno by webanno.

the class AutomationUtil method deleteRelationAnnotation.

public static void deleteRelationAnnotation(AnnotatorState aBModel, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    for (SourceDocument d : aDocumentService.listSourceDocuments(aBModel.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aBModel.getUser());
        CAS cas = aCorrectionDocumentService.readCorrectionCas(d);
        RelationAdapter adapter = (RelationAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
        String sourceFName = adapter.getSourceFeatureName();
        String targetFName = adapter.getTargetFeatureName();
        Type type = getType(cas, aFeature.getLayer().getName());
        Type spanType = getType(cas, adapter.getAttachTypeName());
        Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
        Feature dependentFeature = type.getFeatureByBaseName(targetFName);
        Feature governorFeature = type.getFeatureByBaseName(sourceFName);
        AnnotationFS dependentFs = null;
        AnnotationFS governorFs = null;
        if (adapter.getAttachFeatureName() != null) {
            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);
        }
        int beginOffset = 0;
        int endOffset = cas.getDocumentText().length() - 1;
        String depCoveredText = dependentFs.getCoveredText();
        String govCoveredText = governorFs.getCoveredText();
        deleteRelationAnnotation(adapter, aBModel.getDocument(), aBModel.getUser().getUsername(), cas, aFeature, beginOffset, endOffset, depCoveredText, govCoveredText, aValue);
        aCorrectionDocumentService.writeCorrectionCas(cas, d);
    }
}
Also used : RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) CAS(org.apache.uima.cas.CAS) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

RelationAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter)14 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)11 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)7 Type (org.apache.uima.cas.Type)7 ArrayList (java.util.ArrayList)6 Feature (org.apache.uima.cas.Feature)6 CAS (org.apache.uima.cas.CAS)5 FeatureStructure (org.apache.uima.cas.FeatureStructure)5 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)4 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)3 VComment (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)3 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)3 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)3 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)3 Set (java.util.Set)3 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)3 AttachedAnnotation (de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation)2 RelationCrossSentenceBehavior (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationCrossSentenceBehavior)2 RelationLayerBehavior (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior)2 FeatureSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry)2