Java Code Examples for org.apache.uima.cas.CAS#createAnnotation()

The following examples show how to use org.apache.uima.cas.CAS#createAnnotation() . 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: ComplexTypeTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void thatIntegerValueInConditionWorks() throws Exception
{
    TypeSystemDescription tsd = TypeSystemDescriptionFactory
            .createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");

    CAS cas = createCas(tsd, null, null);
    cas.setDocumentText("blah");
    
    AnnotationFS continent = cas.createAnnotation(getType(cas, "de.Continent"), 0, 1);
    FSUtil.setFeature(continent, "area", 100);
    cas.addFsToIndexes(continent);

    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream(
            "src/test/resources/rules/region.rules"));
    ParsedConstraints constraints = parser.Parse().accept(new ParserVisitor());

    Evaluator constraintsEvaluator = new ValuesGenerator();
    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(continent,
            "name", constraints);
    
    assertThat(possibleValues)
            .extracting(PossibleValue::getValue)
            .containsExactlyInAnyOrder("Fantasy Island");
}
 
Example 2
Source File: SlotFeatureSupportTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapUnwrap() throws Exception
{
    CAS cas = jcas.getCas();
    
    List<LinkWithRoleModel> links = new ArrayList<>();
    links.add(new LinkWithRoleModel("role", "label", 3));
            
    AnnotationFS targetFS = cas.createAnnotation(targetType, 0, cas.getDocumentText().length());
    
    ArrayFS array = cas.createArrayFS(1);
    FeatureStructure linkFS = cas.createFS(linkType);
    FSUtil.setFeature(linkFS, slotFeature.getLinkTypeRoleFeatureName(), "role");
    FSUtil.setFeature(linkFS, slotFeature.getLinkTypeTargetFeatureName(), targetFS);
    array.set(0, linkFS);
    
    assertThat(sut.wrapFeatureValue(slotFeature, cas, array)).isEqualTo(links);
    assertThat(sut.wrapFeatureValue(slotFeature, cas, null)).isEmpty();
    assertThatThrownBy(() -> sut.wrapFeatureValue(slotFeature, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
    
    assertThat(sut.unwrapFeatureValue(slotFeature, cas, links)).isSameAs(links);
    assertThat(sut.unwrapFeatureValue(slotFeature, cas, null)).isNull();
    assertThatThrownBy(() -> sut.unwrapFeatureValue(slotFeature, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
}
 
Example 3
Source File: DataMajorityNerRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    DataMajorityModel model = aContext.get(KEY_MODEL).orElseThrow(() ->
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));

    // Make the predictions
    Type tokenType = CasUtil.getAnnotationType(aCas, Token.class);
    Collection<AnnotationFS> candidates = CasUtil.select(aCas, tokenType);
    List<Annotation> predictions = predict(candidates, model);

    // Add predictions to the CAS
    Type predictedType = getPredictedType(aCas);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature scoreExplanationFeature = getScoreExplanationFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);

    for (Annotation ann : predictions) {
        AnnotationFS annotation = aCas.createAnnotation(predictedType, ann.begin, ann.end);
        annotation.setStringValue(predictedFeature, ann.label);
        annotation.setDoubleValue(scoreFeature, ann.score);
        annotation.setStringValue(scoreExplanationFeature, ann.explanation);
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}
 
Example 4
Source File: CasMergeTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
private AnnotationFS createPOSAnno(CAS aCas, String aValue, int aBegin, int aEnd)
{
    Type type = aCas.getTypeSystem().getType(POS.class.getTypeName());
    
    AnnotationFS clickedFs = aCas.createAnnotation(type, aBegin, aEnd);
    Feature posValue = type.getFeatureByBaseName("PosValue");
    clickedFs.setStringValue(posValue, aValue);
    aCas.addFsToIndexes(clickedFs);
    return clickedFs;
}
 
Example 5
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleTokenRelationWithMultipleFeatureValues() throws Exception
{
    JCas jcas = makeJCasOneSentence();
    CAS cas = jcas.getCas();
    
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
    
    Token gov = tokens.get(0);
    Token dep = tokens.get(tokens.size() - 1);

    Type relationType = cas.getTypeSystem().getType("webanno.custom.ComplexRelation");
    
    // One at the beginning
    // WebAnno legacy conventions
    // AnnotationFS fs1 = cas.createAnnotation(relationType, 
    //      min(dep.getBegin(), gov.getBegin()),
    //      max(dep.getEnd(), gov.getEnd()));
    // DKPro Core conventions
    AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
    FSUtil.setFeature(fs1, "Governor", gov);
    FSUtil.setFeature(fs1, "Dependent", dep);
    FSUtil.setFeature(fs1, "value", "nsubj");
    FSUtil.setFeature(fs1, "boolValue", true);
    FSUtil.setFeature(fs1, "integerValue", 42);
    cas.addFsToIndexes(fs1);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList("webanno.custom.ComplexRelation"));
}
 
Example 6
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNonTokenRelationWithoutFeature() 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);
    
    Span gov = new Span(jcas, t1.getBegin(), t1.getEnd());
    gov.addToIndexes();
    Span dep =  new Span(jcas, t2.getBegin(), t2.getEnd());
    dep.addToIndexes();

    Type relationType = cas.getTypeSystem().getType("webanno.custom.SimpleRelation");
    
    // One at the beginning
    // WebAnno legacy conventions
    // AnnotationFS fs1 = cas.createAnnotation(relationType, 
    //         min(dep.getBegin(), gov.getBegin()),
    //         max(dep.getEnd(), gov.getEnd()));
    // DKPro Core conventions
    AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
    FSUtil.setFeature(fs1, "Governor", gov);
    FSUtil.setFeature(fs1, "Dependent", dep);
    cas.addFsToIndexes(fs1);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList(Span.class),
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList("webanno.custom.SimpleRelation"));
}
 
