Search in sources :

Example 76 with Feature

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

the class WebannoTsv3Writer method setSpanAnnotation.

private void setSpanAnnotation(JCas aJCas) {
    int i = 0;
    // store slot targets for each slot features
    for (String l : spanLayers) {
        Type type = getType(aJCas.getCas(), l);
        for (Feature f : type.getFeatures()) {
            if (slotFeatures != null && slotFeatures.contains(f.getName())) {
                slotFeatureTypes.put(f, getType(aJCas.getCas(), slotTargets.get(i)));
                i++;
            }
        }
    }
    for (String l : spanLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }
        Map<AnnotationUnit, List<List<String>>> annotationsPertype;
        if (annotationsPerPostion.get(l) == null) {
            annotationsPertype = new HashMap<>();
        } else {
            annotationsPertype = annotationsPerPostion.get(l);
        }
        Type type = getType(aJCas.getCas(), l);
        for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
            AnnotationUnit unit = new AnnotationUnit(fs.getBegin(), fs.getEnd(), false, fs.getCoveredText());
            // annotation is per Token
            if (units.contains(unit)) {
                setSpanAnnoPerFeature(annotationsPertype, type, fs, unit, false, false);
            } else // Annotation is on sub-token or multiple tokens
            {
                SubTokenAnno sta = new SubTokenAnno();
                sta.setBegin(fs.getBegin());
                sta.setEnd(fs.getEnd());
                sta.setText(fs.getCoveredText());
                boolean isMultiToken = isMultiToken(fs);
                boolean isFirst = true;
                Set<AnnotationUnit> sus = new LinkedHashSet<>();
                for (AnnotationUnit newUnit : getSubUnits(sta, sus)) {
                    setSpanAnnoPerFeature(annotationsPertype, type, fs, newUnit, isMultiToken, isFirst);
                    isFirst = false;
                }
            }
        }
        if (annotationsPertype.keySet().size() > 0) {
            annotationsPerPostion.put(l, annotationsPertype);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Feature(org.apache.uima.cas.Feature) 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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 77 with Feature

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

the class WebannoTsv3Writer method setChainAnnotation.

private void setChainAnnotation(JCas aJCas) {
    for (String l : chainLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }
        Map<AnnotationUnit, List<List<String>>> annotationsPertype = null;
        Type type = getType(aJCas.getCas(), l + CHAIN);
        Feature chainFirst = type.getFeatureByBaseName(FIRST);
        int chainNo = 1;
        for (FeatureStructure chainFs : selectFS(aJCas.getCas(), type)) {
            AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
            AnnotationUnit unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
            Type lType = linkFs.getType();
            // this is the layer with annotations
            l = lType.getName();
            if (annotationsPerPostion.get(l) == null) {
                annotationsPertype = new HashMap<>();
            } else {
                annotationsPertype = annotationsPerPostion.get(l);
            }
            Feature linkNext = linkFs.getType().getFeatureByBaseName(NEXT);
            int linkNo = 1;
            while (linkFs != null) {
                AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
                if (nextLinkFs != null) {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo, chainNo);
                } else {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo, chainNo);
                }
                linkFs = nextLinkFs;
                linkNo++;
                if (nextLinkFs != null) {
                    unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
                }
            }
            if (annotationsPertype.keySet().size() > 0) {
                annotationsPerPostion.put(l, annotationsPertype);
            }
            chainNo++;
        }
    }
}
Also used : 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) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) ArrayList(java.util.ArrayList) List(java.util.List) Feature(org.apache.uima.cas.Feature)

Example 78 with Feature

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

the class Tsv3XDeserializer method setPrimitiveValue.

private void setPrimitiveValue(TsvColumn aCol, AnnotationFS aAnnotation, String aValue) {
    // after determining whether the values is a null value.
    if (!NULL_VALUE.equals(aValue)) {
        String value = Escaping.unescapeValue(aValue);
        Feature feat = aAnnotation.getType().getFeatureByBaseName(aCol.uimaFeature.getShortName());
        if (feat == null) {
            throw new IllegalArgumentException("CAS type [" + aAnnotation.getType() + "] does not have a feature called [" + aCol.uimaFeature.getShortName() + "]");
        }
        aAnnotation.setFeatureValueFromString(feat, value);
    }
}
Also used : FSUtil.setFeature(org.apache.uima.fit.util.FSUtil.setFeature) FSUtil.getFeature(org.apache.uima.fit.util.FSUtil.getFeature) Feature(org.apache.uima.cas.Feature)

Example 79 with Feature

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

the class WebannoTsv2Reader method createRelationLayer.

