Search in sources :

Example 1 with ArrayFS

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

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

the class SlotFeatureSupport method getFeatureValue.

@Override
public <T> T getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) {
    // Get type and features - we need them later in the loop
    Feature linkFeature = aFS.getType().getFeatureByBaseName(aFeature.getName());
    Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
    Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
    Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
    List<LinkWithRoleModel> links = new ArrayList<>();
    ArrayFS array = (ArrayFS) aFS.getFeatureValue(linkFeature);
    if (array != null) {
        for (FeatureStructure link : array.toArray()) {
            LinkWithRoleModel m = new LinkWithRoleModel();
            m.role = link.getStringValue(roleFeat);
            m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
            m.label = ((AnnotationFS) link.getFeatureValue(targetFeat)).getCoveredText();
            links.add(m);
        }
    }
    return (T) links;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayFS(org.apache.uima.cas.ArrayFS) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 3 with ArrayFS

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

the class WebAnnoCasUtil method setLinkFeatureValue.

public static void setLinkFeatureValue(FeatureStructure aFS, Feature aFeature, List<FeatureStructure> linkFSes) {
    // Create a new array if size differs otherwise re-use existing one
    ArrayFS array = (ArrayFS) WebAnnoCasUtil.getFeatureFS(aFS, aFeature.getShortName());
    if (array == null || (array.size() != linkFSes.size())) {
        array = aFS.getCAS().createArrayFS(linkFSes.size());
    }
    // Fill in links
    array.copyFromArray(linkFSes.toArray(new FeatureStructure[linkFSes.size()]), 0, 0, linkFSes.size());
    aFS.setFeatureValue(aFeature, array);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) ArrayFS(org.apache.uima.cas.ArrayFS)

Example 4 with ArrayFS

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

the class MergeCas method setAllRoleAnnosOnPosition.

private static void setAllRoleAnnosOnPosition(Map<String, JCas> aJCases, Map<String, ArrayFS> slotAnnosPerUser, Set<String> aUsers, FeatureStructure aBaseAnno, Feature aFeature) {
    Type t = aBaseAnno.getType();
    int begin = ((AnnotationFS) aBaseAnno).getBegin();
    int end = ((AnnotationFS) aBaseAnno).getEnd();
    for (String user : aUsers) {
        for (AnnotationFS baseFS : selectCovered(aJCases.get(user).getCas(), t, begin, end)) {
            // if non-equal stacked annotations with slot feature exists, get the right one
            if (isSameAnno(aBaseAnno, baseFS)) {
                ArrayFS roleFs = (ArrayFS) WebAnnoCasUtil.getFeatureFS(baseFS, aFeature.getShortName());
                slotAnnosPerUser.put(user, roleFs);
                break;
            }
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) ArrayFS(org.apache.uima.cas.ArrayFS)

Example 5 with ArrayFS

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

the class MergeCasTest method multiLinkWithRoleTargetDifferenceTest.

@Test
public void multiLinkWithRoleTargetDifferenceTest() throws Exception {
    JCas jcasA = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem());
    DiffUtils.makeLinkHostFS(jcasA, 0, 0, DiffUtils.makeLinkFS(jcasA, "slot1", 0, 0));
    JCas jcasB = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem());
    DiffUtils.makeLinkHostFS(jcasB, 0, 0, DiffUtils.makeLinkFS(jcasB, "slot1", 10, 10));
    Map<String, List<JCas>> casByUser = new LinkedHashMap<>();
    casByUser.put("user1", asList(jcasA));
    casByUser.put("user2", asList(jcasB));
    casByUser.put(CURATION_USER, asList(jcasA));
    List<String> entryTypes = asList(DiffUtils.HOST_TYPE);
    SpanDiffAdapter adapter = new SpanDiffAdapter(DiffUtils.HOST_TYPE);
    adapter.addLinkFeature("links", "role", "target");
    List<? extends DiffAdapter> diffAdapters = asList(adapter);
    DiffResult result = CasDiff2.doDiff(entryTypes, diffAdapters, LinkCompareBehavior.LINK_TARGET_AS_LABEL, casByUser);
    result.print(System.out);
    JCas mergeCas = MergeCas.reMergeCas(result, getSingleCasByUser(casByUser));
    Type hostType = mergeCas.getTypeSystem().getType(DiffUtils.HOST_TYPE);
    int numHost = CasUtil.select(mergeCas.getCas(), hostType).size();
    assertEquals(1, numHost);
    for (FeatureStructure host : CasUtil.select(mergeCas.getCas(), hostType)) {
        ArrayFS linkFss = (ArrayFS) WebAnnoCasUtil.getFeatureFS(host, "links");
        assertEquals(0, linkFss.toArray().length);
    }
}
Also used : SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter) JCas(org.apache.uima.jcas.JCas) LinkedHashMap(java.util.LinkedHashMap) FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) ArrayFS(org.apache.uima.cas.ArrayFS) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) DiffResult(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult) Test(org.junit.Test)

Aggregations

ArrayFS (org.apache.uima.cas.ArrayFS)9 FeatureStructure (org.apache.uima.cas.FeatureStructure)8 ArrayList (java.util.ArrayList)5 Type (org.apache.uima.cas.Type)5 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)5 List (java.util.List)4 JCas (org.apache.uima.jcas.JCas)4 DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)3 Arrays.asList (java.util.Arrays.asList)3 ConfigurationSet (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.ConfigurationSet)2 Position (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.Position)2 SpanDiffAdapter (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter)2 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Feature (org.apache.uima.cas.Feature)2 Test (org.junit.Test)2 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)1 WebAnnoCasUtil.getFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getFeature)1 WebAnnoCasUtil.setFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature)1