Java Code Examples for org.apache.uima.jcas.JCas#getCas()

The following examples show how to use org.apache.uima.jcas.JCas#getCas() . 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: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubtokenChain() 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 t4 = tokens.get(3);
    
    Type head = cas.getTypeSystem().getType("webanno.custom.SimpleChain");
    Type link = cas.getTypeSystem().getType("webanno.custom.SimpleLink");
    
    makeChainHead(head,
            makeChainLink(link, cas, t1.getBegin() + 1, t1.getEnd() - 1, null, null, 
            makeChainLink(link, cas, t2.getBegin() + 1, t2.getEnd() - 1, null, null,
            makeChainLink(link, cas, t4.getBegin() + 1, t4.getEnd() - 1, null, null, null))));

    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_CHAIN_LAYERS, asList("webanno.custom.Simple"));
}
 
Example 2
Source File: VisibilityCalculationTests.java    From inception with Apache License 2.0 6 votes vote down vote up
private CAS getTestCas() throws Exception
{
    String documentText = "Dies ist ein Testtext, ach ist der schoen, der schoenste von allen"
            + " Testtexten.";
    JCas jcas = JCasFactory.createText(documentText, "de");

    NamedEntity neLabel = new NamedEntity(jcas, 0, 3);
    neLabel.setValue("LOC");
    neLabel.addToIndexes();

    // the annotation's feature value is initialized as null
    NamedEntity neNoLabel = new NamedEntity(jcas, 13, 20);
    neNoLabel.addToIndexes();

    return jcas.getCas();
}
 
Example 3
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiTokenChain() 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);
    
    Type head = cas.getTypeSystem().getType("webanno.custom.SimpleChain");
    Type link = cas.getTypeSystem().getType("webanno.custom.SimpleLink");
    
    makeChainHead(head,
            makeChainLink(link, cas, t1.getBegin(), t2.getEnd(), null, null, 
            makeChainLink(link, cas, t3.getBegin(), t4.getEnd(), null, null, null)));

    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_CHAIN_LAYERS, asList("webanno.custom.Simple"));
}
 
Example 4
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroLengthSpanBetweenAdjacentTokens() throws Exception
{
    JCas jcas = makeJCas();
    jcas.setDocumentText("word.");
    new Token(jcas, 0,4).addToIndexes();
    new Token(jcas, 4,5).addToIndexes();
    new Sentence(jcas, 0,5).addToIndexes();
    
    CAS cas = jcas.getCas();
    Type simpleSpanType = cas.getTypeSystem().getType("webanno.custom.SimpleSpan");
    
    // Insert zero-width annotation between the adjacent tokens (at end of first token).
    AnnotationFS fs1a = cas.createAnnotation(simpleSpanType, 4, 4);
    cas.addFsToIndexes(fs1a);

    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList("webanno.custom.SimpleSpan"));
}
 
Example 5
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNonMultiTokenRelationWithoutFeatureValue() 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.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 6
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public SCAS copy() throws UIMAException, IOException, ClassNotFoundException {
    JCas jcas = JCasFactory.createJCas();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream docOS = new ObjectOutputStream(buffer);
    CASCompleteSerializer serializerOut = Serialization.serializeCASComplete(this.getJCas().getCasImpl());
    docOS.writeObject(serializerOut);
    docOS.close();
    ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    CASCompleteSerializer serializerIn = (CASCompleteSerializer)is.readObject();
    Serialization.deserializeCASComplete(serializerIn, jcas.getCasImpl());
    return new SCAS(jcas.getCas());
}
 
