use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.
the class ConstraintsGeneratorTest method testSimplePath.
@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);
}
use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.
the class DependencyLayerInitializer method configure.
@Override
public void configure(Project aProject) throws IOException {
TagSet depTagSet = JsonImportUtil.importTagSetFromJson(aProject, new ClassPathResource("/tagsets/mul-dep-ud.json").getInputStream(), annotationSchemaService);
// Dependency Layer
AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency", RELATION_TYPE, aProject, true);
AnnotationLayer tokenLayer = annotationSchemaService.getLayer(Token.class.getName(), aProject);
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));
}
use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.
the class LegacyProjectInitializer method createDepLayer.
private void createDepLayer(Project aProject, TagSet aTagset) throws IOException {
// Dependency Layer
AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency", RELATION_TYPE, aProject, true);
AnnotationLayer tokenLayer = annotationSchemaService.getLayer(Token.class.getName(), aProject);
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));
}
use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.
the class ConllUReader method makeDependency.
private Dependency makeDependency(JCas aJCas, int govId, int depId, String label, String flavor, Int2ObjectMap<Token> tokens, String[] word) {
Dependency rel = new Dependency(aJCas);
if (govId == 0) {
rel.setGovernor(tokens.get(depId));
rel.setDependent(tokens.get(depId));
} else {
rel.setGovernor(tokens.get(govId));
rel.setDependent(tokens.get(depId));
}
rel.setDependencyType(label);
// This is set via FSUtil because we still use the DKPro Core 1.7.0 JCas classes
FSUtil.setFeature(rel, "flavor", flavor);
rel.setBegin(rel.getDependent().getBegin());
rel.setEnd(rel.getDependent().getEnd());
rel.addToIndexes();
return rel;
}
use of de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency in project webanno by webanno.
the class ConllUWriter method convert.
private void convert(JCas aJCas, PrintWriter aOut) {
Map<SurfaceForm, Collection<Token>> surfaceIdx = indexCovered(aJCas, SurfaceForm.class, Token.class);
Int2ObjectMap<SurfaceForm> surfaceBeginIdx = new Int2ObjectOpenHashMap<>();
for (SurfaceForm sf : select(aJCas, SurfaceForm.class)) {
surfaceBeginIdx.put(sf.getBegin(), sf);
}
for (Sentence sentence : select(aJCas, Sentence.class)) {
HashMap<Token, Row> ctokens = new LinkedHashMap<>();
// Tokens
List<Token> tokens = selectCovered(Token.class, sentence);
for (int i = 0; i < tokens.size(); i++) {
Row row = new Row();
row.id = i + 1;
row.token = tokens.get(i);
row.noSpaceAfter = (i + 1 < tokens.size()) && row.token.getEnd() == tokens.get(i + 1).getBegin();
ctokens.put(row.token, row);
}
// Dependencies
for (Dependency rel : selectCovered(Dependency.class, sentence)) {
String flavor = FSUtil.getFeature(rel, "flavor", String.class);
if (StringUtils.isBlank(flavor) || DependencyFlavor.BASIC.equals(flavor)) {
ctokens.get(rel.getDependent()).deprel = rel;
} else {
ctokens.get(rel.getDependent()).deps.add(rel);
}
}
// Write sentence in CONLL-U format
for (Row row : ctokens.values()) {
String lemma = UNUSED;
if (writeLemma && (row.token.getLemma() != null)) {
lemma = row.token.getLemma().getValue();
}
String pos = UNUSED;
String cpos = UNUSED;
if (writePos && (row.token.getPos() != null)) {
POS posAnno = row.token.getPos();
pos = posAnno.getPosValue();
cpos = dkpro2ud.get(posAnno.getClass());
if (StringUtils.isBlank(cpos)) {
cpos = pos;
}
}
int headId = UNUSED_INT;
String deprel = UNUSED;
String deps = UNUSED;
if (writeDependency) {
if ((row.deprel != null)) {
deprel = row.deprel.getDependencyType();
headId = ctokens.get(row.deprel.getGovernor()).id;
if (headId == row.id) {
// ROOT dependencies may be modeled as a loop, ignore these.
headId = 0;
}
}
StringBuilder depsBuf = new StringBuilder();
for (Dependency d : row.deps) {
if (depsBuf.length() > 0) {
depsBuf.append('|');
}
// Resolve self-looping root to 0-indexed root
int govId = ctokens.get(d.getGovernor()).id;
if (govId == row.id) {
govId = 0;
}
depsBuf.append(govId);
depsBuf.append(':');
depsBuf.append(d.getDependencyType());
}
if (depsBuf.length() > 0) {
deps = depsBuf.toString();
}
}
String head = UNUSED;
if (headId != UNUSED_INT) {
head = Integer.toString(headId);
}
String feats = UNUSED;
if (writeMorph && (row.token.getMorph() != null)) {
feats = row.token.getMorph().getValue();
}
String misc = UNUSED;
if (row.noSpaceAfter) {
misc = "SpaceAfter=No";
}
SurfaceForm sf = surfaceBeginIdx.get(row.token.getBegin());
if (sf != null) {
@SuppressWarnings({ "unchecked", "rawtypes" }) List<Token> covered = (List) surfaceIdx.get(sf);
int id1 = ctokens.get(covered.get(0)).id;
int id2 = ctokens.get(covered.get(covered.size() - 1)).id;
aOut.printf("%d-%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", id1, id2, sf.getValue(), UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED);
}
aOut.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", row.id, row.token.getCoveredText(), lemma, cpos, pos, feats, head, deprel, deps, misc);
}
aOut.println();
}
}
Aggregations