de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency Java Examples

The following examples show how to use de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Mate2StanfordDepConverter.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
  for (Dependency d : JCasUtil.select(jCas, Dependency.class)) {
    
    if (!d.getDependencyType().equals("--")) {
      GrammaticalRelation rel = relmaps.get(d.getDependencyType());
      
      String s = rel.toString();
      d.setDependencyType(s);
    } else {
      if (d.getDependencyType().equals("--") && d.getDependent().getBegin() == d.getGovernor().getBegin() && 
          d.getDependent().getEnd() == d.getGovernor().getEnd()) {
        d.setDependencyType(GrammaticalRelation.ROOT.toString());
        
      }
    }
  }
}
 
Example #2
Source File: CasDiffTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
    public void relationDistanceTest()
        throws Exception
    {
        Map<String, List<CAS>> casByUser = load(
                "casdiff/relationDistance/user1.conll",
                "casdiff/relationDistance/user2.conll");

        List<? extends DiffAdapter> diffAdapters = asList(new RelationDiffAdapter(
                Dependency.class.getName(), "Dependent", "Governor", "DependencyType"));

        CasDiff diff = doDiff(diffAdapters, LINK_TARGET_AS_LABEL, casByUser);
        DiffResult result = diff.toResult();
        
        // result.print(System.out);
        
        assertEquals(27, result.size());
        assertEquals(0, result.getDifferingConfigurationSets().size());
        assertEquals(2, result.getIncompleteConfigurationSets().size());
        
        // Todo: Agreement has moved to separate project - should create agreement test there
//        CodingAgreementResult agreement = getCohenKappaAgreement(diff, entryTypes.get(0),
//                "DependencyType", casByUser);
//        assertEquals(1.0, agreement.getAgreement(), 0.000001d);
//        assertEquals(2, agreement.getIncompleteSetsByPosition().size());
    }
 
Example #3
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testDependencyWithValues() throws Exception 
{
    JCas jcas = makeJCasOneSentence();
    
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
    Token t1 = tokens.get(0);
    Token t2 = tokens.get(1);
    
    POS p1 = new POS(jcas, t1.getBegin(), t1.getEnd());
    p1.setPosValue("POS1");
    p1.addToIndexes();
    t1.setPos(p1);

    POS p2 = new POS(jcas, t2.getBegin(), t2.getEnd());
    p2.setPosValue("POS2");
    p2.addToIndexes();
    t2.setPos(p2);
    
    Dependency dep1 = new Dependency(jcas);
    dep1.setGovernor(t1);
    dep1.setDependent(t2);
    // WebAnno legacy conventions
    // dep1.setBegin(min(dep1.getDependent().getBegin(), dep1.getGovernor().getBegin()));
    // dep1.setEnd(max(dep1.getDependent().getEnd(), dep1.getGovernor().getEnd()));
    // DKPro Core conventions
    dep1.setBegin(dep1.getDependent().getBegin());
    dep1.setEnd(dep1.getDependent().getEnd());
    dep1.addToIndexes();
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList(POS.class),
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList(Dependency.class));
}
 
Example #4
Source File: RemoveDanglingRelationsRepairTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void test()
    throws Exception
{
    JCas jcas = JCasFactory.createJCas();

    jcas.setDocumentText("This is a test.");
    
    Token span1 = new Token(jcas, 0, 4);
    span1.addToIndexes();
    
    Token span2 = new Token(jcas, 6, 8);
    
    Dependency dep = new Dependency(jcas, 0, 8);
    dep.setGovernor(span1);
    dep.setDependent(span2);
    dep.addToIndexes();
    
    List<LogMessage> messages = new ArrayList<>();
    CasDoctor cd = new CasDoctor(RemoveDanglingRelationsRepair.class,
            AllFeatureStructuresIndexedCheck.class);
    // A project is not required for this check
    boolean result = cd.analyze(null, jcas.getCas(), messages);
    // A project is not required for this repair
    cd.repair(null, jcas.getCas(), messages);
    
    assertFalse(result);
    
    messages.forEach(System.out::println);
}
 
Example #5
Source File: LegacyProjectInitializer.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void createDepLayer(Project aProject, TagSet aTagset)
    throws IOException
{
    // Dependency Layer
    AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency",
            RELATION_TYPE, aProject, true, SINGLE_TOKEN, OVERLAP_ONLY);
    AnnotationLayer tokenLayer = annotationSchemaService.findLayer(aProject,
            Token.class.getName());
    List<AnnotationFeature> tokenFeatures = annotationSchemaService
            .listAnnotationFeature(tokenLayer);
    AnnotationFeature tokenPosFeature = null;
    for (AnnotationFeature feature : tokenFeatures) {
        if (feature.getName().equals("pos")) {
            tokenPosFeature = feature;
            break;
        }
    }
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenPosFeature);

    annotationSchemaService.createLayer(depLayer);
    annotationSchemaService
            .createFeature(new AnnotationFeature(aProject, depLayer, "DependencyType",
                    "Relation", CAS.TYPE_NAME_STRING, "Dependency relation", aTagset));

    String[] flavors = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    String[] flavorDesc = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    TagSet flavorsTagset = annotationSchemaService.createTagSet("Dependency flavors",
            "Dependency flavors", "mul", flavors, flavorDesc, aProject);
    
    annotationSchemaService.createFeature(new AnnotationFeature(aProject, depLayer, "flavor",
            "Flavor", CAS.TYPE_NAME_STRING, "Dependency relation", flavorsTagset));
}
 
Example #6
Source File: BratRenderer.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * Scan through the layers once to remember which layers attach to which layers.
 */