Example 7
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiTokenSlotFeature() 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(1);
    Token t3 = tokens.get(2);
    Token t4 = tokens.get(3);
    Token t5 = tokens.get(4);
    
    Type type = cas.getTypeSystem().getType("webanno.custom.SimpleSpan");
    AnnotationFS s2 = cas.createAnnotation(type, t2.getBegin(), t3.getEnd());
    cas.addFsToIndexes(s2);
    AnnotationFS s3 = cas.createAnnotation(type, t4.getBegin(), t5.getEnd());
    cas.addFsToIndexes(s3);

    FeatureStructure link1 = makeLinkFS(jcas, "p1", s2);
    FeatureStructure link2 = makeLinkFS(jcas, "p2", s3);
    
    makeLinkHostFS(jcas, t1.getBegin(), t1.getEnd(), link1, link2);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SLOT_FEATS, asList("webanno.custom.SimpleLinkHost:links"),
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList("webanno.custom.SimpleSpan", 
                    "webanno.custom.SimpleLinkHost"),
            WebannoTsv3Writer.PARAM_LINK_TYPES, asList("webanno.custom.LinkType"),
            WebannoTsv3Writer.PARAM_SLOT_TARGETS, asList("webanno.custom.SimpleSpan"));
}
 
Example 8
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNonMultiTokenRelationWithMultipleFeatureValues() 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(1);
    Token t3 = tokens.get(2);
    Token t4 = tokens.get(3);
    
    Span gov = new Span(jcas, t1.getBegin(), t2.getEnd());
    gov.addToIndexes();
    Span dep =  new Span(jcas, t3.getBegin(), t4.getEnd());
    dep.addToIndexes();

    Type relationType = cas.getTypeSystem().getType("webanno.custom.ComplexRelation");
    
    // One at the beginning
    // WebAnno legacy conventions
    // AnnotationFS fs1 = cas.createAnnotation(relationType, 
    //         min(dep.getBegin(), gov.getBegin()),
    //         max(dep.getEnd(), gov.getEnd()));
    // DKPro Core conventions
    AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
    FSUtil.setFeature(fs1, "Governor", gov);
    FSUtil.setFeature(fs1, "Dependent", dep);
    FSUtil.setFeature(fs1, "value", "nsubj");
    FSUtil.setFeature(fs1, "boolValue", true);
    FSUtil.setFeature(fs1, "integerValue", 42);
    cas.addFsToIndexes(fs1);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList(Span.class),
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList("webanno.custom.ComplexRelation"));
}
 