Example 7
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 8
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleStackedNonTokenOverlappingRelationWithoutFeatureValue() 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(), t2.getEnd());
    gov.addToIndexes();
    new Span(jcas, t1.getBegin(), t2.getEnd()).addToIndexes();

    Span dep =  new Span(jcas, t2.getBegin(), t2.getEnd());
    dep.addToIndexes();
    new Span(jcas, t2.getBegin(), t2.getEnd()).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 9
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleStackedNonTokenRelationWithoutFeatureValue() 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();
    new Span(jcas, t2.getBegin(), t2.getEnd()).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 10
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiTokenStackedSlotFeature() 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(), t3.getEnd());
    cas.addFsToIndexes(s2);
    AnnotationFS s3 = cas.createAnnotation(type, t2.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 11
Source File: EvaluationPreprocessorAnnotator.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {

    // because of later evaluation, copy annotation to view_gold (later used
    // by AnnotationEvaluator) and remove it from view_system.
    Collection<? extends Annotation> goldsFromInitialView = select(jCas,
            goldAnnotation);
    JCas goldView = null;
    try {
        goldView = jCas.createView(VIEW_GOLD);
    } catch (Throwable e) {
        throw new AnalysisEngineProcessException(
                NO_RESOURCE_FOR_PARAMETERS, new Object[] { VIEW_GOLD }, e);
    }

    CasCopier casCopier = new CasCopier(jCas.getCas(), goldView.getCas());

    goldView.setDocumentText(jCas.getDocumentText());
    // view_system annot. stored in List for later delete
    // (conccurentModifExeption)
    List<Annotation> toDelete = new ArrayList<Annotation>();
    for (Annotation g : goldsFromInitialView) {
        goldView.addFsToIndexes(casCopier.copyFs(g));
        if (deleteFrom) {
            toDelete.add(g);
        }
    }
    Annotation[] arr = toDelete.toArray(new Annotation[toDelete.size()]);
    for (int i = 0; i < arr.length; i++) {
        arr[i].removeFromIndexes(jCas);
    }
}
 
Example 12
Source File: RemoteStringMatchingNerRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
private CAS buildCas(String typeSystem) throws IOException, UIMAException
{
    // We need to save the typeSystem XML to disk as the
    // JCasFactory needs a file and not a string
    TypeSystemDescription tsd = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(IOUtils.toInputStream(typeSystem, UTF_8), null));
    JCas jCas = JCasFactory.createJCas(tsd);
    return jCas.getCas();
}
 
Example 13
Source File: StringMatchingRecommenderTest.java    From inception with Apache License 2.0 5 votes vote down vote up
@Test
public void thatPredictionForCrossSentenceLayerWorks() throws Exception
{
    recommender.getLayer().setCrossSentence(true);
    
    StringMatchingRecommender sut = new StringMatchingRecommender(recommender, traits);
    
    JCas jcas = JCasFactory.createJCas();
    TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class);
    builder.buildTokens(jcas, "John Smith .\nPeter Johnheim .");
    CAS cas = jcas.getCas();
    casStorageSession.add("cas", EXCLUSIVE_WRITE_ACCESS, cas);
    
    RecommenderTestHelper.addScoreFeature(cas, NamedEntity.class, "value");

    List<GazeteerEntry> gazeteer = new ArrayList<>();
    gazeteer.add(new GazeteerEntry("Smith . Peter", "ORG"));

    sut.pretrain(gazeteer, context);

    sut.predict(context, cas);

    List<NamedEntity> predictions = getPredictions(cas, NamedEntity.class);

    assertThat(predictions).extracting(NamedEntity::getCoveredText)
            .contains("Smith .\nPeter");
}
 
Example 14
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleSameRoleSlotFeature() 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, "p1", 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 15
Source File: SparkUimaUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static List<SCAS> readFrom(CollectionReader reader) throws IOException, UIMAException {
  List<SCAS> scasList = new ArrayList<>();
  while (reader.hasNext()) {
    JCas jcas = JCasFactory.createJCas();
    CAS cas = jcas.getCas();
    reader.getNext(cas);
    scasList.add(new SCAS(cas));
  }
  return scasList;
}
 
Example 16
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testStackedChain() 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 head = cas.getTypeSystem().getType("webanno.custom.SimpleChain");
    Type link = cas.getTypeSystem().getType("webanno.custom.SimpleLink");
    
    makeChainHead(head,
            makeChainLink(link, cas, t1.getBegin(), t1.getEnd(), null, null, 
            makeChainLink(link, cas, t2.getBegin(), t2.getEnd(), null, null,
            makeChainLink(link, cas, t3.getBegin(), t3.getEnd(), null, null, null))));

    makeChainHead(head,
            makeChainLink(link, cas, t3.getBegin(), t3.getEnd(), null, null, 
            makeChainLink(link, cas, t2.getBegin(), t2.getEnd(), null, null,
            makeChainLink(link, cas, t1.getBegin(), t1.getEnd(), null, null, null))));

    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_CHAIN_LAYERS, asList("webanno.custom.Simple"));
}
 