private static List<AnnotationLayer> getAttachingLayers(AnnotationLayer aTarget,
        List<AnnotationLayer> aLayers, AnnotationSchemaService aAnnotationService)
{
    List<AnnotationLayer> attachingLayers = new ArrayList<>();

    // Chains always attach to themselves
    if (CHAIN_TYPE.equals(aTarget.getType())) {
        attachingLayers.add(aTarget);
    }

    // FIXME This is a hack! Actually we should check the type of the attachFeature when
    // determine which layers attach to with other layers. Currently we only use attachType,
    // but do not follow attachFeature if it is set.
    if (aTarget.isBuiltIn() && aTarget.getName().equals(POS.class.getName())) {
        attachingLayers.add(aAnnotationService.findLayer(aTarget.getProject(),
                Dependency.class.getName()));
    }

    // Custom layers
    for (AnnotationLayer l : aLayers) {
        if (aTarget.equals(l.getAttachType())) {
            attachingLayers.add(l);
        }
    }

    return attachingLayers;
}
 
Example #7
Source File: CasDiffTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
    public void noDifferencesPosDependencyTest()
        throws Exception
    {
        Map<String, List<CAS>> casByUser = load(
                "casdiff/noDifferences/data.conll",
                "casdiff/noDifferences/data.conll");

        List<? extends DiffAdapter> diffAdapters = asList(POS_DIFF_ADAPTER,
                DEPENDENCY_DIFF_ADAPTER);

        CasDiff diff = doDiff(diffAdapters, LINK_TARGET_AS_LABEL, casByUser);
        DiffResult result = diff.toResult();

        // result.print(System.out);
        
        assertEquals(52, result.size());
        assertEquals(26, result.size(POS.class.getName()));
        assertEquals(26, result.size(Dependency.class.getName()));
        assertEquals(0, result.getDifferingConfigurationSets().size());
        assertEquals(0, result.getIncompleteConfigurationSets().size());

        // Todo: Agreement has moved to separate project - should create agreement test there
//        CodingAgreementResult agreement = AgreementUtils.getCohenKappaAgreement(diff,
//                entryTypes.get(0), "PosValue", casByUser);
//        assertEquals(1.0d, agreement.getAgreement(), 0.000001d);
//        assertEquals(0, agreement.getIncompleteSetsByPosition().size());
    }
 
Example #8
Source File: Tsv3XSerializerTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testRelation() throws Exception
{
    // Create test document
    JCas cas = makeJCasOneSentence("This is a test .");
    List<Token> tokens = new ArrayList<>(select(cas, Token.class));
    Dependency dep = new Dependency(cas);
    dep.setGovernor(tokens.get(0));
    dep.setDependent(tokens.get(1));
    dep.setDependencyType("dep");
    dep.setBegin(dep.getDependent().getBegin());
    dep.setEnd(dep.getDependent().getEnd());
    dep.addToIndexes();
    
    // Set up TSV schema
    TsvSchema schema = new TsvSchema();
    Type dependencyType = cas.getCasType(Dependency.type);
    schema.addColumn(new TsvColumn(dependencyType, LayerType.RELATION, "DependencyType",
            FeatureType.PRIMITIVE));
    schema.addColumn(new TsvColumn(dependencyType, LayerType.RELATION, "Governor",
            FeatureType.RELATION_REF));

    // Convert test document content to TSV model
    TsvDocument doc = Tsv3XCasDocumentBuilder.of(schema, cas);
    doc.getSentences().get(0).getTokens().get(1).addUimaAnnotation(dep, false);

    assertEquals(
            join(asList("1-1\t0-4\tThis\t_\t_\t", "1-2\t5-7\tis\tdep\t1-1\t"), "\n"),
            join(asList(doc.getToken(0, 0), doc.getToken(0, 1)), "\n"));
    
    String expectedSentence = 
            "#Text=This is a test .\n" + 
            "1-1\t0-4\tThis\t_\t_\t\n" + 
            "1-2\t5-7\tis\tdep\t1-1\t\n" + 
            "1-3\t8-9\ta\t_\t_\t\n" + 
            "1-4\t10-14\ttest\t_\t_\t\n" + 
            "1-5\t15-16\t.\t_\t_\t\n";
    assertEquals(expectedSentence, doc.getSentences().get(0).toString());
}
 
Example #9
Source File: CasDiffTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
    public void relationLabelTest()
        throws Exception
    {
        Map<String, List<CAS>> casByUser = new HashMap<>();
        casByUser.put("user1",
                asList(loadWebAnnoTsv3("testsuite/" + testContext.getMethodName() + "/user1.tsv")
                        .getCas()));
        casByUser.put("user2",
                asList(loadWebAnnoTsv3("testsuite/" + testContext.getMethodName() + "/user2.tsv")
                        .getCas()));

        List<? extends DiffAdapter> diffAdapters = asList(new RelationDiffAdapter(
                Dependency.class.getName(), "Dependent", "Governor", "DependencyType"));

        CasDiff diff = doDiff(diffAdapters, LINK_TARGET_AS_LABEL, casByUser);
        DiffResult result = diff.toResult();

        // result.print(System.out);
        
        assertEquals(26, result.size());
        assertEquals(1, result.getDifferingConfigurationSets().size());
        assertEquals(0, result.getIncompleteConfigurationSets().size());
        
        // Todo: Agreement has moved to separate project - should create agreement test there
//        CodingAgreementResult agreement = getCohenKappaAgreement(diff, entryTypes.get(0),
//                "DependencyType", casByUser);
//        assertEquals(0.958199d, agreement.getAgreement(), 0.000001d);
//        assertEquals(0, agreement.getIncompleteSetsByPosition().size());
    }
 
Example #10
Source File: DependencyLayerInitializer.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(Project aProject) throws IOException
{
    TagSet depTagSet = JsonImportUtil.importTagSetFromJson(aProject,
            new ClassPathResource("/tagsets/mul-dep-ud2.json").getInputStream(),
            annotationSchemaService);
    
    // Dependency Layer
    AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency",
            RELATION_TYPE, aProject, true, SINGLE_TOKEN, OVERLAP_ONLY);
    AnnotationLayer tokenLayer = annotationSchemaService.findLayer(aProject,
            Token.class.getName());
    List<AnnotationFeature> tokenFeatures = annotationSchemaService
            .listAnnotationFeature(tokenLayer);
    AnnotationFeature tokenPosFeature = null;
    for (AnnotationFeature feature : tokenFeatures) {
        if (feature.getName().equals("pos")) {
            tokenPosFeature = feature;
            break;
        }
    }
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenPosFeature);

    annotationSchemaService.createLayer(depLayer);
    annotationSchemaService
            .createFeature(new AnnotationFeature(aProject, depLayer, "DependencyType",
                    "Relation", CAS.TYPE_NAME_STRING, "Dependency relation", depTagSet));

    String[] flavors = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    String[] flavorDesc = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    TagSet flavorsTagset = annotationSchemaService.createTagSet("Dependency flavors",
            "Dependency flavors", "mul", flavors, flavorDesc, aProject);

    annotationSchemaService.createFeature(new AnnotationFeature(aProject, depLayer, "flavor",
            "Flavor", CAS.TYPE_NAME_STRING, "Dependency relation", flavorsTagset));
}
 