/**
 * Creates a relation layer. For every token, store the governor positions and the dependent
 * annotation
 */
private void createRelationLayer(JCas aJcas, Map<Type, Type> relationayers, Map<Type, Map<String, List<AnnotationFS>>> tokenAnnotations, Map<Type, Map<String, List<String>>> relationTargets) {
    for (Type layer : relationayers.keySet()) {
        if (relationTargets.get(layer) == null) {
            continue;
        }
        Feature dependentFeature = layer.getFeatureByBaseName("Dependent");
        Feature governorFeature = layer.getFeatureByBaseName("Governor");
        Map<String, List<String>> tokenIdMaps = relationTargets.get(layer);
        Map<String, List<AnnotationFS>> tokenAnnos = tokenAnnotations.get(relationayers.get(layer));
        Map<String, List<AnnotationFS>> relationAnnos = tokenAnnotations.get(layer);
        for (String dependnetId : tokenIdMaps.keySet()) {
            int i = 0;
            for (String governorId : tokenIdMaps.get(dependnetId)) {
                AnnotationFS relationAnno = relationAnnos.get(dependnetId).get(i);
                AnnotationFS dependentAnno = tokenAnnos.get(dependnetId).get(0);
                AnnotationFS governorAnno = tokenAnnos.get(governorId).get(0);
                if (layer.getName().equals(Dependency.class.getName())) {
                    Type tokenType = getType(aJcas.getCas(), Token.class.getName());
                    Feature attachFeature = tokenType.getFeatureByBaseName("pos");
                    AnnotationFS posDependentAnno = dependentAnno;
                    dependentAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType, dependentAnno.getBegin(), dependentAnno.getEnd()).get(0);
                    dependentAnno.setFeatureValue(attachFeature, posDependentAnno);
                    AnnotationFS posGovernorAnno = governorAnno;
                    governorAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType, governorAnno.getBegin(), governorAnno.getEnd()).get(0);
                    governorAnno.setFeatureValue(attachFeature, posGovernorAnno);
                }
                // update begin/end of relation annotation
                if (dependentAnno.getEnd() <= governorAnno.getEnd()) {
                    ((Annotation) relationAnno).setBegin(dependentAnno.getBegin());
                    ((Annotation) relationAnno).setEnd(governorAnno.getEnd());
                } else {
                    ((Annotation) relationAnno).setBegin(governorAnno.getBegin());
                    ((Annotation) relationAnno).setEnd(dependentAnno.getEnd());
                }
                relationAnno.setFeatureValue(dependentFeature, dependentAnno);
                relationAnno.setFeatureValue(governorFeature, governorAnno);
                i++;
            }
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) ArrayList(java.util.ArrayList) List(java.util.List) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Feature(org.apache.uima.cas.Feature) Annotation(org.apache.uima.jcas.tcas.Annotation)

Example 80 with Feature

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

the class WebannoTsv2Reader method getLayerAndFeature.

private int getLayerAndFeature(JCas aJcas, int columns, Map<Type, Set<Feature>> spanLayers, Map<Type, Type> relationayers, String line) throws IOException {
    StringTokenizer headerTk = new StringTokenizer(line, "#");
    while (headerTk.hasMoreTokens()) {
        String layerNames = headerTk.nextToken().trim();
        StringTokenizer layerTk = new StringTokenizer(layerNames, "|");
        Set<Feature> features = new LinkedHashSet<>();
        String layerName = layerTk.nextToken().trim();
        Iterator<Type> types = aJcas.getTypeSystem().getTypeIterator();
        boolean layerExists = false;
        while (types.hasNext()) {
            if (types.next().getName().equals(layerName)) {
                layerExists = true;
                break;
            }
        }
        if (!layerExists) {
            throw new IOException(fileName + " This is not a valid TSV File. The layer " + layerName + " is not created in the project.");
        }
        Type layer = CasUtil.getType(aJcas.getCas(), layerName);
        while (layerTk.hasMoreTokens()) {
            String ft = layerTk.nextToken().trim();
            if (ft.startsWith("AttachTo=")) {
                Type attachLayer = CasUtil.getType(aJcas.getCas(), ft.substring(9));
                relationayers.put(layer, attachLayer);
                columns++;
                continue;
            }
            Feature feature = layer.getFeatureByBaseName(ft);
            if (feature == null) {
                throw new IOException(fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName);
            }
            features.add(feature);
            columns++;
        }
        spanLayers.put(layer, features);
    }
    return columns;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringTokenizer(java.util.StringTokenizer) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) IOException(java.io.IOException) 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