Search in sources :

Example 11 with Feature

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

the class WebannoTsv3Writer method setRelationAnnoPerFeature.

private void setRelationAnnoPerFeature(Map<AnnotationUnit, List<List<String>>> annotationsPertype, Type type, AnnotationFS fs, AnnotationUnit depUnit, AnnotationUnit govUnit, int aGovRef, int aDepRef, Type aDepType) {
    List<String> annoPerFeatures = new ArrayList<>();
    featurePerLayer.putIfAbsent(type.getName(), new LinkedHashSet<>());
    for (Feature feature : type.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;
        }
        int ref = getRefId(type, fs, depUnit);
        String annotation = fs.getFeatureValueAsString(feature);
        if (annotation == null) {
            annotation = "*";
        } else {
            annotation = replaceEscapeChars(annotation);
        }
        // +(ref > 0 ? "[" + ref + "]" : ""));
        annoPerFeatures.add(annotation);
        featurePerLayer.get(type.getName()).add(feature.getShortName());
    }
    // add the governor and dependent unit addresses (separated by _
    String govRef = unitsLineNumber.get(govUnit) + ((aDepRef > 0 || aGovRef > 0) ? "[" + aGovRef + "_" + aDepRef + "]" : "");
    annoPerFeatures.add(govRef);
    featurePerLayer.get(type.getName()).add(BT + aDepType.getName());
    // the column for the dependent unit address
    annotationsPertype.putIfAbsent(depUnit, new ArrayList<>());
    if (annoPerFeatures.size() == 0) {
        annoPerFeatures.add("*");
    }
    annotationsPertype.get(depUnit).add(annoPerFeatures);
}
Also used : ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature)

Example 12 with Feature

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

the class Tsv3XCasSchemaAnalyzer method isRelationLayer.

public static boolean isRelationLayer(Type aType) {
    Feature relSourceFeat = aType.getFeatureByBaseName(FEAT_REL_SOURCE);
    boolean hasSourceFeature = relSourceFeat != null && !isPrimitiveFeature(relSourceFeat);
    Feature relTargetFeat = aType.getFeatureByBaseName(FEAT_REL_TARGET);
    boolean hasTargetFeature = relTargetFeat != null && !isPrimitiveFeature(relTargetFeat);
    boolean compatible = true;
    for (Feature feat : aType.getFeatures()) {
        if (CAS.FEATURE_BASE_NAME_SOFA.equals(feat.getShortName()) || FEAT_REL_SOURCE.equals(feat.getShortName()) || FEAT_REL_TARGET.equals(feat.getShortName())) {
            continue;
        }
        if (!isPrimitiveFeature(feat)) {
            compatible = false;
            // LOG.debug("Incompatible feature in type [" + aType + "]: " + feat);
            break;
        }
    }
    return hasSourceFeature && hasTargetFeature && compatible;
}
Also used : Feature(org.apache.uima.cas.Feature)

Example 13 with Feature

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

the class Tsv3XCasSchemaAnalyzer method analyze.

public static TsvSchema analyze(TypeSystem aTypeSystem) {
    TsvSchema schema = new TsvSchema();
    Set<Type> chainLinkTypes = new HashSet<>();
    // Consider only direct subtypes of the UIMA Annotation type. Currently, WebAnno only
    // supports such layers.
    Type annotationType = aTypeSystem.getType(CAS.TYPE_NAME_ANNOTATION);
    Type documentAnnotationType = aTypeSystem.getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
    for (Type type : aTypeSystem.getDirectSubtypes(annotationType)) {
        if (aTypeSystem.subsumes(documentAnnotationType, type)) {
            continue;
        }
        if (type.getName().equals(Token.class.getName()) || type.getName().equals(Sentence.class.getName())) {
            continue;
        }
        switch(schema.getLayerType(type)) {
            case RELATION:
                schema.addColumn(new TsvColumn(type, RELATION, type.getFeatureByBaseName(FEAT_REL_SOURCE), RELATION_REF));
                generateColumns(aTypeSystem, schema, RELATION, type);
                break;
            case CHAIN:
                schema.addColumn(new TsvColumn(type, CHAIN, type.getFeatureByBaseName(COREFERENCE_TYPE_FEATURE), CHAIN_ELEMENT_TYPE));
                schema.addColumn(new TsvColumn(type, CHAIN, type.getFeatureByBaseName(COREFERENCE_RELATION_FEATURE), CHAIN_LINK_TYPE));
                chainLinkTypes.add(type);
                break;
            case SPAN:
                schema.addColumn(new TsvColumn(type, SPAN));
                generateColumns(aTypeSystem, schema, SPAN, type);
                break;
            case INCOMPATIBLE:
                // Do not generate a column definition for incompatible types.
                break;
        }
    }
    // Scan again for the chain head types
    Type topType = aTypeSystem.getType(CAS.TYPE_NAME_ANNOTATION_BASE);
    for (Type type : aTypeSystem.getDirectSubtypes(topType)) {
        Feature firstFeat = type.getFeatureByBaseName(CHAIN_FIRST_FEAT);
        if (firstFeat != null && chainLinkTypes.contains(firstFeat.getRange())) {
            schema.addChainHeadType(type);
        }
    }
    return schema;
}
Also used : LayerType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.LayerType) Type(org.apache.uima.cas.Type) FeatureType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.FeatureType) TsvColumn(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn) TsvSchema(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvSchema) Feature(org.apache.uima.cas.Feature) HashSet(java.util.HashSet)

