Search in sources :

Example 1 with AnnotationComparator

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator in project webanno by webanno.

the class SpanOverlapBehavior method onRender.

@Override
public void onRender(TypeAdapter aAdapter, VDocument aResponse, Map<AnnotationFS, VSpan> aAnnoToSpanIdx, int aPageBegin, int aPageEnd) {
    if (aAnnoToSpanIdx.isEmpty()) {
        return;
    }
    // The following code requires annotations with the same offsets to be adjacent during
    // iteration, so we sort the entries here
    AnnotationComparator cmp = new AnnotationComparator();
    final List<AnnotationFS> sortedSpans = aAnnoToSpanIdx.keySet().stream().sorted(cmp).collect(Collectors.toList());
    switch(aAdapter.getLayer().getOverlapMode()) {
        case ANY_OVERLAP:
            // Nothing to check
            break;
        case NO_OVERLAP:
            {
                Set<AnnotationFS> overlapping = new HashSet<>();
                Set<AnnotationFS> stacking = new HashSet<>();
                overlappingOrStackingSpans(sortedSpans, stacking, overlapping);
                overlapping.forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Overlap is not permitted.")));
                stacking.forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
                break;
            }
        case STACKING_ONLY:
            // Here, we must find all overlapping relations because they are not permitted
            overlappingNonStackingSpans(sortedSpans).forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Only stacking is permitted.")));
            break;
        case OVERLAP_ONLY:
            stackingSpans(sortedSpans).forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
            break;
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) LogMessage(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage) CAS(org.apache.uima.cas.CAS) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) WebAnnoCasUtil.selectOverlapping(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectOverlapping) HashSet(java.util.HashSet) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) CasUtil.select(org.apache.uima.fit.util.CasUtil.select) CasUtil.selectAt(org.apache.uima.fit.util.CasUtil.selectAt) Collection(java.util.Collection) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ERROR(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR) Set(java.util.Set) LayerSupport(de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupport) Collectors(java.util.stream.Collectors) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) List(java.util.List) Component(org.springframework.stereotype.Component) ChainLayerSupport(de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.ChainLayerSupport) OverlapMode(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) HashSet(java.util.HashSet) Set(java.util.Set) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)

Example 2 with AnnotationComparator

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator in project webanno by webanno.

the class ChainAdapter method addArc.

public int addArc(SourceDocument aDocument, String aUsername, CAS aCas, AnnotationFS aOriginFs, AnnotationFS aTargetFs) {
    // Determine if the links are adjacent. If so, just update the arc label
    AnnotationFS originNext = getNextLink(aOriginFs);
    AnnotationFS targetNext = getNextLink(aTargetFs);
    // adjacent - origin links to target
    if (WebAnnoCasUtil.isSame(originNext, aTargetFs)) {
    } else // adjacent - target links to origin
    if (WebAnnoCasUtil.isSame(targetNext, aOriginFs)) {
        if (isLinkedListBehavior()) {
            throw new IllegalStateException("Cannot change direction of a link within a chain");
        } else {
        // in set mode there are no arc labels anyway
        }
    } else // if origin and target are not adjacent
    {
        FeatureStructure originChain = getChainForLink(aCas, aOriginFs);
        FeatureStructure targetChain = getChainForLink(aCas, aTargetFs);
        AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs);
        if (!WebAnnoCasUtil.isSame(originChain, targetChain)) {
            if (isLinkedListBehavior()) {
                // the rest becomes its own chain
                if (originNext != null) {
                    newChain(aCas, originNext);
                // we set originNext below
                // we set the arc label below
                }
                // if targetFs has a prev, then split it off
                if (targetPrev != null) {
                    setNextLink(targetPrev, null);
                } else // if it has no prev then we fully append the target chain to the origin chain
                // and we can remove the target chain head
                {
                    aCas.removeFsFromIndexes(targetChain);
                }
                // connect the rest of the target chain to the origin chain
                setNextLink(aOriginFs, aTargetFs);
            } else {
                // collect all the links
                List<AnnotationFS> links = new ArrayList<>();
                links.addAll(collectLinks(originChain));
                links.addAll(collectLinks(targetChain));
                // sort them ascending by begin and descending by end (default UIMA order)
                links.sort(new AnnotationComparator());
                // thread them
                AnnotationFS prev = null;
                for (AnnotationFS link : links) {
                    if (prev != null) {
                        // Set next link
                        setNextLink(prev, link);
                    // // Clear arc label - it makes no sense in this mode
                    // setLabel(prev, aFeature, null);
                    }
                    prev = link;
                }
                // make sure the last link terminates the chain
                setNextLink(links.get(links.size() - 1), null);
                // the chain head needs to point to the first link
                setFirstLink(originChain, links.get(0));
                // we don't need the second chain head anymore
                aCas.removeFsFromIndexes(targetChain);
            }
        } else {
            // if the two links are in the same chain, we just ignore the action
            if (isLinkedListBehavior()) {
                throw new IllegalStateException("Cannot connect two spans that are already part of the same chain");
            }
        }
    }
    publishEvent(new ChainLinkCreatedEvent(this, aDocument, aUsername, getLayer(), aOriginFs));
    // We do not actually create a new FS for the arc. Features are set on the originFS.
    return WebAnnoCasUtil.getAddr(aOriginFs);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) ChainLinkCreatedEvent(de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.ChainLinkCreatedEvent) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 3 with AnnotationComparator

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator in project webanno by webanno.