Example #11
Source File: TwoPairedKappaTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoUserDiffArcAnnotation()
    throws Exception
{
    Map<User, List<SourceDocument>> userDocs = new HashMap<>();
    userDocs.put(user1, asList(document));
    userDocs.put(user2, asList(document));
    
    Map<User, CAS> userCases = new HashMap<>();
    userCases.put(user1, kappatestCas);
    userCases.put(user2, kappaarcdiff);
    
    Map<SourceDocument, Map<User, CAS>> documentJCases = new HashMap<>();
    documentJCases.put(document, userCases);
    
    // Check against new impl
    CasDiff diff = doDiff(asList(DEPENDENCY_DIFF_ADAPTER), LINK_TARGET_AS_LABEL,
            convert(userCases));
    DiffResult result = diff.toResult();
    AgreementResult agreement = getCohenKappaAgreement(diff,
            Dependency.class.getName(), "DependencyType", convert(userCases));
    
    // Asserts
    System.out.printf("Agreement: %s%n", agreement.toString());
    result.print(System.out);
    
    assertEquals(0.86153d, agreement.getAgreement(), 0.00001d);
    assertEquals(9, result.size());
    assertEquals(1, result.getDifferingConfigurationSets().size());
    assertEquals(0, result.getIncompleteConfigurationSets().size());
}
 
Example #12
Source File: TwoPairedKappaTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoUserDiffArcAndSpanAnnotation()
    throws Exception
{
    Map<User, List<SourceDocument>> userDocs = new HashMap<>();
    userDocs.put(user1, asList(document));
    userDocs.put(user2, asList(document));
    
    Map<User, CAS> userCases = new HashMap<>();
    userCases.put(user1, kappatestCas);
    userCases.put(user2, kappaspanarcdiff);

    Map<SourceDocument, Map<User, CAS>> documentJCases = new HashMap<>();
    documentJCases.put(document, userCases);
    
    // Check against new impl
    CasDiff diff = doDiff(asList(DEPENDENCY_DIFF_ADAPTER), LINK_TARGET_AS_LABEL,
            convert(userCases));
    DiffResult result = diff.toResult();
    CodingAgreementResult agreement = getCohenKappaAgreement(diff,
            Dependency.class.getName(), "DependencyType", convert(userCases));
    
    // Asserts
    System.out.printf("Agreement: %s%n", agreement.toString());
    result.print(System.out);
    AgreementUtils.dumpAgreementStudy(System.out, agreement);
    
    assertEquals(0.86153d, agreement.getAgreement(), 0.00001d);
    assertEquals(9, result.size());
    assertEquals(1, result.getDifferingConfigurationSets().size());
    assertEquals(0, result.getIncompleteConfigurationSets().size());
}
 
Example #13
Source File: NoMultipleIncomingRelationsCheckTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testFail() throws Exception
{

    AnnotationLayer relationLayer = new AnnotationLayer();
    relationLayer.setName(Dependency.class.getName());

    relationLayer.setType(WebAnnoConst.RELATION_TYPE);
    Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull()))
            .thenReturn(Arrays.asList(relationLayer));

    JCas jcas = JCasFactory.createJCas();

    jcas.setDocumentText("This is a test.");

    Token spanThis = new Token(jcas, 0, 4);
    spanThis.addToIndexes();

    Token spanIs = new Token(jcas, 5, 7);
    spanIs.addToIndexes();

    Token spanA = new Token(jcas, 8, 9);
    spanA.addToIndexes();

    Dependency dep1 = new Dependency(jcas, 0, 7);
    dep1.setGovernor(spanThis);
    dep1.setDependent(spanIs);
    dep1.addToIndexes();

    Dependency dep2 = new Dependency(jcas, 0, 9);
    dep2.setGovernor(spanA);
    dep2.setDependent(spanIs);
    dep2.addToIndexes();

    List<LogMessage> messages = new ArrayList<>();

    boolean result = check.check(null, jcas.getCas(), messages);

    messages.forEach(System.out::println);

    assertTrue(result);

    // also check the message itself
    assertEquals(1, messages.size());
    assertEquals(
            "[NoMultipleIncomingRelationsCheck] Relation [This] -> [is] points to span that already has an incoming relation [a] -> [is].",
            messages.get(0).toString());

}
 
