Search in sources :

Example 1 with FeatureStructure

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

the class AnnotationDetailEditorPanel method getAttachedRels.

public Set<AnnotationFS> getAttachedRels(AnnotationFS aFs, AnnotationLayer aLayer) {
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> toBeDeleted = new HashSet<>();
    for (AnnotationLayer relationLayer : annotationService.listAttachedRelationLayers(aLayer)) {
        ArcAdapter relationAdapter = (ArcAdapter) annotationService.getAdapter(relationLayer);
        Type relationType = CasUtil.getType(cas, relationLayer.getName());
        Feature sourceFeature = relationType.getFeatureByBaseName(relationAdapter.getSourceFeatureName());
        Feature targetFeature = relationType.getFeatureByBaseName(relationAdapter.getTargetFeatureName());
        // This code is already prepared for the day that relations can go between
        // different layers and may have different attach features for the source and
        // target layers.
        Feature relationSourceAttachFeature = null;
        Feature relationTargetAttachFeature = null;
        if (relationAdapter.getAttachFeatureName() != null) {
            relationSourceAttachFeature = sourceFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
            relationTargetAttachFeature = targetFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
        }
        for (AnnotationFS relationFS : CasUtil.select(cas, relationType)) {
            // Here we get the annotations that the relation is pointing to in the UI
            FeatureStructure sourceFS;
            if (relationSourceAttachFeature != null) {
                sourceFS = relationFS.getFeatureValue(sourceFeature).getFeatureValue(relationSourceAttachFeature);
            } else {
                sourceFS = relationFS.getFeatureValue(sourceFeature);
            }
            FeatureStructure targetFS;
            if (relationTargetAttachFeature != null) {
                targetFS = relationFS.getFeatureValue(targetFeature).getFeatureValue(relationTargetAttachFeature);
            } else {
                targetFS = relationFS.getFeatureValue(targetFeature);
            }
            if (isSame(sourceFS, aFs) || isSame(targetFS, aFs)) {
                toBeDeleted.add(relationFS);
                LOG.debug("Deleted relation [" + getAddr(relationFS) + "] from layer [" + relationLayer.getName() + "]");
            }
        }
    }
    return toBeDeleted;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CAS(org.apache.uima.cas.CAS) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Feature(org.apache.uima.cas.Feature) WebAnnoCasUtil.setFeature(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) HashSet(java.util.HashSet)

Example 2 with FeatureStructure

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

the class AnnotationDetailEditorPanel method populateTagsBasedOnRules.

/**
 * Adds and sorts tags based on Constraints rules
 */
private void populateTagsBasedOnRules(JCas aJCas, FeatureState aModel) {
    LOG.trace("populateTagsBasedOnRules(feature: " + aModel.feature.getUiName() + ")");
    AnnotatorState state = getModelObject();
    // Add values from rules
    String restrictionFeaturePath;
    switch(aModel.feature.getLinkMode()) {
        case WITH_ROLE:
            restrictionFeaturePath = aModel.feature.getName() + "." + aModel.feature.getLinkTypeRoleFeatureName();
            break;
        case NONE:
            restrictionFeaturePath = aModel.feature.getName();
            break;
        default:
            throw new IllegalArgumentException("Unsupported link mode [" + aModel.feature.getLinkMode() + "] on feature [" + aModel.feature.getName() + "]");
    }
    aModel.indicator.reset();
    // Fetch possible values from the constraint rules
    List<PossibleValue> possibleValues;
    try {
        FeatureStructure featureStructure = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
        Evaluator evaluator = new ValuesGenerator();
        // Only show indicator if this feature can be affected by Constraint rules!
        aModel.indicator.setAffected(evaluator.isThisAffectedByConstraintRules(featureStructure, restrictionFeaturePath, state.getConstraints()));
        possibleValues = evaluator.generatePossibleValues(featureStructure, restrictionFeaturePath, state.getConstraints());
        LOG.debug("Possible values for [" + featureStructure.getType().getName() + "] [" + restrictionFeaturePath + "]: " + possibleValues);
    } catch (Exception e) {
        error("Unable to evaluate constraints: " + ExceptionUtils.getRootCauseMessage(e));
        LOG.error("Unable to evaluate constraints: " + e.getMessage(), e);
        possibleValues = new ArrayList<>();
    }
    // Fetch actual tagset
    List<Tag> valuesFromTagset = annotationService.listTags(aModel.feature.getTagset());
    // First add tags which are suggested by rules and exist in tagset
    List<Tag> tagset = compareSortAndAdd(possibleValues, valuesFromTagset, aModel.indicator);
    // Then add the remaining tags
    for (Tag remainingTag : valuesFromTagset) {
        if (!tagset.contains(remainingTag)) {
            tagset.add(remainingTag);
        }
    }
    // Record the possible values and the (re-ordered) tagset in the feature state
    aModel.possibleValues = possibleValues;
    aModel.tagset = tagset;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) ArrayList(java.util.ArrayList) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) Tag(de.tudarmstadt.ukp.clarin.webanno.model.Tag) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 3 with FeatureStructure

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

