Search in sources :

Example 1 with ParserVisitor

use of de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor 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;
}
Also used : Scope(de.tudarmstadt.ukp.clarin.webanno.constraints.model.Scope) Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) StringReader(java.io.StringReader) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) ParseException(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ParseException) ConstraintSet(de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar)

Example 2 with ParserVisitor

use of de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor 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);
}
Also used : CollectionReader(org.apache.uima.collection.CollectionReader) Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ArrayList(java.util.ArrayList) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) JCas(org.apache.uima.jcas.JCas) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) FileInputStream(java.io.FileInputStream) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Lemma(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar) Test(org.junit.Test)

Example 3 with ParserVisitor

use of de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor 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);
}
Also used : CollectionReader(org.apache.uima.collection.CollectionReader) Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ArrayList(java.util.ArrayList) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) JCas(org.apache.uima.jcas.JCas) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) FileInputStream(java.io.FileInputStream) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Lemma(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar) Test(org.junit.Test)

Example 4 with ParserVisitor

use of de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor 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());
}
Also used : Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) ArrayList(java.util.ArrayList) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) JCas(org.apache.uima.jcas.JCas) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) FileInputStream(java.io.FileInputStream) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) NamedEntity(de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity) Type(org.apache.uima.cas.Type) CAS(org.apache.uima.cas.CAS) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar) Test(org.junit.Test)

Example 5 with ParserVisitor

use of de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor 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);
}
Also used : Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar) Test(org.junit.Test)

Aggregations

ConstraintsGrammar (de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar)9 Parse (de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse)9 ParsedConstraints (de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints)9 ParserVisitor (de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor)9 FileInputStream (java.io.FileInputStream)8 Test (org.junit.Test)8 Evaluator (de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator)7 PossibleValue (de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue)7 ValuesGenerator (de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator)7 JCas (org.apache.uima.jcas.JCas)6 Lemma (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma)4 LinkedList (java.util.LinkedList)4 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)3 ArrayList (java.util.ArrayList)3 CAS (org.apache.uima.cas.CAS)3 Type (org.apache.uima.cas.Type)3 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)3 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)2 Feature (org.apache.uima.cas.Feature)2 TypeSystem (org.apache.uima.cas.TypeSystem)2