Search in sources :

Example 46 with Feature

use of org.apache.uima.cas.Feature 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 47 with Feature

use of org.apache.uima.cas.Feature 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 48 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class SpanAdapter method getAnnotation.

@Override
public List<String> getAnnotation(Sentence aSentence, AnnotationFeature aFeature) {
    CAS cas = aSentence.getCAS();
    Type type = getType(cas, getAnnotationTypeName());
    List<String> annotations = new ArrayList<>();
    for (Token token : selectCovered(Token.class, aSentence)) {
        List<AnnotationFS> tokenLevelAnnotations = selectCovered(type, token);
        if (tokenLevelAnnotations.size() > 0) {
            AnnotationFS anno = tokenLevelAnnotations.get(0);
            Feature labelFeature = anno.getType().getFeatureByBaseName(aFeature.getName());
            annotations.add(anno.getFeatureValueAsString(labelFeature));
        } else {
            annotations.add(NILL);
        }
    }
    return annotations;
}
Also used : 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) ArrayList(java.util.ArrayList) 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 49 with Feature

use of org.apache.uima.cas.Feature 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 50 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class SpanAdapter method getMultipleAnnotation.

public Map<Integer, String> getMultipleAnnotation(Sentence sentence, AnnotationFeature aFeature) throws CASException {
    Map<Integer, String> multAnno = new HashMap<>();
    Type type = getType(sentence.getCAS(), getAnnotationTypeName());
    for (AnnotationFS fs : selectCovered(type, sentence)) {
        boolean isBegin = true;
        Feature labelFeature = fs.getType().getFeatureByBaseName(aFeature.getName());
        for (Token token : selectCovered(Token.class, fs)) {
            if (multAnno.get(getAddr(token)) == null) {
                if (isBegin) {
                    multAnno.put(getAddr(token), "B-" + fs.getFeatureValueAsString(labelFeature));
                    isBegin = false;
                } else {
                    multAnno.put(getAddr(token), "I-" + fs.getFeatureValueAsString(labelFeature));
                }
            }
        }
    }
    return multAnno;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) HashMap(java.util.HashMap) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

Feature (org.apache.uima.cas.Feature)84 Type (org.apache.uima.cas.Type)62 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)50 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)48 ArrayList (java.util.ArrayList)23 FeatureStructure (org.apache.uima.cas.FeatureStructure)18 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)18 JCas (org.apache.uima.jcas.JCas)18 List (java.util.List)15 Test (org.junit.Test)14 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)13 WebAnnoCasUtil.setFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature)12 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)12 CAS (org.apache.uima.cas.CAS)10 HashSet (java.util.HashSet)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 HashMap (java.util.HashMap)7 TypeSystem (org.apache.uima.cas.TypeSystem)7 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)6