Search in sources :

Example 21 with AnnotationLayer

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer in project webanno by webanno.

the class FeatureAttachedSpanAnnotationsTrulyAttachedCheck method check.

@Override
public boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages) {
    boolean ok = true;
    for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
        if (!(WebAnnoConst.SPAN_TYPE.equals(layer.getType()) && layer.getAttachFeature() != null)) {
            continue;
        }
        Type layerType;
        Type attachType;
        try {
            layerType = getType(aCas, layer.getName());
            attachType = getType(aCas, layer.getAttachType().getName());
        } catch (IllegalArgumentException e) {
            // check
            continue;
        }
        for (AnnotationFS anno : select(aCas, layerType)) {
            for (AnnotationFS attach : selectCovered(attachType, anno)) {
                AnnotationFS candidate = getFeature(attach, layer.getAttachFeature().getName(), AnnotationFS.class);
                if (candidate != anno) {
                    aMessages.add(new LogMessage(this, LogLevel.ERROR, "Annotation should be attached to [" + layer.getAttachFeature().getName() + "] but is not.\nAnnotation: [" + anno + "]\nAttach annotation:[" + attach + "]"));
                    ok = false;
                }
            }
        }
    }
    return ok;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 22 with AnnotationLayer

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer in project webanno by webanno.

the class ReattachFeatureAttachedSpanAnnotationsAndDeleteExtrasRepair method repair.

