Java Code Examples for de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS#addToIndexes()

The following examples show how to use de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS#addToIndexes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tcf2DKPro.java    From inception with Apache License 2.0 6 votes vote down vote up
public void convertPos(JCas aJCas, TextCorpus aCorpusData, Map<String, Token> aTokens)
{
    if (aCorpusData.getPosTagsLayer() == null) {
        return;
    }
    for (int i = 0; i < aCorpusData.getPosTagsLayer().size(); i++) {
        eu.clarin.weblicht.wlfxb.tc.api.Token[] posTokens = aCorpusData.getPosTagsLayer()
                .getTokens(aCorpusData.getPosTagsLayer().getTag(i));
        String value = aCorpusData.getPosTagsLayer().getTag(i).getString();

        POS outPos = new POS(aJCas);

        outPos.setBegin(aTokens.get(posTokens[0].getID()).getBegin());
        outPos.setEnd(aTokens.get(posTokens[0].getID()).getEnd());
        outPos.setPosValue(value);
        POSUtils.assignCoarseValue(outPos);
        outPos.addToIndexes();

        // Set the POS to the token
        aTokens.get(posTokens[0].getID()).setPos(outPos);
    }
}
 
Example 2
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void thatRelationCrossSentenceBehaviorOnValidateGeneratesErrors() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(posAnnotations.size() - 1);

    depLayer.setCrossSentence(true);
    sut.add(document, username, source, target, jcas.getCas());
    
    depLayer.setCrossSentence(false);
    assertThat(sut.validate(jcas.getCas()))
            .extracting(Pair::getLeft)
            .usingElementComparatorIgnoringFields("source", "message")
            .containsExactly(LogMessage.error(null, ""));
}
 
Example 3
Source File: AgreementMeasureTestSuite_ImplBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
public <R extends Serializable, T extends DefaultAgreementTraits> R twoWithoutLabelTest(
        AggreementMeasureSupport<T, R, ICodingAnnotationStudy> aSupport, T aTraits)
    throws Exception
{
    AnnotationLayer layer = new AnnotationLayer(POS.class.getName(),
            POS.class.getSimpleName(), SPAN_TYPE, project, false, SINGLE_TOKEN, NO_OVERLAP);
    layer.setId(1l);
    layers.add(layer);

    AnnotationFeature feature = new AnnotationFeature(project, layer, "PosValue", "PosValue",
            CAS.TYPE_NAME_STRING);
    feature.setId(1l);
    features.add(feature);
    
    JCas user1 = JCasFactory.createJCas();
    user1.setDocumentText("test");
    new POS(user1, 0, 1).addToIndexes();
    new POS(user1, 1, 2).addToIndexes();
    POS p1 = new POS(user1, 3, 4);
    p1.setPosValue("A");
    p1.addToIndexes();

    JCas user2 = JCasFactory.createJCas();
    user2.setDocumentText("test");
    new POS(user2, 0, 1).addToIndexes();
    new POS(user2, 2, 3).addToIndexes();
    POS p2 = new POS(user2, 3, 4);
    p2.setPosValue("B");
    p2.addToIndexes();
    
    Map<String, List<CAS>> casByUser = new LinkedHashMap<>();
    casByUser.put("user1", asList(user1.getCas()));
    casByUser.put("user2", asList(user2.getCas()));
    
    AggreementMeasure<R> measure = aSupport.createMeasure(feature, aTraits);
    
    return measure.getAgreement(casByUser);
}
 
Example 4
Source File: AgreementMeasureTestSuite_ImplBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
public <R extends Serializable, T extends DefaultAgreementTraits> R 
        singleNoDifferencesWithAdditionalCasTest(
                AggreementMeasureSupport<T, R, ICodingAnnotationStudy> aSupport)
    throws Exception
{
    AnnotationLayer layer = new AnnotationLayer(POS.class.getName(),
            POS.class.getSimpleName(), SPAN_TYPE, project, false, SINGLE_TOKEN, NO_OVERLAP);
    layer.setId(1l);
    layers.add(layer);

    AnnotationFeature feature = new AnnotationFeature(project, layer, "PosValue", "PosValue",
            CAS.TYPE_NAME_STRING);
    feature.setId(1l);
    features.add(feature);
    
    T traits = aSupport.createTraits();
    
    JCas user1 = JCasFactory.createJCas();
    user1.setDocumentText("test");

    JCas user2 = JCasFactory.createJCas();
    user2.setDocumentText("test");
    
    JCas user3 = JCasFactory.createJCas();
    user3.setDocumentText("test");
    POS pos3 = new POS(user3, 0, 4);
    pos3.setPosValue("test");
    pos3.addToIndexes();
    
    Map<String, List<CAS>> casByUser = new LinkedHashMap<>();
    casByUser.put("user1", asList(user1.getCas()));
    casByUser.put("user2", asList(user2.getCas()));
    casByUser.put("user3", asList(user3.getCas()));
    
    AggreementMeasure<R> measure = aSupport.createMeasure(feature, traits);
    
    return measure.getAgreement(casByUser);
}
 