Example #14
Source File: CasMergeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleCopyRelationToEmptyAnnoTest()
    throws Exception
{
    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());

    AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
    AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);

    AnnotationFS originClicked = createPOSAnno(jcas, "NN", 0, 0);
    AnnotationFS targetClicked = createPOSAnno(jcas, "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(FEAT_REL_SOURCE);
    Feature targetFeature = type.getFeatureByBaseName(FEAT_REL_TARGET);

    AnnotationFS clickedFs = jcas.createAnnotation(type, 0, 1);
    clickedFs.setFeatureValue(sourceFeature, originClickedToken);
    clickedFs.setFeatureValue(targetFeature, targetClickedToken);
    jcas.addFsToIndexes(clickedFs);

    CAS mergeCAs = createJCas().getCas();
    AnnotationFS origin = createPOSAnno(mergeCAs, "NN", 0, 0);
    AnnotationFS target = createPOSAnno(mergeCAs, "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);

    sut.mergeRelationAnnotation(null, null, depLayer, mergeCAs, clickedFs, false);
    
    assertEquals(1, selectCovered(mergeCAs, type, 0, 1).size());
}
 
Example #15
Source File: CasMergeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleCopyRelationToStackedTargetsTest()
    throws Exception
{
    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());

    AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
    AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);

    AnnotationFS originClicked = createPOSAnno(jcas, "NN", 0, 0);
    AnnotationFS targetClicked = createPOSAnno(jcas, "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(FEAT_REL_SOURCE);
    Feature targetFeature = type.getFeatureByBaseName(FEAT_REL_TARGET);

    AnnotationFS clickedFs = jcas.createAnnotation(type, 0, 1);
    clickedFs.setFeatureValue(sourceFeature, originClickedToken);
    clickedFs.setFeatureValue(targetFeature, targetClickedToken);
    jcas.addFsToIndexes(clickedFs);

    CAS mergeCAs = createJCas().getCas();
    AnnotationFS origin = createPOSAnno(mergeCAs, "NN", 0, 0);
    AnnotationFS target = createPOSAnno(mergeCAs, "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, "NN", 0, 0);
    AnnotationFS target2 = createPOSAnno(mergeCAs, "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);

    assertThatExceptionOfType(AnnotationException.class)
            .isThrownBy(() -> sut.mergeRelationAnnotation(null, null, depLayer, mergeCAs, 
                    clickedFs, false))
            .withMessageContaining("Stacked sources exist");
}
 
Example #16
Source File: CasMergeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void thatMergingRelationIsRejectedIfAlreadyExists()
    throws Exception
{
    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());

    AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
    AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);

    AnnotationFS originClicked = createPOSAnno(jcas, "NN", 0, 0);
    AnnotationFS targetClicked = createPOSAnno(jcas, "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(FEAT_REL_SOURCE);
    Feature targetFeature = type.getFeatureByBaseName(FEAT_REL_TARGET);

    AnnotationFS clickedFs = jcas.createAnnotation(type, 0, 1);
    clickedFs.setFeatureValue(sourceFeature, originClickedToken);
    clickedFs.setFeatureValue(targetFeature, targetClickedToken);
    jcas.addFsToIndexes(clickedFs);

    CAS mergeCAs = createJCas().getCas();
    AnnotationFS origin = createPOSAnno(mergeCAs, "NN", 0, 0);
    AnnotationFS target = createPOSAnno(mergeCAs, "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 existing = mergeCAs.createAnnotation(type, 0, 1);
    existing.setFeatureValue(sourceFeature, originToken);
    existing.setFeatureValue(targetFeature, targetToken);
    mergeCAs.addFsToIndexes(existing);

    assertThatExceptionOfType(AnnotationException.class)
            .isThrownBy(() -> sut.mergeRelationAnnotation(null, null, depLayer, mergeCAs, 
                    clickedFs, false))
            .withMessageContaining("annotation already exists");
}
 
Example #17
Source File: FixAttachFeature330.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void doMigration()
{
    long start = System.currentTimeMillis();
    
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("migrationRoot");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    
    TransactionStatus status = null;
    try {
        status = txManager.getTransaction(def);

        for (Project project : projectService.listProjects()) {
            try {
                AnnotationLayer tokenLayer = annotationSchemaService.findLayer(
                        project, Token.class.getName());
                
                // Set attach-feature of Dependency layer to Token.pos if necessary
                fix(project, Dependency.class, RELATION_TYPE, tokenLayer, "pos");

                // Set attach-feature of POS layer to Token.pos if necessary
                fix(project, POS.class, SPAN_TYPE, tokenLayer, "pos");

                // Set attach-feature of Lemma layer to Token.lemma if necessary
                fix(project, Lemma.class, SPAN_TYPE, tokenLayer, "lemma");

                // Set attach-feature of MorphologicalFeatures layer to Token.morph if necessary
                fix(project, MorphologicalFeatures.class, SPAN_TYPE, tokenLayer, "morph");
            }
            catch (NoResultException e) {
                // This only happens if a project is not fully set up. Every project
                // should have a Token layer. However, it is not the responsibility of this
                // migration to enforce this, so we just ignore it.
                log.warn("Project {} does not seem to include a Token layer!", project);
            }
        }
        
        txManager.commit(status);
    }
    finally {
        if (status != null && !status.isCompleted()) {
            txManager.rollback(status);
        }
    }
    
    log.info("Migration [" + getClass().getSimpleName() + "] took {}ms",
            System.currentTimeMillis() - start);
}
 
Example #18
Source File: ConstraintsGeneratorTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimplePath()
    throws Exception
{
    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream(
            "src/test/resources/rules/10.rules"));
    Parse p = parser.Parse();

    ParsedConstraints constraints = p.accept(new ParserVisitor());

    JCas jcas = JCasFactory.createJCas();
    jcas.setDocumentText("The sun.");

    // Add token annotations
    Token t_the = new Token(jcas, 0, 3);
    t_the.addToIndexes();
    Token t_sun = new Token(jcas, 0, 3);
    t_sun.addToIndexes();

    // Add POS annotations and link them to the tokens
    POS p_the = new POS(jcas, t_the.getBegin(), t_the.getEnd());
    p_the.setPosValue("DET");
    p_the.addToIndexes();
    t_the.setPos(p_the);
    POS p_sun = new POS(jcas, t_sun.getBegin(), t_sun.getEnd());
    p_sun.setPosValue("NN");
    p_sun.addToIndexes();
    t_sun.setPos(p_sun);

    // Add dependency annotations
    Dependency dep_the_sun = new Dependency(jcas);
    dep_the_sun.setGovernor(t_sun);
    dep_the_sun.setDependent(t_the);
    dep_the_sun.setDependencyType("det");
    dep_the_sun.setBegin(dep_the_sun.getGovernor().getBegin());
    dep_the_sun.setEnd(dep_the_sun.getGovernor().getEnd());
    dep_the_sun.addToIndexes();

    Evaluator constraintsEvaluator = new ValuesGenerator();

    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(
            dep_the_sun, "DependencyType", constraints);

    List<PossibleValue> expectedOutput = new LinkedList<>();
    expectedOutput.add(new PossibleValue("det", false));

    assertEquals(expectedOutput, possibleValues);
}
 
Example #19
Source File: TwoPairedKappaTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreeUserDiffArcAndSpanAnnotation()
    throws Exception
{
    Map<User, List<SourceDocument>> userDocs = new HashMap<>();
    userDocs.put(user1, asList(document));
    userDocs.put(user2, asList(document));
    userDocs.put(user3, asList(document));
    
    Map<User, CAS> userCases = new HashMap<>();
    userCases.put(user1, kappatestCas);
    userCases.put(user2, kappaspandiff);
    userCases.put(user3, kappaspanarcdiff);
    
    Map<SourceDocument, Map<User, CAS>> documentJCases = new HashMap<>();
    documentJCases.put(document, userCases);

    // Check against new impl
    CasDiff diff = doDiff(asList(POS_DIFF_ADAPTER, DEPENDENCY_DIFF_ADAPTER),
            LINK_TARGET_AS_LABEL, convert(userCases));
    DiffResult result = diff.toResult();
    
    Map<String, List<CAS>> user1and2 = convert(userCases);
    user1and2.remove("user3");
    AgreementResult agreement12 = getCohenKappaAgreement(diff,
            Dependency.class.getName(), "DependencyType", user1and2);

    Map<String, List<CAS>> user2and3 = convert(userCases);
    user2and3.remove("user1");
    AgreementResult agreement23 = getCohenKappaAgreement(diff,
            Dependency.class.getName(), "DependencyType", user2and3);

    Map<String, List<CAS>> user1and3 = convert(userCases);
    user1and3.remove("user2");
    AgreementResult agreement13 = getCohenKappaAgreement(diff,
            Dependency.class.getName(), "DependencyType", user1and3);

    // Asserts
    result.print(System.out);
    
    System.out.printf("New agreement 1/2: %s%n", agreement12.toString());
    System.out.printf("New agreement 2/3: %s%n", agreement23.toString());
    System.out.printf("New agreement 1/3: %s%n", agreement13.toString());
}
 
Example #20
Source File: NoMultipleIncomingRelationsCheckTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testOkBecauseCoref() throws Exception
{

    AnnotationLayer relationLayer = new AnnotationLayer();
    relationLayer.setName(CoreferenceChain.class.getName());

    relationLayer.setType(WebAnnoConst.CHAIN_TYPE);
    Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull()))
            .thenReturn(Arrays.asList(relationLayer));

    JCas jcas = JCasFactory.createJCas();

    jcas.setDocumentText("This is a test.");

    Token spanThis = new Token(jcas, 0, 4);
    spanThis.addToIndexes();

    Token spanIs = new Token(jcas, 6, 8);
    spanIs.addToIndexes();

    Token spanA = new Token(jcas, 9, 10);
    spanA.addToIndexes();

    Dependency dep1 = new Dependency(jcas, 0, 8);
    dep1.setGovernor(spanThis);
    dep1.setDependent(spanIs);
    dep1.addToIndexes();

    Dependency dep2 = new Dependency(jcas, 0, 10);
    dep2.setGovernor(spanA);
    dep2.setDependent(spanIs);
    dep2.addToIndexes();

    List<LogMessage> messages = new ArrayList<>();

    boolean result = check.check(null, jcas.getCas(), messages);

    messages.forEach(System.out::println);

    assertTrue(result);
}
 
