use of de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints in project webanno by webanno.
the class ConstraintsServiceImpl method loadConstraints.
@Override
public ParsedConstraints loadConstraints(Project aProject) throws IOException, ParseException {
ParsedConstraints merged = null;
for (ConstraintSet set : listConstraintSets(aProject)) {
String script = readConstrainSet(set);
ConstraintsGrammar parser = new ConstraintsGrammar(new StringReader(script));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
if (merged == null) {
merged = constraints;
} else {
// Merge imports
for (Entry<String, String> e : constraints.getImports().entrySet()) {
// constraint file(s).
if (merged.getImports().containsKey(e.getKey()) && !e.getValue().equalsIgnoreCase(merged.getImports().get(e.getKey()))) {
// If detected, notify user with proper message and abort merging
String errorMessage = "Conflict detected in imports for key \"" + e.getKey() + "\", conflicting values are \"" + e.getValue() + "\" & \"" + merged.getImports().get(e.getKey()) + "\". Please contact Project Admin for correcting this." + "Constraints feature may not work." + "\nAborting Constraint rules merge!";
throw new ParseException(errorMessage);
}
}
merged.getImports().putAll(constraints.getImports());
// Merge scopes
for (Scope scope : constraints.getScopes()) {
Scope target = merged.getScopeByName(scope.getScopeName());
if (target == null) {
// Scope does not exist yet
merged.getScopes().add(scope);
} else {
// Scope already exists
target.getRules().addAll(scope.getRules());
}
}
}
}
return merged;
}
use of de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints in project webanno by webanno.
the class SymbolicRulesTest method testSimpleSymbolicRules2.
@Test
public void testSimpleSymbolicRules2() throws Exception {
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/symbolic2.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
JCas jcas = JCasFactory.createJCas();
CollectionReader reader = createReader(Conll2006Reader.class, Conll2006Reader.PARAM_SOURCE_LOCATION, "src/test/resources/text/1.conll");
reader.getNext(jcas.getCas());
POS pos = new POS(jcas, 8, 9);
pos.setPosValue("pronoun");
pos.addToIndexes();
Evaluator constraintsEvaluator = new ValuesGenerator();
Lemma lemma = select(jcas, Lemma.class).iterator().next();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(lemma, "value", constraints);
List<PossibleValue> expectedOutput = new ArrayList<>();
expectedOutput.add(new PossibleValue("good", true));
assertEquals(expectedOutput, possibleValues);
}
use of de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints in project webanno by webanno.
the class SymbolicRulesTest method testSimpleSymbolicRules.
@Test
public void testSimpleSymbolicRules() throws Exception {
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/symbolic1.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
JCas jcas = JCasFactory.createJCas();
CollectionReader reader = createReader(Conll2006Reader.class, Conll2006Reader.PARAM_SOURCE_LOCATION, "src/test/resources/text/1.conll");
reader.getNext(jcas.getCas());
POS pos = new POS(jcas, 8, 9);
pos.setPosValue("pronoun");
pos.addToIndexes();
Evaluator constraintsEvaluator = new ValuesGenerator();
Lemma lemma = select(jcas, Lemma.class).iterator().next();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(lemma, "value", constraints);
List<PossibleValue> expectedOutput = new ArrayList<>();
expectedOutput.add(new PossibleValue("good", true));
assertEquals(expectedOutput, possibleValues);
}
use of de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints in project webanno by webanno.
the class ConstraintsGeneratorTest method testTwoConditions.
@Test
public void testTwoConditions() throws Exception {
JCas jcas = makeJCasOneSentence();
CAS cas = jcas.getCas();
List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
Token t1 = tokens.get(0);
Token t2 = tokens.get(tokens.size() - 1);
NamedEntity gov = new NamedEntity(jcas, t1.getBegin(), t1.getEnd());
gov.setValue("Animal");
gov.addToIndexes();
NamedEntity dep = new NamedEntity(jcas, t2.getBegin(), t2.getEnd());
dep.setValue("NotWeight");
dep.addToIndexes();
Type relationType = cas.getTypeSystem().getType("webanno.custom.Relation");
AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
FSUtil.setFeature(fs1, "Governor", gov);
FSUtil.setFeature(fs1, "Dependent", dep);
cas.addFsToIndexes(fs1);
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/twoConditions.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
Evaluator constraintsEvaluator = new ValuesGenerator();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(fs1, "label", constraints);
System.out.println(possibleValues);
// "Weight" != "NotWeight", so the rule should not match
assertEquals(0, possibleValues.size());
}
use of de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints 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);
}
Aggregations