Search in sources :

Example 1 with SpanCreatedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanCreatedEvent 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)

Aggregations

SpanCreatedEvent (de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanCreatedEvent)1 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)1 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)1 Feature (org.apache.uima.cas.Feature)1 Type (org.apache.uima.cas.Type)1 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)1 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)1