Example #21
Source File: NoMultipleIncomingRelationsCheckTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testOK() throws Exception
{
    AnnotationLayer relationLayer = new AnnotationLayer();
    relationLayer.setName(Dependency.class.getName());

    relationLayer.setType(WebAnnoConst.RELATION_TYPE);
    Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull()))
            .thenReturn(Arrays.asList(relationLayer));

    JCas jcas = JCasFactory.createJCas();

    jcas.setDocumentText("This is a test.");

    Token spanThis = new Token(jcas, 0, 4);
    spanThis.addToIndexes();

    Token spanIs = new Token(jcas, 6, 8);
    spanIs.addToIndexes();

    Token spanA = new Token(jcas, 9, 10);
    spanA.addToIndexes();

    Dependency dep1 = new Dependency(jcas, 0, 8);
    dep1.setGovernor(spanThis);
    dep1.setDependent(spanIs);
    dep1.addToIndexes();

    Dependency dep2 = new Dependency(jcas, 6, 10);
    dep2.setGovernor(spanIs);
    dep2.setDependent(spanA);
    dep2.addToIndexes();

    List<LogMessage> messages = new ArrayList<>();

    boolean result = check.check(null, jcas.getCas(), messages);

    messages.forEach(System.out::println);

    assertTrue(result);
}
 
Example #22
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception
{
    if (jcas == null) {
        jcas = JCasFactory.createJCas();
    }
    else {
        jcas.reset();
    }
    
    username = "user";
    
    project = new Project();
    project.setId(1l);
    project.setMode(PROJECT_TYPE_ANNOTATION);
    
    document = new SourceDocument();
    document.setId(1l);
    document.setProject(project);
    
    // Set up annotation schema with POS and Dependency
    AnnotationLayer tokenLayer = new AnnotationLayer(Token.class.getName(), "Token", SPAN_TYPE,
            project, true, SINGLE_TOKEN, NO_OVERLAP);
    tokenLayer.setId(1l);
    AnnotationFeature tokenLayerPos = new AnnotationFeature(1l, tokenLayer, "pos",
            POS.class.getName());

    AnnotationLayer posLayer = new AnnotationLayer(POS.class.getName(), "POS", SPAN_TYPE,
            project, true, SINGLE_TOKEN, NO_OVERLAP);
    posLayer.setId(2l);

    depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency", RELATION_TYPE,
            project, true, SINGLE_TOKEN, OVERLAP_ONLY);
    depLayer.setId(3l);
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenLayerPos);
    dependencyLayerGovernor = new AnnotationFeature(2l, depLayer, "Governor",
            Token.class.getName());
    dependencyLayerDependent = new AnnotationFeature(3l, depLayer, "Dependent",
            Token.class.getName());

    layerSupportRegistry = new LayerSupportRegistryImpl(asList());
    featureSupportRegistry = new FeatureSupportRegistryImpl(asList());
    
    behaviors = asList(new RelationAttachmentBehavior(), new RelationOverlapBehavior(),
            new RelationCrossSentenceBehavior());
}
 
