Search in sources :

Example 11 with Dependency

use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.

the class TcfReader method convertDependencies.

private void convertDependencies(JCas aJCas, TextCorpus aCorpusData, Map<String, Token> aTokens) {
    if (aCorpusData.getDependencyParsingLayer() == null) {
        // No layer to read from.
        return;
    }
    for (int i = 0; i < aCorpusData.getDependencyParsingLayer().size(); i++) {
        DependencyParse dependencyParse = aCorpusData.getDependencyParsingLayer().getParse(i);
        for (eu.clarin.weblicht.wlfxb.tc.api.Dependency dependency : dependencyParse.getDependencies()) {
            eu.clarin.weblicht.wlfxb.tc.api.Token[] governorTokens = aCorpusData.getDependencyParsingLayer().getGovernorTokens(dependency);
            eu.clarin.weblicht.wlfxb.tc.api.Token[] dependentTokens = aCorpusData.getDependencyParsingLayer().getDependentTokens(dependency);
            POS dependentPos = aTokens.get(dependentTokens[0].getID()).getPos();
            // as a default POS --
            if (dependentPos == null) {
                getUimaContext().getLogger().log(Level.INFO, "There is no pos for this token, added is -- as a pos");
                dependentPos = new POS(aJCas);
                dependentPos.setBegin(aTokens.get(dependentTokens[0].getID()).getBegin());
                dependentPos.setEnd(aTokens.get(dependentTokens[0].getID()).getEnd());
                dependentPos.setPosValue("--");
                dependentPos.addToIndexes();
                aTokens.get(dependentTokens[0].getID()).setPos(dependentPos);
            }
            if (governorTokens != null) {
                POS governerPos = aTokens.get(governorTokens[0].getID()).getPos();
                if (governerPos == null) {
                    if (dependency.getFunction().equals("ROOT")) {
                    // do nothing
                    } else {
                        getUimaContext().getLogger().log(Level.INFO, "There is no pos for this token, added is -- as a pos");
                        governerPos = new POS(aJCas);
                        governerPos.setBegin(aTokens.get(governorTokens[0].getID()).getBegin());
                        governerPos.setEnd(aTokens.get(governorTokens[0].getID()).getEnd());
                        governerPos.setPosValue("--");
                        governerPos.addToIndexes();
                        aTokens.get(governorTokens[0].getID()).setPos(governerPos);
                    }
                }
            } else {
                governorTokens = dependentTokens;
            }
            Dependency outDependency = new Dependency(aJCas);
            outDependency.setDependencyType(dependency.getFunction());
            outDependency.setGovernor(aTokens.get(governorTokens[0].getID()));
            outDependency.setDependent(dependency.getFunction().equals("ROOT") ? aTokens.get(governorTokens[0].getID()) : aTokens.get(dependentTokens[0].getID()));
            outDependency.setBegin(outDependency.getDependent().getBegin());
            outDependency.setEnd(outDependency.getDependent().getEnd());
            outDependency.addToIndexes();
        }
    }
}
Also used : DependencyParse(eu.clarin.weblicht.wlfxb.tc.api.DependencyParse) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)

Example 12 with Dependency

use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.

the class WebAnnoTsv3WriterTestBase method testDependencyWithValues.

@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));
}
Also used : POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Test(org.junit.Test)

Example 13 with Dependency

use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.

the class Tsv3XSerializerTest method testRelation.

@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());
}
Also used : LayerType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.LayerType) Type(org.apache.uima.cas.Type) FeatureType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.FeatureType) TsvColumn(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn) ArrayList(java.util.ArrayList) TsvDocument(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvDocument) JCas(org.apache.uima.jcas.JCas) TsvSchema(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvSchema) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Test(org.junit.Test)

Example 14 with Dependency

use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.

the class RemoveDanglingRelationsRepairTest method test.

@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);
}
Also used : LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) ArrayList(java.util.ArrayList) CasDoctor(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Test(org.junit.Test)

Aggregations

Dependency (de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency)14 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)10 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)6 ArrayList (java.util.ArrayList)6 JCas (org.apache.uima.jcas.JCas)5 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)4 Test (org.junit.Test)3 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)2 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)2 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)2 TsvColumn (de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn)2 MorphologicalFeatures (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.morph.MorphologicalFeatures)2 SemArg (de.tudarmstadt.ukp.dkpro.core.api.semantics.type.SemArg)2 SemArgLink (de.tudarmstadt.ukp.dkpro.core.api.semantics.type.SemArgLink)2 SemPred (de.tudarmstadt.ukp.dkpro.core.api.semantics.type.SemPred)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 AnalysisEngineProcessException (org.apache.uima.analysis_engine.AnalysisEngineProcessException)2