Example 14 with Feature

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

the class Tsv3XCasSchemaAnalyzer method isChainLayer.

public static boolean isChainLayer(Type aType) {
    boolean hasTypeFeature = aType.getFeatureByBaseName(COREFERENCE_TYPE_FEATURE) != null;
    boolean hasRelationFeature = aType.getFeatureByBaseName(COREFERENCE_RELATION_FEATURE) != null;
    boolean nameEndsInLink = aType.getName().endsWith("Link");
    boolean compatible = true;
    for (Feature feat : aType.getFeatures()) {
        if (CAS.FEATURE_BASE_NAME_SOFA.equals(feat.getShortName()) || CHAIN_NEXT_FEAT.equals(feat.getShortName()) || COREFERENCE_TYPE_FEATURE.equals(feat.getShortName()) || COREFERENCE_RELATION_FEATURE.equals(feat.getShortName())) {
            continue;
        }
        if (!isPrimitiveFeature(feat)) {
            compatible = false;
            LOG.debug("Incompatible feature in type [" + aType + "]: " + feat);
            break;
        }
    }
    return hasTypeFeature && hasRelationFeature && nameEndsInLink && compatible;
}
Also used : Feature(org.apache.uima.cas.Feature)

Example 15 with Feature

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

the class Tsv3XDeserializer method parseColumnDeclaration.

private TsvColumn parseColumnDeclaration(JCas aJCas, LayerType aLayerType, Type aUimaType, int aIndex, String aColDecl, TsvColumn aPrevCol) throws IOException {
    TypeSystem ts = aJCas.getTypeSystem();
    TsvColumn column;
    // SLOT_ROLE - starts with "ROLE_"
    if (SPAN.equals(aLayerType) && startsWith(aColDecl, HEADER_PREFIX_ROLE)) {
        String[] subFields = splitPreserveAllTokens(aColDecl, '_');
        String featureName = substringAfter(subFields[1], ":");
        Feature feat = aUimaType.getFeatureByBaseName(featureName);
        if (feat == null) {
            throw new IOException("CAS type [" + aUimaType.getName() + "] does not have a feature called [" + featureName + "]");
        }
        column = new TsvColumn(aIndex, aUimaType, aLayerType, featureName, SLOT_ROLE);
        String typeName = subFields[2];
        Type type = ts.getType(typeName);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + typeName + "]");
        }
        column.setTargetTypeHint(type);
    } else // RELATION_REF - starts with "BT_
    if (RELATION.equals(aLayerType) && startsWith(aColDecl, HEADER_PREFIX_BASE_TYPE)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, FEAT_REL_SOURCE, RELATION_REF);
        String typeName = substringAfter(aColDecl, HEADER_PREFIX_BASE_TYPE);
        Type type = ts.getType(typeName);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + typeName + "]");
        }
        column.setTargetTypeHint(type);
    } else // CHAIN_ELEMENT_TYPE - "referenceType"
    if (CHAIN.equals(aLayerType) && COREFERENCE_TYPE_FEATURE.equals(aColDecl)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, COREFERENCE_TYPE_FEATURE, CHAIN_ELEMENT_TYPE);
    } else // CHAIN_LINK_TYPE - "referenceRelation"
    if (CHAIN.equals(aLayerType) && COREFERENCE_RELATION_FEATURE.equals(aColDecl)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, COREFERENCE_RELATION_FEATURE, CHAIN_LINK_TYPE);
    } else // SLOT_TARGET - name of the link target type
    if (SPAN.equals(aLayerType) && aColDecl.contains(".") || ts.getType(aColDecl) != null) {
        // the type name really exists in the target CAS.
        if (ts.getType(aColDecl) == null) {
            throw new IOException("CAS type system does not contain a type named [" + aColDecl + "]");
        }
        // name from it.
        if (aPrevCol == null || !SLOT_ROLE.equals(aPrevCol.featureType)) {
            throw new IOException("Slot target column declaration must follow slot role column declaration");
        }
        column = new TsvColumn(aIndex, aUimaType, aLayerType, aPrevCol.uimaFeature.getShortName(), SLOT_TARGET);
        Type type = ts.getType(aColDecl);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + aColDecl + "]");
        }
        column.setTargetTypeHint(type);
    } else // PRIMITIVE - feature name
    if (aUimaType.getFeatureByBaseName(aColDecl) != null) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, aColDecl, PRIMITIVE);
    } else {
        throw new IOException("Type [" + aUimaType.getName() + "] does not contain a feature called [" + aColDecl + "]");
    }
    return column;
}
Also used : TypeSystem(org.apache.uima.cas.TypeSystem) Type(org.apache.uima.cas.Type) LayerType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.LayerType) TsvColumn(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn) IOException(java.io.IOException) FSUtil.setFeature(org.apache.uima.fit.util.FSUtil.setFeature) FSUtil.getFeature(org.apache.uima.fit.util.FSUtil.getFeature) Feature(org.apache.uima.cas.Feature)

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