the class RelationOverlapBehavior method onRender.

@Override
public void onRender(TypeAdapter aAdapter, VDocument aResponse, Map<AnnotationFS, VArc> aAnnoToArcIdx) {
    if (aAnnoToArcIdx.isEmpty()) {
        return;
    }
    final AnnotationLayer layer = aAdapter.getLayer();
    final RelationAdapter adapter = (RelationAdapter) aAdapter;
    final CAS cas = aAnnoToArcIdx.keySet().iterator().next().getCAS();
    final Type type = getType(cas, adapter.getAnnotationTypeName());
    final Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
    final Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
    AnnotationComparator cmp = new AnnotationComparator();
    final List<AnnotationFS> sortedRelations = aAnnoToArcIdx.keySet().stream().sorted(cmp).collect(Collectors.toList());
    switch(layer.getOverlapMode()) {
        case ANY_OVERLAP:
            // Nothing to check
            break;
        case NO_OVERLAP:
            {
                Set<AnnotationFS> overlapping = new HashSet<>();
                Set<AnnotationFS> stacking = new HashSet<>();
                overlappingOrStackingRelations(sortedRelations, sourceFeature, targetFeature, stacking, overlapping);
                overlapping.forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Overlap is not permitted.")));
                stacking.forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
                break;
            }
        case STACKING_ONLY:
            {
                // Here, we must find all overlapping relations because they are not permitted
                overlappingNonStackingRelations(sortedRelations, sourceFeature, targetFeature).forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Only stacking is permitted.")));
                break;
            }
        case OVERLAP_ONLY:
            // Here, we must find all stacked relations because they are not permitted.
            // We go through all relations based on the their offsets. Stacked relations must have
            // the same offsets (at least if we consider relations as having a direction, i.e.
            // that a relation A->B does not count as stacked on a relation B->A). But since there
            // can be multiple relations going out from the same sourceFS, we need to consider all
            // of them for potential stacking.
            stackingRelations(sortedRelations, sourceFeature, targetFeature).forEach(fs -> aResponse.add(new VComment(new VID(fs), ERROR, "Stacking is not permitted.")));
            break;
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) LogMessage(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) HashSet(java.util.HashSet) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) WebAnnoCasUtil.isSame(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.isSame) FeatureStructure(org.apache.uima.cas.FeatureStructure) Collections.emptyList(java.util.Collections.emptyList) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) CasUtil.select(org.apache.uima.fit.util.CasUtil.select) Collection(java.util.Collection) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ERROR(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) OverlapMode(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) IllegalPlacementException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException) HashSet(java.util.HashSet) Set(java.util.Set) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Feature(org.apache.uima.cas.Feature) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) 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)

Aggregations

AnnotationComparator (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)3 WebAnnoConst (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst)2 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)2 IllegalPlacementException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.IllegalPlacementException)2 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)2 VComment (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)2 ERROR (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR)2 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)2 OverlapMode (de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode)2 LogMessage (de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage)2 Collection (java.util.Collection)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Pair (org.apache.commons.lang3.tuple.Pair)2