Search in sources :

Example 26 with DiffResult

use of de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult in project webanno by webanno.

the class TwoPairedKappaTest method testTwoUserSameAnnotation.

@Test
public void testTwoUserSameAnnotation() throws Exception {
    Map<User, List<SourceDocument>> userDocs = new HashMap<>();
    userDocs.put(user1, asList(document));
    userDocs.put(user2, asList(document));
    Map<User, JCas> userCases = new HashMap<>();
    userCases.put(user1, kappatestCas.getJCas());
    userCases.put(user2, kappatestCas.getJCas());
    Map<SourceDocument, Map<User, JCas>> documentJCases = new HashMap<>();
    documentJCases.put(document, userCases);
    // Check against new impl
    DiffResult diff = CasDiff2.doDiff(POS.class, new SpanDiffAdapter(POS.class, "PosValue"), LinkCompareBehavior.LINK_TARGET_AS_LABEL, convert(userCases));
    AgreementResult agreement = AgreementUtils.getCohenKappaAgreement(diff, POS.class.getName(), "PosValue", convert(userCases));
    // Asserts
    System.out.printf("Agreement: %s%n", agreement.toString());
    diff.print(System.out);
    assertEquals(1.0d, agreement.getAgreement(), 0.000001);
    assertEquals(9, diff.size());
    assertEquals(0, diff.getDifferingConfigurationSets().size());
    assertEquals(0, diff.getIncompleteConfigurationSets().size());
}
Also used : AgreementResult(de.tudarmstadt.ukp.clarin.webanno.curation.agreement.AgreementUtils.AgreementResult) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Arrays.asList(java.util.Arrays.asList) List(java.util.List) DiffResult(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 27 with DiffResult

use of de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult in project webanno by webanno.

the class TwoPairedKappaTest method testTwoUserDiffSpanAnnotation.

@Test
public void testTwoUserDiffSpanAnnotation() throws Exception {
    Map<User, List<SourceDocument>> userDocs = new HashMap<>();
    userDocs.put(user1, asList(document));
    userDocs.put(user2, asList(document));
    Map<User, JCas> userCases = new HashMap<>();
    userCases.put(user1, kappatestCas.getJCas());
    userCases.put(user2, kappaspandiff.getJCas());
    Map<SourceDocument, Map<User, JCas>> documentJCases = new HashMap<>();
    documentJCases.put(document, userCases);
    // Check against new impl
    DiffResult diff = CasDiff2.doDiff(POS.class, new SpanDiffAdapter(POS.class, "PosValue"), LinkCompareBehavior.LINK_TARGET_AS_LABEL, convert(userCases));
    AgreementResult agreement = AgreementUtils.getCohenKappaAgreement(diff, POS.class.getName(), "PosValue", convert(userCases));
    // Asserts
    System.out.printf("Agreement: %s%n", agreement.toString());
    diff.print(System.out);
    assertEquals(0.86153d, agreement.getAgreement(), 0.00001d);
    assertEquals(9, diff.size());
    assertEquals(1, diff.getDifferingConfigurationSets().size());
    assertEquals(0, diff.getIncompleteConfigurationSets().size());
}
Also used : AgreementResult(de.tudarmstadt.ukp.clarin.webanno.curation.agreement.AgreementUtils.AgreementResult) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Arrays.asList(java.util.Arrays.asList) List(java.util.List) DiffResult(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 28 with DiffResult

use of de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult in project webanno by webanno.

the class MergeCas method reMergeCas.

/**
 * Using {@code DiffResult}, determine the annotations to be deleted from the randomly generated
 * MergeCase. The initial Merge CAs is stored under a name {@code CurationPanel#CURATION_USER}.
 * <p>
 * Any similar annotations stacked in a {@code CasDiff2.Position} will be assumed a difference
 * <p>
 * Any two annotation with different value will be assumed a difference
 * <p>
 * Any non stacked empty/null annotations are assumed agreement
 * <p>
 * Any non stacked annotations with similar values for each of the features are assumed
 * agreement
 * <p>
 * Any two link mode / slotable annotations which agree on the base features are assumed
 * agreement
 *
 * @param aDiff
 *            the {@code CasDiff2.DiffResult}
 * @param aJCases
 *            a map of{@code JCas}s for each users and the random merge
 * @return the actual merge {@code JCas}
 */
public static JCas reMergeCas(DiffResult aDiff, Map<String, JCas> aJCases) {
    Set<FeatureStructure> slotFeaturesToReset = new HashSet<>();
    Set<FeatureStructure> annotationsToDelete = new HashSet<>();
    Set<String> users = aJCases.keySet();
    for (Position position : aDiff.getPositions()) {
        Map<String, List<FeatureStructure>> annosPerUser = new HashMap<>();
        ConfigurationSet cfgs = aDiff.getConfigurtionSet(position);
        if (cfgs.getConfigurations(WebAnnoConst.CURATION_USER).isEmpty()) {
            // annotations
            continue;
        }
        AnnotationFS mergeAnno = (AnnotationFS) cfgs.getConfigurations(WebAnnoConst.CURATION_USER).get(0).getFs(WebAnnoConst.CURATION_USER, aJCases);
        // Get Annotations per user in this position
        getAllAnnosOnPosition(aJCases, annosPerUser, users, mergeAnno);
        for (FeatureStructure mergeFs : annosPerUser.get(WebAnnoConst.CURATION_USER)) {
            // incomplete annotations
            if (aJCases.size() != annosPerUser.size()) {
                annotationsToDelete.add(mergeFs);
            } else // agreed and not stacked
            if (isAgree(mergeFs, annosPerUser)) {
                Type t = mergeFs.getType();
                Feature sourceFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
                Feature targetFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
                // Is this a relation?
                if (sourceFeat != null && targetFeat != null) {
                    AnnotationFS source = (AnnotationFS) mergeFs.getFeatureValue(sourceFeat);
                    AnnotationFS target = (AnnotationFS) mergeFs.getFeatureValue(targetFeat);
                    // all span anno on this source positions
                    Map<String, List<FeatureStructure>> sourceAnnosPerUser = new HashMap<>();
                    // all span anno on this target positions
                    Map<String, List<FeatureStructure>> targetAnnosPerUser = new HashMap<>();
                    getAllAnnosOnPosition(aJCases, sourceAnnosPerUser, users, source);
                    getAllAnnosOnPosition(aJCases, targetAnnosPerUser, users, target);
                    if (isAgree(source, sourceAnnosPerUser) && isAgree(target, targetAnnosPerUser)) {
                        slotFeaturesToReset.add(mergeFs);
                    } else {
                        annotationsToDelete.add(mergeFs);
                    }
                } else {
                    slotFeaturesToReset.add(mergeFs);
                }
            } else // disagree or stacked annotations
            {
                annotationsToDelete.add(mergeFs);
            }
        // remove dangling rels
        // setDanglingRelToDel(aJCases.get(CurationPanel.CURATION_USER),
        // mergeFs, annotationsToDelete);
        }
    }
    // remove annotations that do not agree or are a stacked ones
    for (FeatureStructure fs : annotationsToDelete) {
        if (!slotFeaturesToReset.contains(fs)) {
            JCas mergeCas = aJCases.get(WebAnnoConst.CURATION_USER);
            // Check if this difference is on POS, STEM and LEMMA (so remove from the token too)
            Type type = fs.getType();
            int fsBegin = ((AnnotationFS) fs).getBegin();
            int fsEnd = ((AnnotationFS) fs).getEnd();
            if (type.getName().equals(POS.class.getName())) {
                Token t = JCasUtil.selectCovered(mergeCas, Token.class, fsBegin, fsEnd).get(0);
                t.setPos(null);
            }
            if (type.getName().equals(Stem.class.getName())) {
                Token t = JCasUtil.selectCovered(mergeCas, Token.class, fsBegin, fsEnd).get(0);
                t.setStem(null);
            }
            if (type.getName().equals(Lemma.class.getName())) {
                Token t = JCasUtil.selectCovered(mergeCas, Token.class, fsBegin, fsEnd).get(0);
                t.setLemma(null);
            }
            if (type.getName().equals(MorphologicalFeatures.class.getName())) {
                Token t = JCasUtil.selectCovered(mergeCas, Token.class, fsBegin, fsEnd).get(0);
                t.setMorph(null);
            }
            mergeCas.removeFsFromIndexes(fs);
        }
    }
    // if slot bearing annotation, clean
    for (FeatureStructure baseFs : slotFeaturesToReset) {
        for (Feature roleFeature : baseFs.getType().getFeatures()) {
            if (isLinkMode(baseFs, roleFeature)) {
                // FeatureStructure roleFs = baseFs.getFeatureValue(f);
                ArrayFS roleFss = (ArrayFS) WebAnnoCasUtil.getFeatureFS(baseFs, roleFeature.getShortName());
                if (roleFss == null) {
                    continue;
                }
                Map<String, ArrayFS> roleAnnosPerUser = new HashMap<>();
                setAllRoleAnnosOnPosition(aJCases, roleAnnosPerUser, users, baseFs, roleFeature);
                List<FeatureStructure> linkFSes = new LinkedList<>(Arrays.asList(roleFss.toArray()));
                for (FeatureStructure roleFs : roleFss.toArray()) {
                    if (isRoleAgree(roleFs, roleAnnosPerUser)) {
                        for (Feature targetFeature : roleFs.getType().getFeatures()) {
                            if (isBasicFeature(targetFeature)) {
                                continue;
                            }
                            if (!targetFeature.getShortName().equals("target")) {
                                continue;
                            }
                            AnnotationFS targetFs = (AnnotationFS) roleFs.getFeatureValue(targetFeature);
                            if (targetFs == null) {
                                continue;
                            }
                            Map<String, List<FeatureStructure>> targetAnnosPerUser = new HashMap<>();
                            getAllAnnosOnPosition(aJCases, targetAnnosPerUser, users, targetFs);
                            // do not agree on targets
                            if (!isAgree(targetFs, targetAnnosPerUser)) {
                                linkFSes.remove(roleFs);
                            }
                        }
                    } else // do not agree on some role features
                    {
                        linkFSes.remove(roleFs);
                    }
                }
                ArrayFS array = baseFs.getCAS().createArrayFS(linkFSes.size());
                array.copyFromArray(linkFSes.toArray(new FeatureStructure[linkFSes.size()]), 0, 0, linkFSes.size());
                baseFs.setFeatureValue(roleFeature, array);
            }
        }
    }
    return aJCases.get(WebAnnoConst.CURATION_USER);
}
Also used : HashMap(java.util.HashMap) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Feature(org.apache.uima.cas.Feature) WebAnnoCasUtil.setFeature(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Stem(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Stem) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Lemma(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) MorphologicalFeatures(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.morph.MorphologicalFeatures) Position(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.Position) LinkedList(java.util.LinkedList) ConfigurationSet(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.ConfigurationSet) Type(org.apache.uima.cas.Type) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) ArrayFS(org.apache.uima.cas.ArrayFS) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with DiffResult

use of de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult in project webanno by webanno.

the class CopyAnnotationTest method copyLinkToEmptyTest.

@Test
public void copyLinkToEmptyTest() 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");
    FeatureStructure copyFS = DiffUtils.makeLinkFS(mergeCAs, "slot1", 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(0, 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 30 with DiffResult

use of de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult in project webanno by webanno.

the class MergeCasTest method simpleSpanNoDiffWithLabelTest.

@Test
public void simpleSpanNoDiffWithLabelTest() throws Exception {
    Map<String, List<JCas>> casByUser = DiffUtils.loadWebAnnoTSV(null, "mergecas/simplespan/1sentenceposlabel.tsv", "mergecas/simplespan/1sentenceposlabel.tsv");
    List<String> entryTypes = asList(POS.class.getName());
    List<SpanDiffAdapter> diffAdapters = asList(SpanDiffAdapter.POS);
    addRandomMergeCas(casByUser);
    DiffResult result = CasDiff2.doDiff(entryTypes, diffAdapters, LinkCompareBehavior.LINK_TARGET_AS_LABEL, casByUser);
    JCas mergeCas = MergeCas.reMergeCas(result, getSingleCasByUser(casByUser));
    casByUser = new HashMap<>();
    JCas actual = DiffUtils.readWebAnnoTSV("mergecas/simplespan/1sentenceposlabel.tsv", null);
    casByUser.put("actual", asList(actual));
    casByUser.put("merge", asList(mergeCas));
    result = CasDiff2.doDiff(entryTypes, diffAdapters, LinkCompareBehavior.LINK_TARGET_AS_LABEL, casByUser);
    assertEquals(0, result.getDifferingConfigurationSets().size());
    assertEquals(0, result.getIncompleteConfigurationSets().size());
}
Also used : POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) SpanDiffAdapter(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter) JCas(org.apache.uima.jcas.JCas) 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

DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)53 List (java.util.List)53 Arrays.asList (java.util.Arrays.asList)52 Test (org.junit.Test)51 ArrayList (java.util.ArrayList)49 JCas (org.apache.uima.jcas.JCas)45 SpanDiffAdapter (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter)37 LinkedHashMap (java.util.LinkedHashMap)27 AgreementResult (de.tudarmstadt.ukp.clarin.webanno.curation.agreement.AgreementUtils.AgreementResult)24 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)20 ArcDiffAdapter (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.ArcDiffAdapter)13 Type (org.apache.uima.cas.Type)12 Dependency (de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency)11 Map (java.util.Map)9 HashMap (java.util.HashMap)8 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)6 FeatureStructure (org.apache.uima.cas.FeatureStructure)6 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)5 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)5 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)5