Example 17
Source File: SimpleTextMerger.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(JCas aJCas) throws AnalysisEngineProcessException {
  // procure a new CAS if we don't have one already
  if (mMergedCas == null) {
    mMergedCas = getEmptyJCas();
  }

  // append document text
  String docText = aJCas.getDocumentText();
  int prevDocLen = mDocBuf.length();
  mDocBuf.append(docText);

  // copy specified annotation types
  CasCopier copier = new CasCopier(aJCas.getCas(), mMergedCas.getCas());
  Set copiedIndexedFs = new HashSet(); // needed in case one annotation is in two indexes (could
  // happen if specified annotation types overlap)
  for (int i = 0; i < mAnnotationTypesToCopy.length; i++) {
    Type type = mMergedCas.getTypeSystem().getType(mAnnotationTypesToCopy[i]);
    FSIndex index = aJCas.getCas().getAnnotationIndex(type);
    Iterator iter = index.iterator();
    while (iter.hasNext()) {
      FeatureStructure fs = (FeatureStructure) iter.next();
      if (!copiedIndexedFs.contains(fs)) {
        Annotation copyOfFs = (Annotation) copier.copyFs(fs);
        // update begin and end
        copyOfFs.setBegin(copyOfFs.getBegin() + prevDocLen);
        copyOfFs.setEnd(copyOfFs.getEnd() + prevDocLen);
        mMergedCas.addFsToIndexes(copyOfFs);
        copiedIndexedFs.add(fs);
      }
    }
  }

  // get the SourceDocumentInformation FS, which indicates the sourceURI of the document
  // and whether the incoming CAS is the last segment
  FSIterator it = aJCas.getAnnotationIndex(SourceDocumentInformation.type).iterator();
  if (!it.hasNext()) {
    throw new AnalysisEngineProcessException(MESSAGE_DIGEST, MISSING_SOURCE_DOCUMENT_INFO,
            new Object[0]);
  }
  SourceDocumentInformation sourceDocInfo = (SourceDocumentInformation) it.next();
  if (sourceDocInfo.getLastSegment()) {
    // time to produce an output CAS
    // set the document text
    mMergedCas.setDocumentText(mDocBuf.toString());

    // add source document info to destination CAS
    SourceDocumentInformation destSDI = new SourceDocumentInformation(mMergedCas);
    destSDI.setUri(sourceDocInfo.getUri());
    destSDI.setOffsetInSource(0);
    destSDI.setLastSegment(true);
    destSDI.addToIndexes();

    mDocBuf = new StringBuffer();
    mReadyToOutput = true;
  }
}
 
Example 18
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 19
Source File: BratAjaxCasUtilTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsBeginEndInSameSentence() throws Exception
{
    JCas jcas = JCasFactory.createJCas();

    JCasBuilder jb = new JCasBuilder(jcas);
    Sentence s1 = jb.add("Sentence 1.", Sentence.class);
    jb.add(" ");
    Sentence s2 = jb.add("Sentence 2.", Sentence.class);
    jb.add(" ");
    Sentence s3 = jb.add(".", Sentence.class);
    Sentence s4 = jb.add(".", Sentence.class);
    jb.close();

    CAS cas = jcas.getCas();
    
    assertTrue(isBeginEndInSameSentence(cas, s2.getBegin(), s2.getEnd()));

    assertTrue(isBeginEndInSameSentence(cas, s1.getBegin() + 1, s1.getEnd() - 1));
    // Invalid because begin > end
    assertTrue(isBeginEndInSameSentence(cas, s1.getEnd() - 1, s1.getBegin() + 1));

    assertTrue(isBeginEndInSameSentence(cas, s1.getBegin(), s1.getEnd()));

    // Invalid because begin > end
    assertFalse(isBeginEndInSameSentence(cas, s2.getBegin(), s1.getBegin()));
    assertFalse(isBeginEndInSameSentence(cas, s1.getBegin(), s2.getBegin()));

    assertTrue(isBeginEndInSameSentence(cas, 0, 0));

    // Note that this is an invalid use of isBeginEndInSameSentence because two begin offsets
    // are compared with each other
    assertTrue(isBeginEndInSameSentence(cas, s3.getBegin(), s4.getBegin()));
    
    // Note that these are invalid uses of isBeginEndInSameSentence because the first offset
    // must be a begin offset
    // Invalid because begin > end
    assertFalse(isBeginEndInSameSentence(cas, s1.getEnd(), s1.getBegin()));
    // Invalid because begin > end
    assertFalse(isBeginEndInSameSentence(cas, s2.getEnd(), s2.getBegin()));
}
 
Example 20
Source File: JCasUtil.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * Convenience method to get the specified view or a default view if the requested view does not
 * exist. The default can also be {@code null}.
 * 
 * @param jcas
 *          a JCas
 * @param viewName
 *          the requested view.
 * @param fallback
 *          the default view if the requested view does not exist.
 * @return the requested view or the default if the requested view does not exist.
 * @throws IllegalStateException
 *           if the JCas wrapper cannot be obtained.
 */
public static JCas getView(JCas jcas, String viewName, JCas fallback) {
  try {
    CAS fallbackCas = fallback != null ? fallback.getCas() : null;
    CAS view = CasUtil.getView(jcas.getCas(), viewName, fallbackCas);
    return view != null ? view.getJCas() : null;
  } catch (CASException e) {
    throw new IllegalStateException(e);
  }
}