Example 9
Source File: AllAnnotationsIndexedCheckTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testOK()
    throws Exception
{
    TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory()
            .createTypeSystemDescription();
    
    String refTypeName = "RefType";
    
    TypeDescription refTypeDesc = tsd.addType(refTypeName, null, CAS.TYPE_NAME_ANNOTATION);
    refTypeDesc.addFeature("ref", null, CAS.TYPE_NAME_ANNOTATION);
    
    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    
    Type refType = cas.getTypeSystem().getType(refTypeName);
    
    // A regular index annotation
    AnnotationFS anno1 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
    cas.addFsToIndexes(anno1);

    // An indexed annotation but reachable through an indexe one (below)
    AnnotationFS anno2 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
    cas.addFsToIndexes(anno2);

    // An indexed annotation that references the non-indexed annotation above
    AnnotationFS anno3 = cas.createAnnotation(refType, 0, 1);
    anno3.setFeatureValue(refType.getFeatureByBaseName("ref"), anno2);
    cas.addFsToIndexes(anno3);
    
    List<LogMessage> messages = new ArrayList<>();
    CasDoctor cd = new CasDoctor(AllFeatureStructuresIndexedCheck.class);
    // A project is not required for this check
    boolean result = cd.analyze(null, cas, messages);
    
    messages.forEach(System.out::println);
    
    assertTrue(result);
}
 
Example 10
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleStackedNonTokenRelationWithoutFeatureValue3() 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);
    
    Span gov = new Span(jcas, t1.getBegin(), t1.getEnd());
    gov.addToIndexes();
    new Span(jcas, t1.getBegin(), t1.getEnd()).addToIndexes();

    Span dep =  new Span(jcas, t2.getBegin(), t2.getEnd());
    dep.addToIndexes();

    Type relationType = cas.getTypeSystem().getType("webanno.custom.Relation");
    
    // One at the beginning
    // WebAnno legacy conventions
    // AnnotationFS fs1 = cas.createAnnotation(relationType, 
    //         min(dep.getBegin(), gov.getBegin()),
    //         max(dep.getEnd(), gov.getEnd()));
    // DKPro Core conventions
    AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
    FSUtil.setFeature(fs1, "Governor", gov);
    FSUtil.setFeature(fs1, "Dependent", dep);
    cas.addFsToIndexes(fs1);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList(Span.class),
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList("webanno.custom.Relation"));
}
 
Example 11
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleSlotFeature() 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(1);
    Token t3 = tokens.get(2);
    
    Type type = cas.getTypeSystem().getType("webanno.custom.SimpleSpan");
    AnnotationFS s2 = cas.createAnnotation(type, t2.getBegin(), t2.getEnd());
    cas.addFsToIndexes(s2);
    AnnotationFS s3 = cas.createAnnotation(type, t3.getBegin(), t3.getEnd());
    cas.addFsToIndexes(s3);

    FeatureStructure link1 = makeLinkFS(jcas, "p1", s2);
    FeatureStructure link2 = makeLinkFS(jcas, "p2", s3);
    
    makeLinkHostFS(jcas, t1.getBegin(), t1.getEnd(), link1, link2);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SLOT_FEATS, asList("webanno.custom.SimpleLinkHost:links"),
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList("webanno.custom.SimpleSpan", 
                    "webanno.custom.SimpleLinkHost"),
            WebannoTsv3Writer.PARAM_LINK_TYPES, asList("webanno.custom.LinkType"),
            WebannoTsv3Writer.PARAM_SLOT_TARGETS, asList("webanno.custom.SimpleSpan"));
}
 
Example 12
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleTokenRelationWithoutFeatureValue() throws Exception
{
    JCas jcas = makeJCasOneSentence();
    CAS cas = jcas.getCas();
    
    List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
    
    Token gov = tokens.get(0);
    Token dep = tokens.get(tokens.size() - 1);

    Type relationType = cas.getTypeSystem().getType("webanno.custom.Relation");
    
    // One at the beginning
    // WebAnno legacy conventions
    // AnnotationFS fs1 = cas.createAnnotation(relationType, 
    //         min(dep.getBegin(), gov.getBegin()),
    //         max(dep.getEnd(), gov.getEnd()));
    // DKPro Core conventions
    AnnotationFS fs1 = cas.createAnnotation(relationType, dep.getBegin(), dep.getEnd());
    FSUtil.setFeature(fs1, "Governor", gov);
    FSUtil.setFeature(fs1, "Dependent", dep);
    cas.addFsToIndexes(fs1);
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_RELATION_LAYERS, asList("webanno.custom.Relation"));
}
 