Example #23
Source File: RelationRendererTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception
{
    if (jcas == null) {
        jcas = JCasFactory.createJCas();
    }
    else {
        jcas.reset();
    }
    
    username = "user";
    
    project = new Project();
    project.setId(1l);
    project.setMode(PROJECT_TYPE_ANNOTATION);
    
    document = new SourceDocument();
    document.setId(1l);
    document.setProject(project);
    
    // Set up annotation schema with POS and Dependency
    AnnotationLayer tokenLayer = new AnnotationLayer(Token.class.getName(), "Token", SPAN_TYPE,
            project, true, SINGLE_TOKEN, NO_OVERLAP);
    tokenLayer.setId(1l);
    AnnotationFeature tokenLayerPos = new AnnotationFeature(1l, tokenLayer, "pos",
            POS.class.getName());

    AnnotationLayer posLayer = new AnnotationLayer(POS.class.getName(), "POS", SPAN_TYPE,
            project, true, SINGLE_TOKEN, NO_OVERLAP);
    posLayer.setId(2l);

    depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency", RELATION_TYPE,
            project, true, SINGLE_TOKEN, OVERLAP_ONLY);
    depLayer.setId(3l);
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenLayerPos);
    dependencyLayerGovernor = new AnnotationFeature(2l, depLayer, "Governor",
            Token.class.getName());
    dependencyLayerDependent = new AnnotationFeature(3l, depLayer, "Dependent",
            Token.class.getName());

    featureSupportRegistry = new FeatureSupportRegistryImpl(asList());
    layerSupportRegistry = new LayerSupportRegistryImpl(asList());
    
    behaviors = asList(new RelationAttachmentBehavior(), new RelationOverlapBehavior(),
            new RelationCrossSentenceBehavior());
}
 
Example #24
Source File: WebAnnoTsv3XReaderWriterTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void test()
    throws Exception
{
    String targetFolder = "target/test-output/" + testContext.getTestOutputFolderName();
    
    CollectionReader reader = CollectionReaderFactory.createReader(
            WebannoTsv3XReader.class,
            WebannoTsv3XReader.PARAM_SOURCE_LOCATION, "src/test/resources/tsv3/",
            WebannoTsv3XReader.PARAM_PATTERNS, "coref.tsv");

    AnalysisEngineDescription writer = createEngineDescription(
            WebannoTsv3XWriter.class,
            WebannoTsv3XWriter.PARAM_TARGET_LOCATION, targetFolder,
            WebannoTsv3XWriter.PARAM_STRIP_EXTENSION, true,
            WebannoTsv3XWriter.PARAM_OVERWRITE, true);

    runPipeline(reader, writer);

    CollectionReader reader1 = CollectionReaderFactory.createReader(
            WebannoTsv3XReader.class,
            WebannoTsv3XReader.PARAM_SOURCE_LOCATION, "src/test/resources/tsv3/",
            WebannoTsv3XReader.PARAM_PATTERNS, "coref.tsv");

    CollectionReader reader2 = CollectionReaderFactory.createReader(
            WebannoTsv3XReader.class,
            WebannoTsv3XReader.PARAM_SOURCE_LOCATION, targetFolder,
            WebannoTsv3XReader.PARAM_PATTERNS, "coref.tsv");

    CAS cas1 = JCasFactory.createJCas().getCas();
    reader1.getNext(cas1);

    CAS cas2 = JCasFactory.createJCas().getCas();
    reader2.getNext(cas2);

    assertEquals(JCasUtil.select(cas2.getJCas(), Token.class).size(),
            JCasUtil.select(cas1.getJCas(), Token.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), POS.class).size(),
            JCasUtil.select(cas1.getJCas(), POS.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Lemma.class).size(),
            JCasUtil.select(cas1.getJCas(), Lemma.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), NamedEntity.class).size(),
            JCasUtil.select(cas1.getJCas(), NamedEntity.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Sentence.class).size(),
            JCasUtil.select(cas1.getJCas(), Sentence.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Dependency.class).size(),
            JCasUtil.select(cas1.getJCas(), Dependency.class).size());
}
 
