use of de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator in project webanno by webanno.
the class AnnotationDetailEditorPanel method populateTagsBasedOnRules.
/**
* Adds and sorts tags based on Constraints rules
*/
private void populateTagsBasedOnRules(JCas aJCas, FeatureState aModel) {
LOG.trace("populateTagsBasedOnRules(feature: " + aModel.feature.getUiName() + ")");
AnnotatorState state = getModelObject();
// Add values from rules
String restrictionFeaturePath;
switch(aModel.feature.getLinkMode()) {
case WITH_ROLE:
restrictionFeaturePath = aModel.feature.getName() + "." + aModel.feature.getLinkTypeRoleFeatureName();
break;
case NONE:
restrictionFeaturePath = aModel.feature.getName();
break;
default:
throw new IllegalArgumentException("Unsupported link mode [" + aModel.feature.getLinkMode() + "] on feature [" + aModel.feature.getName() + "]");
}
aModel.indicator.reset();
// Fetch possible values from the constraint rules
List<PossibleValue> possibleValues;
try {
FeatureStructure featureStructure = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
Evaluator evaluator = new ValuesGenerator();
// Only show indicator if this feature can be affected by Constraint rules!
aModel.indicator.setAffected(evaluator.isThisAffectedByConstraintRules(featureStructure, restrictionFeaturePath, state.getConstraints()));
possibleValues = evaluator.generatePossibleValues(featureStructure, restrictionFeaturePath, state.getConstraints());
LOG.debug("Possible values for [" + featureStructure.getType().getName() + "] [" + restrictionFeaturePath + "]: " + possibleValues);
} catch (Exception e) {
error("Unable to evaluate constraints: " + ExceptionUtils.getRootCauseMessage(e));
LOG.error("Unable to evaluate constraints: " + e.getMessage(), e);
possibleValues = new ArrayList<>();
}
// Fetch actual tagset
List<Tag> valuesFromTagset = annotationService.listTags(aModel.feature.getTagset());
// First add tags which are suggested by rules and exist in tagset
List<Tag> tagset = compareSortAndAdd(possibleValues, valuesFromTagset, aModel.indicator);
// Then add the remaining tags
for (Tag remainingTag : valuesFromTagset) {
if (!tagset.contains(remainingTag)) {
tagset.add(remainingTag);
}
}
// Record the possible values and the (re-ordered) tagset in the feature state
aModel.possibleValues = possibleValues;
aModel.tagset = tagset;
}
use of de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator 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.evaluator.ValuesGenerator 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.evaluator.ValuesGenerator 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.evaluator.ValuesGenerator 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