Search in sources :

Example 26 with Feature

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

the class WebAnnoCasUtil method setLinkFeature.

private static void setLinkFeature(FeatureStructure aFS, AnnotationFeature aFeature, List<LinkWithRoleModel> aValue, Feature feature) {
    Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
    Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
    Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
    // Create all the links
    // FIXME: actually we could re-use existing link link feature structures
    List<FeatureStructure> linkFSes = new ArrayList<>();
    if (aValue != null) {
        // remove duplicate links
        Set<LinkWithRoleModel> links = new HashSet<>(aValue);
        for (LinkWithRoleModel e : links) {
            // set
            if (e.targetAddr == -1) {
                continue;
            }
            FeatureStructure link = aFS.getCAS().createFS(linkType);
            link.setStringValue(roleFeat, e.role);
            link.setFeatureValue(targetFeat, selectByAddr(aFS.getCAS(), e.targetAddr));
            linkFSes.add(link);
        }
    }
    setLinkFeatureValue(aFS, feature, linkFSes);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) HashSet(java.util.HashSet)

Example 27 with Feature

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

the class RemoteApiController2 method forceSetFeatureValue.

private static void forceSetFeatureValue(FeatureStructure aFS, String aFeatureName, String aValue) {
    CASImpl casImpl = (CASImpl) aFS.getCAS().getLowLevelCAS();
    TypeSystemImpl ts = (TypeSystemImpl) aFS.getCAS().getTypeSystem();
    Feature feat = aFS.getType().getFeatureByBaseName(aFeatureName);
    int featCode = ((FeatureImpl) feat).getCode();
    int thisType = ((TypeImpl) aFS.getType()).getCode();
    if (!ts.isApprop(thisType, featCode)) {
        throw new IllegalArgumentException("Feature structure does not have that feature");
    }
    if (!ts.subsumes(ts.getType(CAS.TYPE_NAME_STRING), feat.getRange())) {
        throw new IllegalArgumentException("Not a string feature!");
    }
    casImpl.ll_setStringValue(casImpl.ll_getFSRef(aFS), featCode, aValue);
}
Also used : CASImpl(org.apache.uima.cas.impl.CASImpl) FeatureImpl(org.apache.uima.cas.impl.FeatureImpl) TypeSystemImpl(org.apache.uima.cas.impl.TypeSystemImpl) Feature(org.apache.uima.cas.Feature) TypeImpl(org.apache.uima.cas.impl.TypeImpl)

Example 28 with Feature

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

the class TypeSystemAnalysis method analyzeRelationLayer.

private Optional<RelationDetails> analyzeRelationLayer(TypeSystem aTS, Type aType) {
    // A UIMA type can be a relation layer if...
    // ... there are exactly two non-primitive features
    // ... both have the same range
    // ... the range is a span layer
    List<Feature> nonPrimitiveFeatures = aType.getFeatures().stream().filter(f -> !isBuiltInFeature(f)).filter(f -> !f.getRange().isPrimitive()).collect(Collectors.toList());
    // ... there are exactly two non-primitive features
    if (nonPrimitiveFeatures.size() != 2) {
        return Optional.empty();
    }
    Feature ref1 = nonPrimitiveFeatures.get(0);
    Feature ref2 = nonPrimitiveFeatures.get(1);
    // Relations must use the names "Governor" and "Dependent" for its references because
    // these names are hardcoded throughout WebAnno.
    List<String> validNames = asList(FEAT_REL_SOURCE, FEAT_REL_TARGET);
    if (!validNames.contains(ref1.getShortName()) || !validNames.contains(ref2.getShortName())) {
        return Optional.empty();
    }
    // ... both have the same range
    if (!ref1.getRange().getName().equals(ref2.getRange().getName())) {
        return Optional.empty();
    }
    // Annotation should be fine.
    if (!aTS.subsumes(aTS.getType(CAS.TYPE_NAME_ANNOTATION), ref1.getRange())) {
        return Optional.empty();
    }
    RelationDetails details = new RelationDetails();
    details.attachLayer = ref1.getRange().getName();
    details.sourceFeature = WebAnnoConst.FEAT_REL_SOURCE;
    details.targetFeature = WebAnnoConst.FEAT_REL_TARGET;
    // Hm, ok, so this looks like a relation layer.
    return Optional.of(details);
}
Also used : FEAT_REL_SOURCE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_SOURCE) FEAT_REL_TARGET(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_TARGET) TypeDescription(org.apache.uima.resource.metadata.TypeDescription) LoggerFactory(org.slf4j.LoggerFactory) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) HashMap(java.util.HashMap) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) FSUtil(org.apache.uima.fit.util.FSUtil) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) HashSet(java.util.HashSet) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) Arrays.asList(java.util.Arrays.asList) ListValuedMap(org.apache.commons.collections4.ListValuedMap) Map(java.util.Map) TypeSystem(org.apache.uima.cas.TypeSystem) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Logger(org.slf4j.Logger) MultiValueMode(de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) Set(java.util.Set) RELATION_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.RELATION_TYPE) SPAN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE) Collectors(java.util.stream.Collectors) CasCreationUtils(org.apache.uima.util.CasCreationUtils) List(java.util.List) FeatureDescription(org.apache.uima.resource.metadata.FeatureDescription) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) StringUtils.removeEnd(org.apache.commons.lang3.StringUtils.removeEnd) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) CHAIN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CHAIN_TYPE) Optional(java.util.Optional) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 29 with Feature

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

the class TypeSystemAnalysis method analyzeChainLayer.

private Optional<ChainDetails> analyzeChainLayer(TypeSystem aTS, Type aType) {
    // DKPro Core CoreferenceChain - we expect that all chains follow this convention.
    if (!aType.getName().endsWith("Chain")) {
        return Optional.empty();
    }
    // There must be an initial chain feature. The name of this feature is currently
    // hard-coded in WebAnno to the one used by the DKPro Core CoreferenceChain
    Feature first = aType.getFeatureByBaseName("first");
    if (first == null) {
        return Optional.empty();
    }
    // The initial chain features must be a non-primitive
    if (first.getRange().isPrimitive()) {
        return Optional.empty();
    }
    // The chain link must contain a feature named "next" used to connect to the next link
    Feature next = first.getRange().getFeatureByBaseName("next");
    if (next == null || !next.getRange().equals(first.getRange())) {
        return Optional.empty();
    }
    // AnnotationBase
    if (!aTS.getParent(aType).equals(aTS.getType(CAS.TYPE_NAME_ANNOTATION_BASE))) {
        return Optional.empty();
    }
    ChainDetails details = new ChainDetails();
    details.nextFeature = "next";
    // Hm, ok, so this looks like a chain layer.
    return Optional.of(details);
}
Also used : Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 30 with Feature

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

the class TypeSystemAnalysis method analyzeFeatures.

private void analyzeFeatures(AnnotationLayer aLayer, TypeSystem aTS, TypeDescription aTD, Optional<? extends LayerDetails> aDetails) {
    Type type = aTS.getType(aTD.getName());
    for (FeatureDescription fd : aTD.getFeatures()) {
        Feature feat = type.getFeatureByBaseName(fd.getName());
        // We do not need to set up built-in features
        if (isBuiltInFeature(feat)) {
            continue;
        }
        if (aDetails.isPresent() && aDetails.get().isHiddenFeature(feat)) {
            continue;
        }
        AnnotationFeature f = analyzeFeature(aTS, fd, feat);
        features.put(aLayer.getName(), f);
    }
}
Also used : Type(org.apache.uima.cas.Type) FeatureDescription(org.apache.uima.resource.metadata.FeatureDescription) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) 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