the class WebannoTsv3Reader method addChainAnnotations.

/**
 * The individual link annotations are stored in a {@link TreeMap} (chainAnnosPerTye) with chain
 * number and link number references, sorted in an ascending order <br>
 * Iterate over each chain number and link number references and construct the chain.
 */
private void addChainAnnotations(JCas aJCas) {
    for (Type linkType : chainAnnosPerTyep.keySet()) {
        for (int chainNo : chainAnnosPerTyep.get(linkType).keySet()) {
            Type chainType = aJCas.getCas().getTypeSystem().getType(linkType.getName().substring(0, linkType.getName().length() - 4) + CHAIN);
            Feature firstF = chainType.getFeatureByBaseName(FIRST);
            Feature nextF = linkType.getFeatureByBaseName(NEXT);
            FeatureStructure chain = aJCas.getCas().createFS(chainType);
            aJCas.addFsToIndexes(chain);
            AnnotationFS firstFs = chainAnnosPerTyep.get(linkType).get(chainNo).get(1);
            AnnotationFS linkFs = firstFs;
            chain.setFeatureValue(firstF, firstFs);
            for (int i = 2; i <= chainAnnosPerTyep.get(linkType).get(chainNo).size(); i++) {
                linkFs.setFeatureValue(nextF, chainAnnosPerTyep.get(linkType).get(chainNo).get(i));
                linkFs = chainAnnosPerTyep.get(linkType).get(chainNo).get(i);
            }
        }
    }
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) Feature(org.apache.uima.cas.Feature)

Example 4 with FeatureStructure

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

the class WebannoTsv3Writer method setSpanAnnoPerFeature.

private void setSpanAnnoPerFeature(Map<AnnotationUnit, List<List<String>>> aAnnotationsPertype, Type aType, AnnotationFS aFs, AnnotationUnit aUnit, boolean aIsMultiToken, boolean aIsFirst) {
    List<String> annoPerFeatures = new ArrayList<>();
    featurePerLayer.putIfAbsent(aType.getName(), new LinkedHashSet<>());
    int ref = getRefId(aType, aFs, aUnit);
    if (ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)) != null && ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)).equals(false)) {
        ref = 0;
    }
    if (ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)) == null && ambigUnits.get(aType.getName()).get(aUnit).equals(false)) {
        ref = 0;
    }
    for (Feature feature : aType.getFeatures()) {
        if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end") || feature.getShortName().equals(GOVERNOR) || feature.getShortName().equals(DEPENDENT) || feature.getShortName().equals(FIRST) || feature.getShortName().equals(NEXT)) {
            continue;
        }
        // if slot feature
        if (slotFeatures != null && slotFeatures.contains(feature.getName())) {
            if (aFs.getFeatureValue(feature) != null) {
                ArrayFS array = (ArrayFS) aFs.getFeatureValue(feature);
                StringBuilder sbRole = new StringBuilder();
                StringBuilder sbTarget = new StringBuilder();
                for (FeatureStructure linkFS : array.toArray()) {
                    String role = linkFS.getStringValue(linkFS.getType().getFeatureByBaseName("role"));
                    AnnotationFS targetFs = (AnnotationFS) linkFS.getFeatureValue(linkFS.getType().getFeatureByBaseName("target"));
                    Type tType = targetFs.getType();
                    AnnotationUnit firstUnit = getFirstUnit(targetFs);
                    ref = getRefId(tType, targetFs, firstUnit);
                    // Check if the target is ambiguous or not
                    if (ambigUnits.get(tType.getName()).get(firstUnit).equals(false)) {
                        ref = 0;
                    }
                    if (role == null) {
                        role = "*";
                    } else {
                        // Escape special character
                        role = replaceEscapeChars(role);
                    }
                    if (sbRole.length() < 1) {
                        sbRole.append(role);
                        // record the actual target type column number if slot target is
                        // uima.tcas.Annotation
                        int targetTypeNumber = 0;
                        if (slotFeatureTypes.get(feature).getName().equals(CAS.TYPE_NAME_ANNOTATION)) {
                            targetTypeNumber = layerMaps.get(tType);
                        }
                        sbTarget.append(unitsLineNumber.get(firstUnit)).append(targetTypeNumber == 0 ? "" : "-" + targetTypeNumber).append(ref > 0 ? "[" + ref + "]" : "");
                    } else {
                        sbRole.append(";");
                        sbTarget.append(";");
                        sbRole.append(role);
                        int targetTypeNumber = 0;
                        if (slotFeatureTypes.get(feature).getName().equals(CAS.TYPE_NAME_ANNOTATION)) {
                            targetTypeNumber = layerMaps.get(tType);
                        }
                        sbTarget.append(unitsLineNumber.get(firstUnit)).append(targetTypeNumber == 0 ? "" : "-" + targetTypeNumber).append(ref > 0 ? "[" + ref + "]" : "");
                    }
                }
                annoPerFeatures.add(sbRole.toString().isEmpty() ? "_" : sbRole.toString());
                annoPerFeatures.add(sbTarget.toString().isEmpty() ? "_" : sbTarget.toString());
            } else {
                // setting it to null
                annoPerFeatures.add("_");
                annoPerFeatures.add("_");
            }
            featurePerLayer.get(aType.getName()).add(ROLE + feature.getName() + "_" + slotLinkTypes.get(feature.getName()));
            featurePerLayer.get(aType.getName()).add(slotFeatureTypes.get(feature).getName());
        } else {
            String annotation = aFs.getFeatureValueAsString(feature);
            if (annotation == null) {
                annotation = "*";
            } else {
                // Escape special character
                annotation = replaceEscapeChars(annotation);
            }
            annotation = annotation + (ref > 0 ? "[" + ref + "]" : "");
            // only add BIO markers to multiple annotations
            setAnnoFeature(aIsMultiToken, aIsFirst, annoPerFeatures, annotation);
            featurePerLayer.get(aType.getName()).add(feature.getShortName());
        }
    }
    aAnnotationsPertype.putIfAbsent(aUnit, new ArrayList<>());
    // If the layer do not have a feature at all, add dummy * as a place holder
    if (annoPerFeatures.size() == 0) {
        setAnnoFeature(aIsMultiToken, aIsFirst, annoPerFeatures, "*" + (ref > 0 ? "[" + ref + "]" : ""));
    }
    aAnnotationsPertype.get(aUnit).add(annoPerFeatures);
}
Also used : ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) AnnotationUnit(de.tudarmstadt.ukp.clarin.webanno.tsv.util.AnnotationUnit) ArrayFS(org.apache.uima.cas.ArrayFS)