Example 13
Source File: CasMergeTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
private AnnotationFS createTokenAnno(CAS aCas, int aBegin, int aEnd)
{
    Type type = aCas.getTypeSystem().getType(Token.class.getTypeName());
    AnnotationFS token = aCas.createAnnotation(type, aBegin, aEnd);
    aCas.addFsToIndexes(token);
    return token;
}
 
Example 14
Source File: JsonCasDeserializer.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
public static void deserialize(InputStream inputStream, CAS cas, String encoding) {
	Preconditions.checkNotNull(inputStream, "Paramater input stream is null");
	Preconditions.checkNotNull(inputStream, "Paramater CAS is null");
	
    try {


        JsonFactory factory = new JsonFactory();
        parser = factory.createParser(inputStream);

        SourceDocumentInformation sdi = (SourceDocumentInformation) cas.createAnnotation(cas.getJCas().getCasType(SourceDocumentInformation.type), 0, 0);
        WordAnnotation wa = (WordAnnotation) cas.createAnnotation(cas.getJCas().getCasType(WordAnnotation.type), 0, 0);
        TermOccAnnotation toa = (TermOccAnnotation) cas.createAnnotation(cas.getJCas().getCasType(TermOccAnnotation.type), 0, 0);
        FixedExpression fe = (FixedExpression) cas.createAnnotation(cas.getJCas().getCasType(FixedExpression.type), 0, 0);
        boolean inSdi = false;
        boolean inWa = false;
        boolean inToa = false;
        boolean inFe = false;
        boolean inCoveredText = false;

        while ((token=parser.nextToken()) != null)
        {

                if (inSdi){

                    if (token == JsonToken.END_OBJECT) {
                        inSdi = false;
                    }
                    else {
                        fillSdi(parser,token,sdi);
                    }
                }

                else if (inWa){
                    if (token == JsonToken.END_ARRAY) {
                        inWa = false;
                    }
                    else if (token == JsonToken.END_OBJECT) {
                        wa.addToIndexes();
                        wa = (WordAnnotation) cas.createAnnotation(cas.getJCas().getCasType(WordAnnotation.type), 0, 0);
                    }
                    fillWordAnnotations(parser, token, wa);
                }

                else if (inToa){
                    if (token == JsonToken.END_ARRAY
                            && Objects.equals(parser.getParsingContext().getCurrentName(), "term_occ_annotations")) {
                        inToa = false;
                    }
                    else if (token == JsonToken.END_OBJECT) {
                        toa.addToIndexes();
                        toa = (TermOccAnnotation) cas.createAnnotation(cas.getJCas().getCasType(TermOccAnnotation.type), 0, 0);
                    }
                    FillTermOccAnnotations(parser, token, toa, cas);
                }

                else if (inFe){
                    if (token == JsonToken.END_ARRAY
                            && Objects.equals(parser.getParsingContext().getCurrentName(), "fixed_expressions")) {
                        inFe = false;
                    }
                    else if (token == JsonToken.END_OBJECT) {
                        fe.addToIndexes();
                        fe = (FixedExpression) cas.createAnnotation(cas.getJCas().getCasType(FixedExpression.type), 0, 0);
                    }
                    FillFixedExpressions(parser, token, fe, cas);
                }

                else if (inCoveredText){
                    if (token == JsonToken.VALUE_STRING) {
                        String text = parser.getText();
                        cas.setDocumentText(text);
                    }
                }

                else if ("sdi".equals(parser.getParsingContext().getCurrentName())) {
                    inSdi = true;
                }

                else if ("word_annotations".equals(parser.getParsingContext().getCurrentName())) {
                    inWa = true;
                }

                else if ("term_occ_annotations".equals(parser.getParsingContext().getCurrentName())) {
                    inToa = true;
                }

                else if ("fixed_expressions".equals(parser.getParsingContext().getCurrentName())) {
                    inFe = true;
                }

                else if ("covered_text".equals(parser.getParsingContext().getCurrentName())) {
                    inCoveredText = true;
                }
            }
        sdi.addToIndexes();
    } catch (IOException | CASException e) {
        logger.error("An error occurred during TermSuite Json Cas parsing", e);
    }
}
 
