Search in sources :

Example 36 with Feature

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

the class CopyAnnotationTest method copyLinkToExistingButDiffLinkTest.

@Test
public void copyLinkToExistingButDiffLinkTest() throws Exception {
    JCas mergeCAs = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
    Type type = mergeCAs.getTypeSystem().getType(DiffUtils.HOST_TYPE);
    Feature feature = type.getFeatureByBaseName("f1");
    AnnotationFS mergeFs = DiffUtils.makeLinkHostMultiSPanFeatureFS(mergeCAs, 0, 0, feature, "A", DiffUtils.makeLinkFS(mergeCAs, "slot1", 0, 0));
    FeatureStructure copyFS = DiffUtils.makeLinkFS(mergeCAs, "slot2", 0, 0);
    List<FeatureStructure> linkFs = new ArrayList<>();
    linkFs.add(copyFS);
    WebAnnoCasUtil.setLinkFeatureValue(mergeFs, type.getFeatureByBaseName("links"), linkFs);
    JCas jcasA = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
    DiffUtils.makeLinkHostMultiSPanFeatureFS(jcasA, 0, 0, feature, "A", DiffUtils.makeLinkFS(jcasA, "slot1", 0, 0));
    Map<String, List<JCas>> casByUser = new LinkedHashMap<>();
    casByUser.put("user1", asList(mergeCAs));
    casByUser.put("user2", asList(jcasA));
    List<String> entryTypes = asList(DiffUtils.HOST_TYPE);
    CasDiff2.SpanDiffAdapter adapter = new CasDiff2.SpanDiffAdapter(DiffUtils.HOST_TYPE);
    adapter.addLinkFeature("links", "role", "target");
    List<? extends CasDiff2.DiffAdapter> diffAdapters = asList(adapter);
    CasDiff2.DiffResult diff = CasDiff2.doDiff(entryTypes, diffAdapters, CasDiff2.LinkCompareBehavior.LINK_TARGET_AS_LABEL, casByUser);
    assertEquals(0, diff.getDifferingConfigurationSets().size());
    assertEquals(2, diff.getIncompleteConfigurationSets().size());
}
Also used : CasDiff2(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) LinkedHashMap(java.util.LinkedHashMap) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.Test)

Example 37 with Feature

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

the class CopyAnnotationTest method simpleCopyToDiffExistingAnnoWithStackingTest.

@Test
public void simpleCopyToDiffExistingAnnoWithStackingTest() throws Exception {
    AnnotatorState state = new AnnotatorStateImpl(Mode.CURATION);
    state.setUser(new User());
    posLayer.setAllowStacking(true);
    JCas jcas = JCasFactory.createJCas();
    Type type = jcas.getTypeSystem().getType(POS.class.getTypeName());
    AnnotationFS clickedFs = createPOSAnno(jcas, type, "NN", 0, 0);
    JCas mergeCAs = JCasFactory.createJCas();
    createTokenAnno(mergeCAs, 0, 0);
    AnnotationFS existingFs = mergeCAs.getCas().createAnnotation(type, 0, 0);
    Feature posValue = type.getFeatureByBaseName("PosValue");
    existingFs.setStringValue(posValue, "NE");
    mergeCAs.addFsToIndexes(existingFs);
    MergeCas.addSpanAnnotation(state, annotationSchemaService, posLayer, mergeCAs, clickedFs, true);
    assertEquals(2, CasUtil.selectCovered(mergeCAs.getCas(), type, 0, 0).size());
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) AnnotatorStateImpl(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorStateImpl) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Test(org.junit.Test)

Example 38 with Feature

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

the class CopyAnnotationTest method copySpanWithSlotWithStackingTest.