Example #25
Source File: WebAnnoTsv3ReaderWriterTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void test()
    throws Exception
{
    String targetFolder = "target/test-output/" + testContext.getTestOutputFolderName();
    
    CollectionReader reader = CollectionReaderFactory.createReader(
            WebannoTsv3Reader.class,
            WebannoTsv3Reader.PARAM_SOURCE_LOCATION, "src/test/resources/tsv3/",
            WebannoTsv3Reader.PARAM_PATTERNS, "coref.tsv");

    List<String> slotFeatures = new ArrayList<>();
    List<String> slotTargets = new ArrayList<>();
    List<String> linkTypes = new ArrayList<>();
    List<String> spanLayers = new ArrayList<>();
    spanLayers.add(NamedEntity.class.getName());
    spanLayers.add(POS.class.getName());
    spanLayers.add(Lemma.class.getName());
    List<String> chainLayers = new ArrayList<>();
    chainLayers.add("de.tudarmstadt.ukp.dkpro.core.api.coref.type.Coreference");
    List<String> relationLayers = new ArrayList<>();
    relationLayers.add(Dependency.class.getName());

    AnalysisEngineDescription writer = createEngineDescription(
            WebannoTsv3Writer.class,
            WebannoTsv3Writer.PARAM_TARGET_LOCATION, targetFolder,
            WebannoTsv3Writer.PARAM_STRIP_EXTENSION, true, 
            WebannoTsv3Writer.PARAM_OVERWRITE, true, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, spanLayers, 
            WebannoTsv3Writer.PARAM_SLOT_FEATS, slotFeatures,
            WebannoTsv3Writer.PARAM_SLOT_TARGETS, slotTargets,
            WebannoTsv3Writer.PARAM_LINK_TYPES, linkTypes, 
            WebannoTsv3Writer.PARAM_CHAIN_LAYERS, chainLayers,
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, relationLayers);

    runPipeline(reader, writer);

    CollectionReader reader1 = CollectionReaderFactory.createReader(
            WebannoTsv3Reader.class,
            WebannoTsv3Reader.PARAM_SOURCE_LOCATION, "src/test/resources/tsv3/",
            WebannoTsv3Reader.PARAM_PATTERNS, "coref.tsv");

    CollectionReader reader2 = CollectionReaderFactory.createReader(
            WebannoTsv3Reader.class,
            WebannoTsv3Reader.PARAM_SOURCE_LOCATION, targetFolder,
            WebannoTsv3Reader.PARAM_PATTERNS, "coref.tsv");

    CAS cas1 = JCasFactory.createJCas().getCas();
    reader1.getNext(cas1);

    CAS cas2 = JCasFactory.createJCas().getCas();
    reader2.getNext(cas2);

    assertEquals(JCasUtil.select(cas2.getJCas(), Token.class).size(),
            JCasUtil.select(cas1.getJCas(), Token.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), POS.class).size(),
            JCasUtil.select(cas1.getJCas(), POS.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Lemma.class).size(),
            JCasUtil.select(cas1.getJCas(), Lemma.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), NamedEntity.class).size(),
            JCasUtil.select(cas1.getJCas(), NamedEntity.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Sentence.class).size(),
            JCasUtil.select(cas1.getJCas(), Sentence.class).size());
    assertEquals(JCasUtil.select(cas2.getJCas(), Dependency.class).size(),
            JCasUtil.select(cas1.getJCas(), Dependency.class).size());
}
 
Example #26
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void setRelationAnnotation(JCas aJCas)
{
    for (String l : relationLayers) {
        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);
        Feature dependentFeature = null;
        Feature governorFeature = null;

        List<Feature> features = type.getFeatures();
        Collections.sort(features, (a, b) -> 
                StringUtils.compare(a.getShortName(), b.getShortName()));
        for (Feature feature : features) {
            if (feature.getShortName().equals(DEPENDENT)) {

                // check if the dependent is
                dependentFeature = feature;
            }
            if (feature.getShortName().equals(GOVERNOR)) {
                governorFeature = feature;
            }
        }
        for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
            AnnotationFS depFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            AnnotationFS govFs = (AnnotationFS) fs.getFeatureValue(governorFeature);

            Type govType = govFs.getType();

            AnnotationUnit govUnit = getFirstUnit(
                    getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(govUnit) == null) {
                govUnit = getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText());
            }

            AnnotationUnit depUnit = getFirstUnit(
                    getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(depUnit) == null) {
                depUnit = getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText());
            }
            // Since de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency is over
            // Over POS anno which itself attached to Token, we need the POS type here

            if (type.getName().equals(Dependency.class.getName())) {
                govType = aJCas.getCas().getTypeSystem().getType(POS.class.getName());
            }

            int govRef = 0;
            int depRef = 0;

            // For that unit test case only, where annotations are on Tokens.
            // The WebAnno world do not ever process Token as an annotation
            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(govUnit).equals(true)) {
                govRef = annotaionRefPerType.get(govType).get(govFs);
            }

            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(depUnit).equals(true)) {
                depRef = annotaionRefPerType.get(govType).get(depFs);
            }

            setRelationAnnoPerFeature(annotationsPertype, type, fs, depUnit, govUnit, govRef,
                    depRef, govType);

        }
        if (annotationsPertype.keySet().size() > 0) {
            annotationsPerPostion.put(l, annotationsPertype);
        }
    }
}
 
Example #27
Source File: Tsv3XCasDocumentBuilder.java    From webanno with Apache License 2.0 4 votes vote down vote up
private static void scanUnitForActiveColumns(TsvUnit aUnit)
    {
        for (TsvColumn col : aUnit.getDocument().getSchema().getColumns()) {
            List<AnnotationFS> annotationsForColumn = aUnit.getAnnotationsForColumn(col);
            if (!annotationsForColumn.isEmpty()) {
//                if (SPAN.equals(col.layerType) && SLOT_TARGET.equals(col.featureType)) {
//                    for (AnnotationFS aFS : annotationsForColumn) {
//                        FeatureStructure[] links = getFeature(aFS, col.uimaFeature,
//                                FeatureStructure[].class);
//                        if (links != null && links.length > 0) {
//                        }
//                    }
//                }
                
                
                if (!PLACEHOLDER.equals(col.featureType)) {
                    aUnit.getDocument().activateColumn(col);
                }
                
                // COMPATIBILITY NOTE:
                // WebAnnoTsv3Writer obtains the type of a relation target column not from the
                // type system definition but rather by looking at target used by the first 
                // actual annotation.
                if (RELATION.equals(col.layerType) && RELATION_REF.equals(col.featureType)) {
                    AnnotationFS annotation = annotationsForColumn.get(0);
                    FeatureStructure target = FSUtil.getFeature(annotation, FEAT_REL_SOURCE,
                            FeatureStructure.class);
                    
                    if (target == null) {
                        throw new IllegalStateException(
                                "Relation does not have its source feature (" + FEAT_REL_SOURCE
                                        + ") set: " + annotation);
                    }
                    
                    if (col.uimaType.getName().equals(Dependency.class.getName())) {
                        // COMPATIBILITY NOTE:
                        // WebAnnoTsv3Writer hard-changes the target type for DKPro Core
                        // Dependency annotations from Token to POS - the reason is not really
                        // clear. Probably because the Dependency relations in the WebAnno UI
                        // attach to POS (Token's are not visible as annotations in the UI).
                        col.setTargetTypeHint(aUnit.getDocument().getJCas().getTypeSystem()
                                .getType(POS.class.getName()));
                    }
                    else {
                        TsvSchema schema = aUnit.getDocument().getSchema();
                        col.setTargetTypeHint(schema.getEffectiveType(target));
                    }
                }
            }
        }
    }
 