Example 15
Source File: SlotFeatureSupportTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void thatUsingOutOfTagsetValueInOpenTagsetAddsNewValue() throws Exception
{
    final String role = "TAG-NOT-IN-LIST";
    
    CAS cas = jcas.getCas();

    TagSet slotFeatureTagset = new TagSet();
    slotFeatureTagset.setCreateTag(true);
    
    slotFeature.setTagset(slotFeatureTagset);
    
    AnnotationFS hostFS = cas.createAnnotation(hostType, 0, cas.getDocumentText().length());
    AnnotationFS targetFS = cas.createAnnotation(targetType, 0, cas.getDocumentText().length());
    
    when(schemaService.existsTag(role, slotFeatureTagset)).thenReturn(false);
    
    sut.setFeatureValue(jcas.getCas(), slotFeature, getAddr(hostFS),
            asList(new LinkWithRoleModel(role, "dummy", getAddr(targetFS))));
    
    verify(schemaService).createTag(new Tag(slotFeatureTagset, role));
}
 
Example 16
Source File: CasDiffTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
    public void relationStackedSpansTest()
        throws Exception
    {
        TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
        TypeSystemDescription local = TypeSystemDescriptionFactory
                .createTypeSystemDescriptionFromPath(
                        "src/test/resources/desc/type/webannoTestTypes.xml");
       
        TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));

        TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class,
                Sentence.class);
        
        JCas jcasA = JCasFactory.createJCas(merged);
        {
            CAS casA = jcasA.getCas();
            tb.buildTokens(jcasA, "This is a test .");
            
            List<Token> tokensA = new ArrayList<>(select(jcasA, Token.class));
            Token t1A = tokensA.get(0);
            Token t2A = tokensA.get(tokensA.size() - 1);
            
            NamedEntity govA = new NamedEntity(jcasA, t1A.getBegin(), t1A.getEnd());
            govA.addToIndexes();
            // Here we add a stacked named entity!
            new NamedEntity(jcasA, t1A.getBegin(), t1A.getEnd()).addToIndexes();
            
            NamedEntity depA =  new NamedEntity(jcasA, t2A.getBegin(), t2A.getEnd());
            depA.addToIndexes();
    
            Type relationTypeA = casA.getTypeSystem().getType("webanno.custom.Relation");
            AnnotationFS fs1A = casA.createAnnotation(relationTypeA, depA.getBegin(),
                    depA.getEnd());
            FSUtil.setFeature(fs1A, "Governor", govA);
            FSUtil.setFeature(fs1A, "Dependent", depA);
            FSUtil.setFeature(fs1A, "value", "REL");
            casA.addFsToIndexes(fs1A);
        }

        JCas jcasB = JCasFactory.createJCas(merged);
        {
            CAS casB = jcasB.getCas();
            tb.buildTokens(jcasB, "This is a test .");
            
            List<Token> tokensB = new ArrayList<>(select(jcasB, Token.class));
            Token t1B = tokensB.get(0);
            Token t2B = tokensB.get(tokensB.size() - 1);
            
            NamedEntity govB = new NamedEntity(jcasB, t1B.getBegin(), t1B.getEnd());
            govB.addToIndexes();
            NamedEntity depB =  new NamedEntity(jcasB, t2B.getBegin(), t2B.getEnd());
            depB.addToIndexes();
    
            Type relationTypeB = casB.getTypeSystem().getType("webanno.custom.Relation");
            AnnotationFS fs1B = casB.createAnnotation(relationTypeB, depB.getBegin(),
                    depB.getEnd());
            FSUtil.setFeature(fs1B, "Governor", govB);
            FSUtil.setFeature(fs1B, "Dependent", depB);
            FSUtil.setFeature(fs1B, "value", "REL");
            casB.addFsToIndexes(fs1B);
        }

        Map<String, List<CAS>> casByUser = new LinkedHashMap<>();
        casByUser.put("user1", asList(jcasA.getCas()));
        casByUser.put("user2", asList(jcasB.getCas()));

        List<? extends DiffAdapter> diffAdapters = asList(new RelationDiffAdapter(
                "webanno.custom.Relation", WebAnnoConst.FEAT_REL_TARGET,
                WebAnnoConst.FEAT_REL_SOURCE, "value"));

        CasDiff diff = doDiff(diffAdapters, LINK_TARGET_AS_LABEL, casByUser);
        DiffResult result = diff.toResult();
        
        // result.print(System.out);
        
        assertEquals(1, result.size());
        assertEquals(0, result.getDifferingConfigurationSets().size());
        assertEquals(0, result.getIncompleteConfigurationSets().size());
        
        // Todo: Agreement has moved to separate project - should create agreement test there