Example 5
Source File: SymbolicRulesTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: SymbolicRulesTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void thatCreatingRelationWorks() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(1);

    AnnotationFS dep1 = sut.add(document, username, source, target, jcas.getCas());
    
    assertThat(FSUtil.getFeature(dep1, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0));
    assertThat(FSUtil.getFeature(dep1, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1));
}
 
Example 8
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void thatRelationCrossSentenceBehaviorOnCreateThrowsException() throws Exception
{
    depLayer.setCrossSentence(false);
    
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(posAnnotations.size() - 1);

    assertThatExceptionOfType(MultipleSentenceCoveredException.class)
            .isThrownBy(() -> sut.add(document, username, source, target, jcas.getCas()))
            .withMessageContaining("multiple sentences");
}
 
Example 9
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void thatRelationAttachmentBehaviorOnCreateWorks() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(1);

    AnnotationFS dep = sut.add(document, username, source, target, jcas.getCas());

    assertThat(FSUtil.getFeature(dep, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0));
    assertThat(FSUtil.getFeature(dep, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1));
}
 
Example 10
Source File: RelationRendererTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void thatRelationCrossSentenceBehaviorOnRenderGeneratesErrors() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter adapter = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(posAnnotations.size() - 1);

    depLayer.setCrossSentence(true);
    AnnotationFS dep = adapter.add(document, username, source, target, jcas.getCas());
    
    depLayer.setCrossSentence(false);
    RelationRenderer sut = new RelationRenderer(adapter, layerSupportRegistry,
            featureSupportRegistry, asList(new RelationCrossSentenceBehavior()));
    
    VDocument vdoc = new VDocument();
    sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
    
    assertThat(vdoc.comments())
            .usingFieldByFieldElementComparator()
            .contains(new VComment(dep, ERROR, 
                    "Crossing sentence boundaries is not permitted."));
}
 
Example 11
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 12
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testTokenAttachedAnnotationsWithValues() throws Exception
{
    JCas jcas = makeJCasOneSentence();
    
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
    
    Token t1 = tokens.get(0);
    
    Lemma l1 = new Lemma(jcas, t1.getBegin(), t1.getEnd());
    l1.setValue("lemma1");
    l1.addToIndexes();
    t1.setLemma(l1);
    
    MorphologicalFeatures m1 = new MorphologicalFeatures(jcas, t1.getBegin(), t1.getEnd());
    m1.setValue("morph");
    m1.setTense("tense1");
    m1.addToIndexes();
    t1.setMorph(m1);
    
    POS p1 = new POS(jcas, t1.getBegin(), t1.getEnd());
    p1.setPosValue("pos1");
    p1.addToIndexes();
    t1.setPos(p1);
    
    Stem s1 = new Stem(jcas, t1.getBegin(), t1.getEnd());
    s1.setValue("stem1");
    s1.addToIndexes();
    t1.setStem(s1);
    
    writeAndAssertEquals(jcas, WebannoTsv3Writer.PARAM_SPAN_LAYERS,
            asList(MorphologicalFeatures.class, POS.class, Lemma.class, Stem.class));
}
 
Example 13
Source File: WebannoTsv1Reader.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * Create {@link Token} in the {@link CAS}. If the lemma and pos columns are not empty it will
 * create {@link Lemma} and {@link POS} annotations
 */
