Search in sources :

Example 1 with CasUtil.getType

use of org.apache.uima.fit.util.CasUtil.getType in project webanno by webanno.

the class AutomationUtil method automate.

/**
 * Add new annotation to the CAS using the MIRA prediction. This is different from the add
 * methods in the {@link TypeAdapter}s in such a way that the begin and end offsets are always
 * exact so that no need to re-compute
 *
 * @param aJcas
 *            the JCas.
 * @param aFeature
 *            the feature.
 * @param aLabelValues
 *            the values.
 * @throws AnnotationException
 *             if the annotations could not be created/updated.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static void automate(JCas aJcas, AnnotationFeature aFeature, List<String> aLabelValues) throws AnnotationException, IOException {
    String typeName = aFeature.getLayer().getName();
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature.getLayer().getAttachType().getName();
    Type type = CasUtil.getType(aJcas.getCas(), typeName);
    Feature feature = type.getFeatureByBaseName(aFeature.getName());
    int i = 0;
    String prevNe = "O";
    int begin = 0;
    int end = 0;
    // remove existing annotations of this type, after all it is an
    // automation, no care
    clearAnnotations(aJcas, type);
    if (!aFeature.getLayer().isLockToTokenOffset() || aFeature.getLayer().isMultipleTokens()) {
        for (Token token : select(aJcas, Token.class)) {
            String value = aLabelValues.get(i);
            AnnotationFS newAnnotation;
            if (value.equals("O") && prevNe.equals("O")) {
                i++;
                continue;
            } else if (value.equals("O") && !prevNe.equals("O")) {
                newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", ""));
                prevNe = "O";
                aJcas.getCas().addFsToIndexes(newAnnotation);
            } else if (!value.equals("O") && prevNe.equals("O")) {
                begin = token.getBegin();
                end = token.getEnd();
                prevNe = value;
            } else if (!value.equals("O") && !prevNe.equals("O")) {
                if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", "")) && value.startsWith("B-")) {
                    newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                    newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
                    prevNe = value;
                    begin = token.getBegin();
                    end = token.getEnd();
                    aJcas.getCas().addFsToIndexes(newAnnotation);
                } else if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", ""))) {
                    i++;
                    end = token.getEnd();
                    continue;
                } else {
                    newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                    newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
                    prevNe = value;
                    begin = token.getBegin();
                    end = token.getEnd();
                    aJcas.getCas().addFsToIndexes(newAnnotation);
                }
            }
            i++;
        }
    } else {
        // check if annotation is on an AttachType
        Feature attachFeature = null;
        Type attachType;
        if (attachTypeName != null) {
            attachType = CasUtil.getType(aJcas.getCas(), attachTypeName);
            attachFeature = attachType.getFeatureByBaseName(attachTypeName);
        }
        for (Token token : select(aJcas, Token.class)) {
            AnnotationFS newAnnotation = aJcas.getCas().createAnnotation(type, token.getBegin(), token.getEnd());
            newAnnotation.setFeatureValueFromString(feature, aLabelValues.get(i));
            i++;
            if (attachFeature != null) {
                token.setFeatureValue(attachFeature, newAnnotation);
            }
            aJcas.getCas().addFsToIndexes(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) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 2 with CasUtil.getType

use of org.apache.uima.fit.util.CasUtil.getType in project webanno by webanno.

the class ArcAdapter method delete.

public void delete(AnnotatorState aState, JCas aJCas, AnnotationFeature aFeature, int aBegin, int aEnd, String aDepCoveredText, String aGovCoveredText, Object aValue) {
    Feature dependentFeature = getAnnotationType(aJCas.getCas()).getFeatureByBaseName(getTargetFeatureName());
    Feature governorFeature = getAnnotationType(aJCas.getCas()).getFeatureByBaseName(getSourceFeatureName());
    AnnotationFS dependentFs = null;
    AnnotationFS governorFs = null;
    Type type = CasUtil.getType(aJCas.getCas(), getAnnotationTypeName());
    Type spanType = getType(aJCas.getCas(), getAttachTypeName());
    Feature arcSpanFeature = spanType.getFeatureByBaseName(getAttachFeatureName());
    for (AnnotationFS fs : CasUtil.selectCovered(aJCas.getCas(), type, aBegin, aEnd)) {
        if (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 (aDepCoveredText.equals(dependentFs.getCoveredText()) && aGovCoveredText.equals(governorFs.getCoveredText())) {
            if (ObjectUtils.equals(getFeatureValue(aFeature, fs), aValue)) {
                delete(aState, aJCas, new VID(getAddr(fs)));
            }
        }
    }
}
Also used : 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) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 3 with CasUtil.getType

use of org.apache.uima.fit.util.CasUtil.getType 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)

Example 4 with CasUtil.getType

use of org.apache.uima.fit.util.CasUtil.getType in project webanno by webanno.

the class SpanAdapter method delete.

@Override
public void delete(AnnotatorState aState, JCas aJCas, VID aVid) {
    AnnotationFS fs = selectByAddr(aJCas, AnnotationFS.class, aVid.getId());
    aJCas.removeFsFromIndexes(fs);
    // delete associated attachFeature
    if (getAttachTypeName() != null) {
        Type theType = CasUtil.getType(aJCas.getCas(), getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (attachFeature != null) {
            CasUtil.selectCovered(aJCas.getCas(), theType, fs.getBegin(), fs.getEnd()).get(0).setFeatureValue(attachFeature, null);
        }
    }
    publishEvent(new SpanDeletedEvent(this, aState.getDocument(), aState.getUser().getUsername(), fs));
}
Also used : SpanDeletedEvent(de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanDeletedEvent) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 5 with CasUtil.getType

use of org.apache.uima.fit.util.CasUtil.getType in project webanno by webanno.

the class SpanAdapter method getSpan.

// get feature Value of existing span annotation
public Serializable getSpan(JCas aJCas, int aBegin, int aEnd, AnnotationFeature aFeature, String aLabelValue) {
    if (allowStacking) {
        return null;
    }
    int begin;
    int end;
    // update the begin and ends (no sub token selection)
    if (lockToTokenOffsets) {
        List<Token> tokens = selectOverlapping(aJCas, Token.class, aBegin, aEnd);
        begin = tokens.get(0).getBegin();
        end = tokens.get(tokens.size() - 1).getEnd();
    } else if (allowMultipleToken) {
        List<Token> tokens = selectOverlapping(aJCas, Token.class, aBegin, aEnd);
        begin = tokens.get(0).getBegin();
        end = tokens.get(tokens.size() - 1).getEnd();
    } else {
        begin = aBegin;
        end = aEnd;
    }
    Type type = CasUtil.getType(aJCas.getCas(), getAnnotationTypeName());
    for (AnnotationFS fs : CasUtil.selectCovered(aJCas.getCas(), type, begin, end)) {
        if (fs.getBegin() == aBegin && fs.getEnd() == aEnd) {
            return getFeatureValue(aFeature, fs);
        }
    }
    return null;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Type (org.apache.uima.cas.Type)6 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)6 Feature (org.apache.uima.cas.Feature)5 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)5 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)4 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)2 SpanCreatedEvent (de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanCreatedEvent)1 SpanDeletedEvent (de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.SpanDeletedEvent)1 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)1 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1