//        CodingAgreementResult agreement = AgreementUtils.getCohenKappaAgreement(diff,
//                "webanno.custom.Relation", "value", casByUser);
//
//        // Asserts
//        System.out.printf("Agreement: %s%n", agreement.toString());
//        AgreementUtils.dumpAgreementStudy(System.out, agreement);
//
//        assertEquals(1, agreement.getPluralitySets().size());
    }
 
Example 17
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private FeatureStructure createExampleFS(CAS parmCas) throws Exception {
  // Create a view
  CAS englishView = parmCas.createView("EnglishDocument");
  // Set the document text
  englishView.setDocumentText("this beer is good");

  // create an FS of exampleType and index it
  AnnotationFS fs = englishView.createAnnotation(exampleType, 1, 5);

  // create Array FSs
  StringArrayFS strArrayFS = parmCas.createStringArrayFS(5);
  strArrayFS.set(0, "zzzzzz");
  strArrayFS.set(1, "yyyyyy");
  strArrayFS.set(2, "xxxxxx");
  strArrayFS.set(3, "wwwwww");
  strArrayFS.set(4, "vvvvvv");

  IntArrayFS intArrayFS = parmCas.createIntArrayFS(5);
  intArrayFS.set(0, Integer.MAX_VALUE);
  intArrayFS.set(1, Integer.MAX_VALUE - 1);
  intArrayFS.set(2, 42);
  intArrayFS.set(3, Integer.MIN_VALUE + 1);
  intArrayFS.set(4, Integer.MIN_VALUE);

  FloatArrayFS floatArrayFS = parmCas.createFloatArrayFS(5);
  floatArrayFS.set(0, Float.MAX_VALUE);
  floatArrayFS.set(1, (float) (Float.MAX_VALUE / 1000.0));
  floatArrayFS.set(2, 42);
  floatArrayFS.set(3, (float) (Float.MIN_VALUE * 1000.0));
  floatArrayFS.set(4, Float.MIN_VALUE);

  ByteArrayFS byteArrayFS = parmCas.createByteArrayFS(5);
  byteArrayFS.set(0, (byte) 8);
  byteArrayFS.set(1, (byte) 16);
  byteArrayFS.set(2, (byte) 64);
  byteArrayFS.set(3, (byte) 128);
  byteArrayFS.set(4, (byte) 255);

  BooleanArrayFS boolArrayFS = parmCas.createBooleanArrayFS(20);
  boolean val = false;
  for (int i = 0; i < 20; i++) {
    boolArrayFS.set(i, val = !val);
  }

  ShortArrayFS shortArrayFS = parmCas.createShortArrayFS(5);
  shortArrayFS.set(0, Short.MAX_VALUE);
  shortArrayFS.set(1, (short) (Short.MAX_VALUE - 1));
  shortArrayFS.set(2, (short) (Short.MAX_VALUE - 2));
  shortArrayFS.set(3, (short) (Short.MAX_VALUE - 3));
  shortArrayFS.set(4, (short) (Short.MAX_VALUE - 4));

  LongArrayFS longArrayFS = parmCas.createLongArrayFS(5);
  longArrayFS.set(0, Long.MAX_VALUE);
  longArrayFS.set(1, Long.MAX_VALUE - 1);
  longArrayFS.set(2, Long.MAX_VALUE - 2);
  longArrayFS.set(3, Long.MAX_VALUE - 3);
  longArrayFS.set(4, Long.MAX_VALUE - 4);

  DoubleArrayFS doubleArrayFS = parmCas.createDoubleArrayFS(5);
  doubleArrayFS.set(0, Double.MAX_VALUE);
  doubleArrayFS.set(1, Double.MIN_VALUE);
  doubleArrayFS.set(2, Double.parseDouble("1.5555"));
  doubleArrayFS.set(3, Double.parseDouble("99.000000005"));
  doubleArrayFS.set(4, Double.parseDouble("4.44444444444444444"));

  // set features of fs
  fs.setStringValue(stringFeature, "aaaaaaa");
  fs.setFloatValue(floatFeature, (float) 99.99);

  fs.setFeatureValue(intArrayFeature, intArrayFS);
  fs.setFeatureValue(floatArrayFeature, floatArrayFS);
  fs.setFeatureValue(stringArrayFeature, strArrayFS);

  // fs.setByteValue(byteFeature, Byte.MAX_VALUE);
  fs.setByteValue(byteFeature, (byte) 'z');
  fs.setFeatureValue(byteArrayFeature, byteArrayFS);
  fs.setBooleanValue(booleanFeature, true);
  fs.setFeatureValue(booleanArrayFeature, boolArrayFS);
  fs.setShortValue(shortFeature, Short.MIN_VALUE);
  fs.setFeatureValue(shortArrayFeature, shortArrayFS);
  fs.setLongValue(longFeature, Long.MIN_VALUE);
  fs.setFeatureValue(longArrayFeature, longArrayFS);
  fs.setDoubleValue(doubleFeature, Double.MAX_VALUE);
  fs.setFeatureValue(doubleArrayFeature, doubleArrayFS);
  
  englishView.getIndexRepository().addFS(fs);
  return fs;
}
 