@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages) {
    for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
        if (!(WebAnnoConst.SPAN_TYPE.equals(layer.getType()) && layer.getAttachFeature() != null)) {
            continue;
        }
        int count = 0;
        // attach -> e.g. Token
        for (AnnotationFS anno : select(aCas, getType(aCas, layer.getName()))) {
            for (AnnotationFS attach : selectCovered(getType(aCas, layer.getAttachType().getName()), anno)) {
                AnnotationFS candidate = getFeature(attach, layer.getAttachFeature().getName(), AnnotationFS.class);
                if (candidate == null) {
                    setFeature(attach, layer.getAttachFeature().getName(), anno);
                    count++;
                } else if (candidate != anno) {
                    aMessages.add(new LogMessage(this, LogLevel.ERROR, "Cannot attach annotation because attach feature alread non-null"));
                }
            }
        }
        if (count > 0) {
            aMessages.add(new LogMessage(this, LogLevel.INFO, "Reattached [%d] unattached spans layer [" + layer.getName() + "].", count));
        }
        // Go over the layer that is being attached to (e.g. Lemma) and ensure that if there
        // only exactly one annotation for each annotation in the layer that has the attach
        // feature (e.g. Token) - or short: ensure that there are not multiple Lemmas for a
        // single Token because such a thing is not valid in WebAnno. Layers that have an
        // attach feature cannot have stacking enabled!
        // 
        // attach     -> e.g. Token
        // candidates -> e.g. Lemma
        List<AnnotationFS> toDelete = new ArrayList<>();
        for (AnnotationFS attach : select(aCas, getType(aCas, layer.getAttachType().getName()))) {
            List<AnnotationFS> candidates = selectCovered(getType(aCas, layer.getName()), attach);
            if (!candidates.isEmpty()) {
                // One of the candidates should already be attached
                AnnotationFS attachedCandidate = getFeature(attach, layer.getAttachFeature().getName(), AnnotationFS.class);
                for (AnnotationFS candidate : candidates) {
                    if (candidate != attachedCandidate) {
                        toDelete.add(candidate);
                    }
                }
            }
        }
        // Delete those the extra candidates that are not properly attached
        if (!toDelete.isEmpty()) {
            toDelete.forEach(aCas::removeFsFromIndexes);
            aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed [%d] unattached stacked candidates [" + layer.getName() + "].", toDelete.size()));
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) ArrayList(java.util.ArrayList) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 23 with AnnotationLayer

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer in project webanno by webanno.

the class ReattachFeatureAttachedSpanAnnotationsRepair method repair.

@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages) {
    for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
        if (!(WebAnnoConst.SPAN_TYPE.equals(layer.getType()) && layer.getAttachFeature() != null)) {
            continue;
        }
        int count = 0;
        // attach -> e.g. Token
        for (AnnotationFS anno : select(aCas, getType(aCas, layer.getName()))) {
            for (AnnotationFS attach : selectCovered(getType(aCas, layer.getAttachType().getName()), anno)) {
                AnnotationFS candidate = getFeature(attach, layer.getAttachFeature().getName(), AnnotationFS.class);
                if (candidate == null) {
                    setFeature(attach, layer.getAttachFeature().getName(), anno);
                    count++;
                } else if (candidate != anno) {
                    aMessages.add(new LogMessage(this, LogLevel.ERROR, "Cannot attach annotation because attach feature alread non-null"));
                }
            }
        }
        if (count > 0) {
            aMessages.add(new LogMessage(this, LogLevel.INFO, "Reattached [%d] unattached spans on layer [" + layer.getName() + "].", count));
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 24 with AnnotationLayer

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer in project webanno by webanno.

the class AnnotationSchemaServiceImpl method getProjectTypes.

@Override
public TypeSystemDescription getProjectTypes(Project aProject) {
    // Create a new type system from scratch
    TypeSystemDescription tsd = new TypeSystemDescription_impl();
    for (AnnotationLayer type : listAnnotationLayer(aProject)) {
        if (type.getType().equals(SPAN_TYPE) && !type.isBuiltIn()) {
            TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
            generateFeatures(tsd, td, type);
        } else if (type.getType().equals(RELATION_TYPE) && !type.isBuiltIn()) {
            TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
            AnnotationLayer attachType = type.getAttachType();
            td.addFeature(WebAnnoConst.FEAT_REL_TARGET, "", attachType.getName());
            td.addFeature(WebAnnoConst.FEAT_REL_SOURCE, "", attachType.getName());
            generateFeatures(tsd, td, type);
        } else if (type.getType().equals(CHAIN_TYPE) && !type.isBuiltIn()) {
            TypeDescription tdChains = tsd.addType(type.getName() + "Chain", "", CAS.TYPE_NAME_ANNOTATION_BASE);
            tdChains.addFeature("first", "", type.getName() + "Link");
            // Custom features on chain layers are currently not supported
            // generateFeatures(tsd, tdChains, type);
            TypeDescription tdLink = tsd.addType(type.getName() + "Link", "", CAS.TYPE_NAME_ANNOTATION);
            tdLink.addFeature("next", "", type.getName() + "Link");
            tdLink.addFeature("referenceType", "", CAS.TYPE_NAME_STRING);
            tdLink.addFeature("referenceRelation", "", CAS.TYPE_NAME_STRING);
        }
    }
    return tsd;
}
Also used : TypeSystemDescription_impl(org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) TypeDescription(org.apache.uima.resource.metadata.TypeDescription) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 25 with AnnotationLayer

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer in project webanno by webanno.

the class AnnotationSchemaServiceImpl method getLayer.

@Override
@Transactional(noRollbackFor = NoResultException.class)
public AnnotationLayer getLayer(Project aProject, FeatureStructure aFS) {
    String layerName = aFS.getType().getName();
    AnnotationLayer layer;
    try {
        layer = getLayer(layerName, aProject);
    } catch (NoResultException e) {
        if (layerName.endsWith("Chain")) {
            layerName = layerName.substring(0, layerName.length() - 5);
        }
        if (layerName.endsWith("Link")) {
            layerName = layerName.substring(0, layerName.length() - 4);
        }
        layer = getLayer(layerName, aProject);
    }
    return layer;
}
Also used : NoResultException(javax.persistence.NoResultException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)67 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)35 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)22 ArrayList (java.util.ArrayList)14 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)13 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)12 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)11 LogMessage (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage)8 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)7 Type (org.apache.uima.cas.Type)7 JCas (org.apache.uima.jcas.JCas)6 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)5 FeatureStructure (org.apache.uima.cas.FeatureStructure)5 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)5 Test (org.junit.Test)5 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)4 HashMap (java.util.HashMap)4 TypeSystemDescriptionFactory.createTypeSystemDescription (org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription)4 SoftAssertions (org.assertj.core.api.SoftAssertions)4 Tag (de.tudarmstadt.ukp.clarin.webanno.model.Tag)3