private void createToken(JCas aJCas, StringBuilder text, Map<Integer, String> tokens,
        Map<Integer, String> pos, Map<Integer, String> lemma, Map<String, Token> tokensStored)
{
    int tokenBeginPosition = 0;
    int tokenEndPosition = 0;

    for (int i = 1; i <= tokens.size(); i++) {
        tokenBeginPosition = text.indexOf(tokens.get(i), tokenBeginPosition);
        Token outToken = new Token(aJCas, tokenBeginPosition, text.indexOf(tokens.get(i),
                tokenBeginPosition) + tokens.get(i).length());
        tokenEndPosition = text.indexOf(tokens.get(i), tokenBeginPosition)
                + tokens.get(i).length();
        tokenBeginPosition = tokenEndPosition;
        outToken.addToIndexes();

        // Add pos to CAS if exist
        if (!pos.get(i).equals("_")) {
            POS outPos = new POS(aJCas, outToken.getBegin(), outToken.getEnd());
            outPos.setPosValue(pos.get(i));
            outPos.addToIndexes();
            outToken.setPos(outPos);
        }

        // Add lemma if exist
        if (!lemma.get(i).equals("_")) {
            Lemma outLemma = new Lemma(aJCas, outToken.getBegin(), outToken.getEnd());
            outLemma.setValue(lemma.get(i));
            outLemma.addToIndexes();
            outToken.setLemma(outLemma);
        }
        tokensStored.put("t_" + i, outToken);
    }
}
 
Example 14
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void thatRelationOverlapBehaviorOnCreateWorks() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(1);

    // First annotation should work
    depLayer.setOverlapMode(ANY_OVERLAP);
    sut.add(document, username, source, target, jcas.getCas());
    
    // Adding another annotation at the same place DOES NOT work
    depLayer.setOverlapMode(NO_OVERLAP);
    assertThatExceptionOfType(AnnotationException.class)
            .isThrownBy(() -> sut.add(document, username, source, target, jcas.getCas()))
            .withMessageContaining("no overlap or stacking");

    depLayer.setOverlapMode(OverlapMode.OVERLAP_ONLY);
    assertThatExceptionOfType(AnnotationException.class)
            .isThrownBy(() -> sut.add(document, username, source, target, jcas.getCas()))
            .withMessageContaining("stacking is not allowed");
    
    // Adding another annotation at the same place DOES work
    depLayer.setOverlapMode(OverlapMode.STACKING_ONLY);
    assertThatCode(() -> sut.add(document, username, source, target, jcas.getCas()))
            .doesNotThrowAnyException();

    depLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);
    assertThatCode(() -> sut.add(document, username, source, target, jcas.getCas()))
            .doesNotThrowAnyException();
}
 
Example 15
Source File: RelationAdapterTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void thatRelationOverlapBehaviorOnValidateGeneratesErrors() throws Exception
{
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "This is a test .\nThis is sentence two .");

    for (Token t : select(jcas, Token.class)) {
        POS pos = new POS(jcas, t.getBegin(), t.getEnd());
        t.setPos(pos);
        pos.addToIndexes();
    }

    RelationAdapter sut = new RelationAdapter(layerSupportRegistry, featureSupportRegistry,
        null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE,
        () -> asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors);

    List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class));

    POS source = posAnnotations.get(0);
    POS target = posAnnotations.get(1);

    // Create two annotations stacked annotations
    depLayer.setOverlapMode(ANY_OVERLAP);
    sut.add(document, username, source, target, jcas.getCas());
    AnnotationFS rel2 = sut.add(document, username, source, target, jcas.getCas());
    
    depLayer.setOverlapMode(ANY_OVERLAP);
    assertThat(sut.validate(jcas.getCas()))
            .isEmpty();

    depLayer.setOverlapMode(STACKING_ONLY);
    assertThat(sut.validate(jcas.getCas()))
            .isEmpty();

    depLayer.setOverlapMode(OVERLAP_ONLY);
    assertThat(sut.validate(jcas.getCas()))
            .extracting(Pair::getLeft)
            .usingElementComparatorIgnoringFields("source")
            .containsExactly(
                    LogMessage.error(null, "Stacked relation at [5-7]"), 
                    LogMessage.error(null, "Stacked relation at [5-7]"));

    depLayer.setOverlapMode(NO_OVERLAP);
    assertThat(sut.validate(jcas.getCas()))
            .extracting(Pair::getLeft)
            .usingElementComparatorIgnoringFields("source")
            .containsExactly(
                    LogMessage.error(null, "Stacked relation at [5-7]"), 
                    LogMessage.error(null, "Stacked relation at [5-7]"));

    // Remove the stacked annotation and introduce one that is purely overlapping
    sut.delete(document, username, jcas.getCas(), new VID(rel2));
    depLayer.setOverlapMode(ANY_OVERLAP);
    sut.add(document, username, source, posAnnotations.get(2), jcas.getCas());
    
    depLayer.setOverlapMode(NO_OVERLAP);
    assertThat(sut.validate(jcas.getCas()))
            .extracting(Pair::getLeft)
            .usingElementComparatorIgnoringFields("source")
            .containsExactly(
                    LogMessage.error(null, "Overlapping relation at [5-7]"), 
                    LogMessage.error(null, "Overlapping relation at [8-9]"));
}
 
