Search in sources :

Example 1 with ERROR

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR in project webanno by webanno.

the class RelationRendererTest method thatRelationOverlapBehaviorOnRenderGeneratesErrors.

@Test
public void thatRelationOverlapBehaviorOnRenderGeneratesErrors() throws Exception {
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");
    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }
    RelationAdapter adapter = new RelationAdapter(layerSupportRegistry, featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);
    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));
    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(1);
    RelationRenderer sut = new RelationRenderer(adapter, layerSupportRegistry, featureSupportRegistry, asList(new RelationOverlapBehavior()));
    // Create two annotations stacked annotations
    depLayer.setOverlapMode(ANY_OVERLAP);
    AnnotationFS dep1 = adapter.add(document, username, source, target, jcas.getCas());
    AnnotationFS dep2 = adapter.add(document, username, source, target, jcas.getCas());
    {
        depLayer.setOverlapMode(ANY_OVERLAP);
        VDocument vdoc = new VDocument();
        sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
        assertThat(vdoc.comments()).filteredOn(c -> !YIELD.equals(c.getCommentType())).isEmpty();
    }
    {
        depLayer.setOverlapMode(STACKING_ONLY);
        VDocument vdoc = new VDocument();
        sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
        assertThat(vdoc.comments()).filteredOn(c -> !YIELD.equals(c.getCommentType())).isEmpty();
    }
    {
        depLayer.setOverlapMode(OVERLAP_ONLY);
        VDocument vdoc = new VDocument();
        sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
        assertThat(vdoc.comments()).filteredOn(c -> !YIELD.equals(c.getCommentType())).usingFieldByFieldElementComparator().contains(new VComment(dep1, ERROR, "Stacking is not permitted."), new VComment(dep2, ERROR, "Stacking is not permitted."));
    }
    {
        depLayer.setOverlapMode(NO_OVERLAP);
        VDocument vdoc = new VDocument();
        sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
        assertThat(vdoc.comments()).filteredOn(c -> !YIELD.equals(c.getCommentType())).usingFieldByFieldElementComparator().contains(new VComment(dep1, ERROR, "Stacking is not permitted."), new VComment(dep2, ERROR, "Stacking is not permitted."));
    }
    // Remove the stacked annotation and introduce one that is purely overlapping
    adapter.delete(document, username, jcas.getCas(), new VID(dep2));
    depLayer.setOverlapMode(ANY_OVERLAP);
    AnnotationFS dep3 = adapter.add(document, username, source, posAnnotations.get(2), jcas.getCas());
    {
        depLayer.setOverlapMode(NO_OVERLAP);
        VDocument vdoc = new VDocument();
        sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
        assertThat(vdoc.comments()).filteredOn(c -> !YIELD.equals(c.getCommentType())).usingFieldByFieldElementComparator().contains(new VComment(dep1, ERROR, "Overlap is not permitted."), new VComment(dep3, ERROR, "Overlap is not permitted."));
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) FEAT_REL_SOURCE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_SOURCE) FEAT_REL_TARGET(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_TARGET) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RelationLayerBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationLayerBehavior) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) PROJECT_TYPE_ANNOTATION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_ANNOTATION) TokenBuilder(org.apache.uima.fit.testing.factory.TokenBuilder) OVERLAP_ONLY(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.OVERLAP_ONLY) RelationOverlapBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationOverlapBehavior) ArrayList(java.util.ArrayList) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Arrays.asList(java.util.Arrays.asList) LayerSupportRegistryImpl(de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistryImpl) JCasFactory(org.apache.uima.fit.factory.JCasFactory) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) LayerSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistry) SINGLE_TOKEN(de.tudarmstadt.ukp.clarin.webanno.model.AnchoringMode.SINGLE_TOKEN) ANY_OVERLAP(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.ANY_OVERLAP) Before(org.junit.Before) JCas(org.apache.uima.jcas.JCas) RelationCrossSentenceBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationCrossSentenceBehavior) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) NO_OVERLAP(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.NO_OVERLAP) RelationAttachmentBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAttachmentBehavior) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ERROR(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR) YIELD(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.YIELD) Test(org.junit.Test) RELATION_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.RELATION_TYPE) SPAN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE) RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) STACKING_ONLY(de.tudarmstadt.ukp.clarin.webanno.model.OverlapMode.STACKING_ONLY) List(java.util.List) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) JCasUtil.select(org.apache.uima.fit.util.JCasUtil.select) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) FeatureSupportRegistryImpl(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistryImpl) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) TokenBuilder(org.apache.uima.fit.testing.factory.TokenBuilder) RelationAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationAdapter) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) ArrayList(java.util.ArrayList) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) RelationOverlapBehavior(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.RelationOverlapBehavior) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) Test(org.junit.Test)

Example 2 with ERROR

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR 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 3 with ERROR

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR 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

VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)3 VComment (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)3 ERROR (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType.ERROR)3 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)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 AnnotationComparator (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator)2 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)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 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