Example 18
Source File: ArkTweetTokenizerFixed.java    From argument-reasoning-comprehension-task with Apache License 2.0 4 votes vote down vote up
private void createTokenAnnotation(CAS cas, int start, int end)
{
    AnnotationFS tokenAnno = cas.createAnnotation(tokenType, start, end);
    cas.addFsToIndexes(tokenAnno);
}
 
Example 19
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInNewView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 20
Source File: ComplexTypeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testCountryType()
    throws Exception
{

    TypeSystemDescription tsd = TypeSystemDescriptionFactory
            .createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");

    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    cas.setDocumentText("Asia is the largest continent on Earth. Asia is subdivided into 48 "
            + "countries, two of them (Russia and Turkey) having part of their land in "
            + "Europe. The most active place on Earth for tropical cyclone activity lies "
            + "northeast of the Philippines and south of Japan. The Gobi Desert is in "
            + "Mongolia and the Arabian Desert stretches across much of the Middle East. "
            + "The Yangtze River in China is the longest river in the continent. The "
            + "Himalayas between Nepal and China is the tallest mountain range in the "
            + "world. Tropical rainforests stretch across much of southern Asia and "
            + "coniferous and deciduous forests lie farther north.");
    TypeSystem ts = cas.getTypeSystem();
    Type continentType = ts.getType("de.Continent");
    Feature continentName = continentType.getFeatureByBaseName("name");
    AnnotationFS asiaContinent = cas.createAnnotation(continentType, 0, 4);
    asiaContinent.setStringValue(continentName, "Asia");
    cas.addFsToIndexes(asiaContinent);

    Type countryType = ts.getType("de.Country");
    Feature countryName = countryType.getFeatureByBaseName("name");
    AnnotationFS russia = cas.createAnnotation(countryType, 56, 62);
    russia.setStringValue(countryName, "Russian Federation");
    Feature continentFeature = countryType.getFeatureByBaseName("continent");
    russia.setFeatureValue(continentFeature, asiaContinent);
    cas.addFsToIndexes(russia);

    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream(
            "src/test/resources/rules/region.rules"));
    ParsedConstraints constraints = parser.Parse().accept(new ParserVisitor());

    Evaluator constraintsEvaluator = new ValuesGenerator();

    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(russia,
            "regionType", constraints);

    List<PossibleValue> exValues = new LinkedList<>();
    exValues.add(new PossibleValue("cold", true));

    assertEquals(possibleValues, exValues);
}