Example 16
Source File: ConstraintsGeneratorTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: MtasUimaParserTest.java    From inception with Apache License 2.0 4 votes vote down vote up
@Test
public void testDependencyRelation() throws Exception
{
    // Set up document with a dummy dependency relation
    jcas.setDocumentText("a b");
    Token t1 = new Token(jcas, 0, 1);
    t1.addToIndexes();
    
    POS p1 = new POS(jcas, t1.getBegin(), t1.getEnd());
    p1.setPosValue("A");
    t1.setPos(p1);
    p1.addToIndexes();

    Token t2 = new Token(jcas, 2, 3);
    t2.addToIndexes();

    POS p2 = new POS(jcas, t2.getBegin(), t2.getEnd());
    p2.setPosValue("B");
    t2.setPos(p2);
    p2.addToIndexes();
    
    Dependency d1 = new Dependency(jcas, t2.getBegin(), t2.getEnd());
    d1.setDependent(t2);
    d1.setGovernor(t1);
    d1.addToIndexes();
    
    // Set up annotation schema with POS and Dependency
    AnnotationLayer tokenLayer = new AnnotationLayer(Token.class.getName(), "Token",
            SPAN_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    tokenLayer.setId(1l);
    AnnotationFeature tokenLayerPos = new AnnotationFeature(1l, tokenLayer, "pos",
            POS.class.getName());
    
    AnnotationLayer posLayer = new AnnotationLayer(POS.class.getName(), "POS",
            SPAN_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    posLayer.setId(2l);
    AnnotationFeature posLayerValue = new AnnotationFeature(1l, posLayer, "PosValue",
            CAS.TYPE_NAME_STRING);
    
    AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(),
            "Dependency", RELATION_TYPE, project, true, SINGLE_TOKEN, NO_OVERLAP);
    depLayer.setId(3l);
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenLayerPos);
    AnnotationFeature dependencyLayerGovernor = new AnnotationFeature(2l, depLayer,
            "Governor", Token.class.getName());
    AnnotationFeature dependencyLayerDependent = new AnnotationFeature(3l, depLayer,
            "Dependent", Token.class.getName());
        
    when(annotationSchemaService.listAnnotationLayer(any(Project.class)))
            .thenReturn(asList(tokenLayer, posLayer, depLayer));

    when(annotationSchemaService.getAdapter(posLayer)).thenReturn(new SpanAdapter(
        layerSupportRegistry, featureSupportRegistry, null, posLayer, 
        () -> asList(posLayerValue), null));

    when(annotationSchemaService.getAdapter(depLayer))
            .thenReturn(new RelationAdapter(
                layerSupportRegistry, featureSupportRegistry, null, depLayer,
                FEAT_REL_TARGET, FEAT_REL_SOURCE,
                () -> asList(dependencyLayerGovernor, dependencyLayerDependent),
                emptyList()));

    MtasUimaParser sut = new MtasUimaParser(
            asList(tokenLayerPos, posLayerValue, dependencyLayerGovernor,
                    dependencyLayerDependent),
            annotationSchemaService, featureIndexingSupportRegistry);
    MtasTokenCollection tc = sut.createTokenCollection(jcas.getCas());
    
    MtasUtils.print(tc);
    
    List<MtasToken> tokens = new ArrayList<>();
    tc.iterator().forEachRemaining(tokens::add);

    assertThat(tokens)
        .filteredOn(t -> t.getPrefix().startsWith("Dependency"))
        .extracting(t -> t.getPrefix() + "=" + t.getPostfix())
        .containsExactly(
                "Dependency=b", 
                "Dependency-source=a", 
                "Dependency-source.PosValue=A",
                "Dependency-target=b", 
                "Dependency-target.PosValue=B");
}