Example 5 with FeatureStructure

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

the class Tsv3XCasDocumentBuilder method scanUnitForActiveColumns.

private static void scanUnitForActiveColumns(TsvUnit aUnit) {
    for (TsvColumn col : aUnit.getDocument().getSchema().getColumns()) {
        List<AnnotationFS> annotationsForColumn = aUnit.getAnnotationsForColumn(col);
        if (!annotationsForColumn.isEmpty()) {
            if (!PLACEHOLDER.equals(col.featureType)) {
                aUnit.getDocument().activateColumn(col);
            }
            // actual annotation.
            if (RELATION.equals(col.layerType) && RELATION_REF.equals(col.featureType)) {
                AnnotationFS annotation = annotationsForColumn.get(0);
                FeatureStructure target = FSUtil.getFeature(annotation, FEAT_REL_SOURCE, FeatureStructure.class);
                if (target == null) {
                    throw new IllegalStateException("Relation does not have its source feature (" + FEAT_REL_SOURCE + ") set: " + annotation);
                }
                if (col.uimaType.getName().equals(Dependency.class.getName())) {
                    // COMPATIBILITY NOTE:
                    // WebAnnoTsv3Writer hard-changes the target type for DKPro Core
                    // Dependency annotations from Token to POS - the reason is not really
                    // clear. Probably because the Dependency relations in the WebAnno UI
                    // attach to POS (Token's are not visible as annotations in the UI).
                    col.setTargetTypeHint(aUnit.getDocument().getJCas().getTypeSystem().getType(POS.class.getName()));
                } else {
                    col.setTargetTypeHint(target.getType());
                }
            }
        }
    }
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) TsvColumn(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency)

Aggregations

FeatureStructure (org.apache.uima.cas.FeatureStructure)60 Type (org.apache.uima.cas.Type)38 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)37 ArrayList (java.util.ArrayList)29 JCas (org.apache.uima.jcas.JCas)20 Feature (org.apache.uima.cas.Feature)17 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)16 CAS (org.apache.uima.cas.CAS)16 Test (org.junit.Test)16 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)13 List (java.util.List)12 ArrayFS (org.apache.uima.cas.ArrayFS)8 Arrays.asList (java.util.Arrays.asList)6 LinkedHashMap (java.util.LinkedHashMap)6 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)6 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)5 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)4 LogMessage (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage)4 TsvColumn (de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn)4 Map (java.util.Map)4