@Test
public void copySpanWithSlotWithStackingTest() throws Exception {
    AnnotatorState state = new AnnotatorStateImpl(Mode.CURATION);
    state.setUser(new User());
    slotLayer.setAllowStacking(true);
    JCas jcasA = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
    Type type = jcasA.getTypeSystem().getType(DiffUtils.HOST_TYPE);
    Feature feature = type.getFeatureByBaseName("f1");
    AnnotationFS clickedFs = DiffUtils.makeLinkHostMultiSPanFeatureFS(jcasA, 0, 0, feature, "A", DiffUtils.makeLinkFS(jcasA, "slot1", 0, 0));
    JCas mergeCAs = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
    DiffUtils.makeLinkHostMultiSPanFeatureFS(mergeCAs, 0, 0, feature, "C", DiffUtils.makeLinkFS(mergeCAs, "slot1", 0, 0));
    MergeCas.addSpanAnnotation(state, annotationSchemaService, slotLayer, mergeCAs, clickedFs, true);
    assertEquals(2, CasUtil.selectCovered(mergeCAs.getCas(), type, 0, 0).size());
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotatorStateImpl(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorStateImpl) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Test(org.junit.Test)

Example 39 with Feature

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

the class CopyAnnotationTest method simpleCopyRelationToStackedTargetsTest.

@Test
public void simpleCopyRelationToStackedTargetsTest() throws Exception {
    JCas jcas = JCasFactory.createJCas();
    Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());
    Type posType = jcas.getTypeSystem().getType(POS.class.getTypeName());
    AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
    AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);
    AnnotationFS originClicked = createPOSAnno(jcas, posType, "NN", 0, 0);
    AnnotationFS targetClicked = createPOSAnno(jcas, posType, "NN", 1, 1);
    jcas.addFsToIndexes(originClicked);
    jcas.addFsToIndexes(targetClicked);
    originClickedToken.setFeatureValue(originClickedToken.getType().getFeatureByBaseName("pos"), originClicked);
    targetClickedToken.setFeatureValue(targetClickedToken.getType().getFeatureByBaseName("pos"), targetClicked);
    Feature sourceFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
    Feature targetFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
    AnnotationFS clickedFs = jcas.getCas().createAnnotation(type, 0, 1);
    clickedFs.setFeatureValue(sourceFeature, originClickedToken);
    clickedFs.setFeatureValue(targetFeature, targetClickedToken);
    jcas.addFsToIndexes(clickedFs);
    JCas mergeCAs = JCasFactory.createJCas();
    AnnotationFS origin = createPOSAnno(mergeCAs, posType, "NN", 0, 0);
    AnnotationFS target = createPOSAnno(mergeCAs, posType, "NN", 1, 1);
    mergeCAs.addFsToIndexes(origin);
    mergeCAs.addFsToIndexes(target);
    AnnotationFS originToken = createTokenAnno(mergeCAs, 0, 0);
    AnnotationFS targetToken = createTokenAnno(mergeCAs, 1, 1);
    originToken.setFeatureValue(originToken.getType().getFeatureByBaseName("pos"), origin);
    targetToken.setFeatureValue(targetToken.getType().getFeatureByBaseName("pos"), target);
    mergeCAs.addFsToIndexes(originToken);
    mergeCAs.addFsToIndexes(targetToken);
    AnnotationFS origin2 = createPOSAnno(mergeCAs, posType, "NN", 0, 0);
    AnnotationFS target2 = createPOSAnno(mergeCAs, posType, "NN", 1, 1);
    mergeCAs.addFsToIndexes(origin2);
    mergeCAs.addFsToIndexes(target2);
    AnnotationFS originToken2 = createTokenAnno(mergeCAs, 0, 0);
    AnnotationFS targetToken2 = createTokenAnno(mergeCAs, 1, 1);
    originToken2.setFeatureValue(originToken.getType().getFeatureByBaseName("pos"), origin2);
    targetToken2.setFeatureValue(targetToken.getType().getFeatureByBaseName("pos"), target2);
    mergeCAs.addFsToIndexes(originToken2);
    mergeCAs.addFsToIndexes(targetToken2);
    exception.expect(AnnotationException.class);
    MergeCas.addRelationArcAnnotation(mergeCAs, clickedFs, true, false, originToken, targetToken);
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) JCas(org.apache.uima.jcas.JCas) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Test(org.junit.Test)

Example 40 with Feature

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

the class AutomationUtil method automate.

/**
 * Add new annotation to the CAS using the MIRA prediction. This is different from the add
 * methods in the {@link TypeAdapter}s in such a way that the begin and end offsets are always
 * exact so that no need to re-compute
 *
 * @param aJcas
 *            the JCas.
 * @param aFeature
 *            the feature.
 * @param aLabelValues
 *            the values.
 * @throws AnnotationException
 *             if the annotations could not be created/updated.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static void automate(JCas aJcas, AnnotationFeature aFeature, List<String> aLabelValues) throws AnnotationException, IOException {
    String typeName = aFeature.getLayer().getName();
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature.getLayer().getAttachType().getName();
    Type type = CasUtil.getType(aJcas.getCas(), typeName);
    Feature feature = type.getFeatureByBaseName(aFeature.getName());
    int i = 0;
    String prevNe = "O";
    int begin = 0;
    int end = 0;
    // remove existing annotations of this type, after all it is an
    // automation, no care
    clearAnnotations(aJcas, type);
    if (!aFeature.getLayer().isLockToTokenOffset() || aFeature.getLayer().isMultipleTokens()) {
        for (Token token : select(aJcas, Token.class)) {
            String value = aLabelValues.get(i);
            AnnotationFS newAnnotation;
            if (value.equals("O") && prevNe.equals("O")) {
                i++;
                continue;
            } else if (value.equals("O") && !prevNe.equals("O")) {
                newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", ""));
                prevNe = "O";
                aJcas.getCas().addFsToIndexes(newAnnotation);
            } else if (!value.equals("O") && prevNe.equals("O")) {
                begin = token.getBegin();
                end = token.getEnd();
                prevNe = value;
            } else if (!value.equals("O") && !prevNe.equals("O")) {
                if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", "")) && value.startsWith("B-")) {
                    newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                    newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
                    prevNe = value;
                    begin = token.getBegin();
                    end = token.getEnd();
                    aJcas.getCas().addFsToIndexes(newAnnotation);
                } else if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", ""))) {
                    i++;
                    end = token.getEnd();
                    continue;
                } else {
                    newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
                    newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
                    prevNe = value;
                    begin = token.getBegin();
                    end = token.getEnd();
                    aJcas.getCas().addFsToIndexes(newAnnotation);
                }
            }
            i++;
        }
    } else {
        // check if annotation is on an AttachType
        Feature attachFeature = null;
        Type attachType;
        if (attachTypeName != null) {
            attachType = CasUtil.getType(aJcas.getCas(), attachTypeName);
            attachFeature = attachType.getFeatureByBaseName(attachTypeName);
        }
        for (Token token : select(aJcas, Token.class)) {
            AnnotationFS newAnnotation = aJcas.getCas().createAnnotation(type, token.getBegin(), token.getEnd());
            newAnnotation.setFeatureValueFromString(feature, aLabelValues.get(i));
            i++;
            if (attachFeature != null) {
                token.setFeatureValue(attachFeature, newAnnotation);
            }
            aJcas.getCas().addFsToIndexes(newAnnotation);
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Feature(org.apache.uima.cas.Feature) 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