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();
}
}
}
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));
}
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());
}
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);
}
Aggregations