Example #28
Source File: WebannoTsv2Reader.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * 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
                relationAnno.getCAS().removeFsFromIndexes(relationAnno);
                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.getCAS().addFsToIndexes(relationAnno);

                relationAnno.setFeatureValue(dependentFeature, dependentAnno);
                relationAnno.setFeatureValue(governorFeature, governorAnno);
                
                relationAnno.getCAS().addFsToIndexes(relationAnno);
                i++;
            }

        }
    }
}
 
Example #29
Source File: WebannoTsv1Reader.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * add dependency parsing to CAS
 */
private void createDependency(JCas aJCas, Map<Integer, String> tokens,
        Map<Integer, String> dependencyFunction, Map<Integer, Integer> dependencyDependent,
        Map<String, Token> tokensStored)
{
    for (int i = 1; i <= tokens.size(); i++) {
        if (dependencyFunction.get(i) != null) {
            Dependency outDependency = new Dependency(aJCas);
            outDependency.setDependencyType(dependencyFunction.get(i));

            // if span A has (start,end)= (20, 26) and B has (start,end)= (30, 36)
            // arc drawn from A to B, dependency will have (start, end) = (20, 36)
            // arc drawn from B to A, still dependency will have (start, end) = (20, 36)
            int begin = 0, end = 0;
            // if not ROOT
            if (dependencyDependent.get(i) != 0) {
                begin = tokensStored.get("t_" + i).getBegin() > tokensStored.get(
                        "t_" + dependencyDependent.get(i)).getBegin() ? tokensStored.get(
                        "t_" + dependencyDependent.get(i)).getBegin() : tokensStored.get(
                        "t_" + i).getBegin();
                end = tokensStored.get("t_" + i).getEnd() < tokensStored.get(
                        "t_" + dependencyDependent.get(i)).getEnd() ? tokensStored.get(
                        "t_" + dependencyDependent.get(i)).getEnd() : tokensStored
                        .get("t_" + i).getEnd();
            }
            else {
                begin = tokensStored.get("t_" + i).getBegin();
                end = tokensStored.get("t_" + i).getEnd();
            }

            outDependency.setBegin(begin);
            outDependency.setEnd(end);
            outDependency.setDependent(tokensStored.get("t_" + i));
            if (dependencyDependent.get(i) == 0) {
                outDependency.setGovernor(tokensStored.get("t_" + i));
            }
            else {
                outDependency.setGovernor(tokensStored.get("t_" + dependencyDependent.get(i)));
            }
            outDependency.addToIndexes();
        }
    }
}
 
Example #30
Source File: MtasUimaParserTest.java    From inception with Apache License 2.0 4 votes vote down vote up
@Test
public void testDependencyRelation() throws Exception
{
    // Set up document with a dummy dependency relation
    jcas.setDocumentText("a b");
    Token t1 = new Token(jcas, 0, 1);
    t1.addToIndexes();
    
    POS p1 = new POS(jcas, t1.getBegin(), t1.getEnd());
    p1.setPosValue("A");
    t1.setPos(p1);
    p1.addToIndexes();

    Token t2 = new Token(jcas, 2, 3);
    t2.addToIndexes();

    POS p2 = new POS(jcas, t2.getBegin(), t2.getEnd());
    p2.setPosValue("B");
    t2.setPos(p2);
    p2.addToIndexes();
    
    Dependency d1 = new Dependency(jcas, t2.getBegin(), t2.getEnd());
    d1.setDependent(t2);
    d1.setGovernor(t1);
    d1.addToIndexes();
    
    // Set up annotation schema with POS and Dependency
    AnnotationLayer tokenLayer = new AnnotationLayer(Token.class.getName(), "Token",
            SPAN_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    tokenLayer.setId(1l);
    AnnotationFeature tokenLayerPos = new AnnotationFeature(1l, tokenLayer, "pos",
            POS.class.getName());
    
    AnnotationLayer posLayer = new AnnotationLayer(POS.class.getName(), "POS",
            SPAN_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    posLayer.setId(2l);
    AnnotationFeature posLayerValue = new AnnotationFeature(1l, posLayer, "PosValue",
            CAS.TYPE_NAME_STRING);
    
    AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(),
            "Dependency", RELATION_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    depLayer.setId(3l);
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenLayerPos);
    AnnotationFeature dependencyLayerGovernor = new AnnotationFeature(2l, depLayer,
            "Governor", Token.class.getName());
    AnnotationFeature dependencyLayerDependent = new AnnotationFeature(3l, depLayer,
            "Dependent", Token.class.getName());
        
    when(annotationSchemaService.listAnnotationLayer(any(Project.class)))
            .thenReturn(asList(tokenLayer, posLayer, depLayer));

    when(annotationSchemaService.getAdapter(posLayer)).thenReturn(new SpanAdapter(
        layerSupportRegistry, featureSupportRegistry, null, posLayer, 
        () -> asList(posLayerValue), null));

    when(annotationSchemaService.getAdapter(depLayer))
            .thenReturn(new RelationAdapter(
                layerSupportRegistry, featureSupportRegistry, null, depLayer,
                FEAT_REL_TARGET, FEAT_REL_SOURCE,
                () -> asList(dependencyLayerGovernor, dependencyLayerDependent),
                emptyList()));

    MtasUimaParser sut = new MtasUimaParser(
            asList(tokenLayerPos, posLayerValue, dependencyLayerGovernor,
                    dependencyLayerDependent),
            annotationSchemaService, featureIndexingSupportRegistry);
    MtasTokenCollection tc = sut.createTokenCollection(jcas.getCas());
    
    MtasUtils.print(tc);
    
    List<MtasToken> tokens = new ArrayList<>();
    tc.iterator().forEachRemaining(tokens::add);

    assertThat(tokens)
        .filteredOn(t -> t.getPrefix().startsWith("Dependency"))
        .extracting(t -> t.getPrefix() + "=" + t.getPostfix())
        .containsExactly(
                "Dependency=b", 
                "Dependency-source=a", 
                "Dependency-source.PosValue=A",
                "Dependency-target=b", 
                "Dependency-target.PosValue=B");
}