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

The following examples show how to use org.apache.uima.jcas.JCas#getView() . 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: BmeowTypeAnnotator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
	BmeowTypeDictionary dict = null;
	try {
		if (gold) {
			JCas goldView = jCas.getView("gold");
			dict = constructBmeowTypeDictionaryFromGoldStandard(goldView);
		} else {
			dict = constructBmeowTypeDictionaryFromAida(jCas);
		}
		Collection<Token> tokens = JCasUtil.select(jCas, Token.class);

		for(Token token : tokens){
			annotateBmeowType(jCas, dict, token);
		}
	} catch (EntityLinkingDataAccessException | CASException e) {
		throw new AnalysisEngineProcessException(e);
	}
}
 
Example 2
Source File: TestMistAnalysisEngine.java    From ctakes-docker with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

//        TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("../desc/TypeSystem.xml");
        JCas jcas = JCasFactory.createJCas();
        jcas.setDocumentText("Patient is a 30-year-old man named Leroy Butler from Green Bay, WI.");
        AnalysisEngineDescription aed = AnalysisEngineFactory.createEngineDescription(MistAnalysisEngine.class,
                MistAnalysisEngine.PARAM_MODEL_PATH,
                "SHARP/model/model");
        SimplePipeline.runPipeline(jcas, aed);
        for(Annotation annot : JCasUtil.select(jcas, Annotation.class)){
            System.out.println("Found annotation: " + annot.getCoveredText());
        }
        JCas deidView = jcas.getView(MistAnalysisEngine.DEID_VIEW_NAME);
        System.out.println("Deidentified version:");
        System.out.println(deidView.getDocumentText());
    }
 
Example 3
Source File: I2b2JdbcWriter.java    From ctakes-docker with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * Like process for AEs
 */
@Override
protected void writeJCasInformation( final JCas jcas, 
                                     final String instance,
                                     final int encounterNum,
                                     final long patientNum, final String providerId,
                                     final Timestamp startDate ) throws SQLException {
 
   JCas deidView = null;
   try {
 	  deidView = jcas.getView("DeidView");
   } catch(CASException e) {
 	  throw new RuntimeException(e);
   }

   logger.log(Level.INFO,"view = " + deidView.getViewName());
   writeConceptRows( deidView, instance, Long.toString(patientNum), Integer.toString(encounterNum), providerId );

}
 
Example 4
Source File: I2b2ReadyFileWriter.java    From ctakes-docker with Apache License 2.0 6 votes vote down vote up
@Override
   public void process( final JCas jcas ) throws AnalysisEngineProcessException {

      final String patient = Long.toString( SourceMetadataUtil.getPatientNum( jcas ) );
      final SourceData sourceData = SourceMetadataUtil.getSourceData( jcas );
      final String encounter = sourceData==null?"null":sourceData.getSourceEncounterId();
      final String providerId = sourceData==null?"null":SourceMetadataUtil.getProviderId( sourceData );
      // source date not used ???
//      final String sourceDate = sourceData.getSourceOriginalDate();
      JCas deidView = null;
      try{
        deidView = jcas.getView(DEID_VIEW);
      }catch(CASException e){
        throw new AnalysisEngineProcessException(e);
      }
      final Collection<String> conceptLines = createConceptLines( deidView, patient, encounter, providerId );

      final File outputFile = new File( _outputRootDir, encounter );
      saveAnnotations( outputFile, conceptLines );

   }
 
Example 5
Source File: EvaluationAnnotator.java    From bluima with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {

    JCas goldView, systemView;
    try {
        goldView = jCas.getView(VIEW_GOLD);
        systemView = jCas.getView(VIEW_SYSTEM);
    } catch (CASException e) {
        throw new AnalysisEngineProcessException(e);
    }

    Collection goldAnnot = select(goldView, goldAnnotation);
    Collection systAnnot = select(systemView, systemAnnotation);
    if (verbose)
        System.out
                .println("comparing #gold:" + goldAnnot.size() + " #sys:"
                        + systAnnot.size() + " [pmid "
                        + getHeaderDocId(jCas) + "]");

    String log = evaluator.add(goldAnnot, systAnnot, "");
    if (verbose)
        System.out.println(log);
    // System.out.println(evaluator.compare());
}
 
Example 6
Source File: CooccurrencesEvaluationAnnotator.java    From bluima with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    String pmId = BlueCasUtil.getHeaderDocId(jCas);
    print("pmId " + pmId);

    JCas goldView, systemView;
    try {
        goldView = jCas.getView(VIEW_GOLD);
        systemView = jCas.getView(VIEW_SYSTEM);
    } catch (CASException e) {
        throw new AnalysisEngineProcessException(e);
    }

    Collection goldAnnot = select(goldView, Cooccurrence.class);
    Collection systAnnot = select(systemView, Cooccurrence.class);
    print("comparing #gold:" + goldAnnot.size() + " #sys:"
            + systAnnot.size());

    print(/* "pmId:"+pmId + "\t" + */evaluator.add(goldAnnot, systAnnot,
            pmId));
}
 
Example 7
Source File: Annotator2.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  try {
    JCas sortedView = ViewCreatorAnnotator.createViewSafely(jCas, ViewNames.SORTED_VIEW);
    jCas = jCas.getView(CAS.NAME_DEFAULT_SOFA);
    String initialText = jCas.getDocumentText();
    char[] chars = initialText.toCharArray();
    Arrays.sort(chars);
    String sortedText = new String(chars).trim();
    sortedView.setDocumentText(sortedText);

    sortedView = ViewCreatorAnnotator.createViewSafely(jCas, ViewNames.SORTED_PARENTHESES_VIEW);
    JCas parenthesesView = jCas.getView(ViewNames.PARENTHESES_VIEW);
    String parenthesesText = parenthesesView.getDocumentText();
    chars = parenthesesText.toCharArray();
    Arrays.sort(chars);
    sortedText = new String(chars).trim();
    sortedView.setDocumentText(sortedText);

  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }

}
 
Example 8
Source File: SentenceAndTokenCopier.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  try {
    JCas view1 = jCas.getView(VIEW1);
    JCas view2 = jCas.getView(VIEW2);

    for (Token token1 : select(view1, Token.class)) {
      new Token(view2, token1.getBegin(), token1.getEnd()).addToIndexes();
    }

    for (Sentence sentence1 : select(view1, Sentence.class)) {
      new Sentence(view2, sentence1.getBegin(), sentence1.getEnd()).addToIndexes();
    }
  } catch (CASException ce) {
    throw new AnalysisEngineProcessException(ce);
  }
}
 
Example 9
Source File: JcasSofaTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testGetSofa() throws Exception {
  try {
    File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem.xml");
    TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(typeSystemFile));
    CAS newCas = CasCreationUtils.createCas(typeSystem, null, null);
    File xcasFile = JUnitExtension.getFile("ExampleCas/multiSofaCas.xml"); 
    XCASDeserializer.deserialize(new FileInputStream(xcasFile), newCas);
    JCas newJCas = newCas.getJCas();
    
    SofaID sofaId = new SofaID_impl("EnglishDocument");
    JCas view = newJCas.getView(newJCas.getSofa(sofaId));
  }
  catch (Exception e) {
    JUnitExtension.handleException(e);      
  }      
}
 
Example 10
Source File: CopyAnnotationsAnnotator2Test.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {

    AnalysisEngineDescription copyAnnots = createEngineDescription(
            CopyAnnotationsAnnotator2.class, //
            TO_VIEW, "blah",//
            PARAM_ANNOTATION_CLASS, Protein.class.getName(),//
            DELETE_FROM, true);

    JCas jCas = getTestCas();
    assertEquals("has a DocumentAnnotation at first", 1,
            select(jCas, Annotation.class).size());

    Protein p = new Protein(jCas, 5, 10);
    p.addToIndexes();
    runPipeline(jCas, copyAnnots);
    assertTrue("no more Protein in initial view",
            !exists(jCas, Protein.class));

    JCas newView = jCas.getView("blah");
    Collection<Protein> pNew = select(newView, Protein.class);
    assertEquals("copied to new view", 1, pNew.size());
    assertTrue(haveSameBeginEnd(p, pNew.iterator().next()));

    // copy it back
    AnalysisEngineDescription copyAnnotsBack = createEngineDescription(
            CopyAnnotationsAnnotator2.class, //
            FROM_VIEW, "blah",//
            TO_VIEW, BlueUima.VIEW_SYSTEM,//
            PARAM_ANNOTATION_CLASS, Protein.class.getName(),//
            DELETE_FROM, true);
    runPipeline(jCas, copyAnnotsBack);

    assertTrue(!exists(newView, Protein.class));
    Collection<Protein> pBack = select(jCas, Protein.class);
    assertEquals("copied to protein", 1, pBack.size());
    assertTrue(haveSameBeginEnd(p, pBack.iterator().next()));
}
 
Example 11
Source File: ViewTextCopierAnnotator.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final JCas jCas) throws AnalysisEngineProcessException {
  try {
    final JCas sourceView = jCas.getView(sourceViewName);
    JCas destinationView;
    try {
      destinationView = jCas.getView(destinationViewName);
    } catch (CASRuntimeException ce) {
      destinationView = jCas.createView(destinationViewName);
    }
    destinationView.setDocumentText(sourceView.getDocumentText());
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 12
Source File: Annotator1.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  try {
    JCas parentheticalView = ViewCreatorAnnotator.createViewSafely(jCas,
            ViewNames.PARENTHESES_VIEW);
    jCas = jCas.getView(CAS.NAME_DEFAULT_SOFA);
    String initialText = jCas.getDocumentText();
    String parentheticalText = initialText.replaceAll("[aeiou]+", "($0)");
    parentheticalView.setDocumentText(parentheticalText);
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }

}
 
Example 13
Source File: Annotator3.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  try {
    jCas = jCas.getView(CAS.NAME_DEFAULT_SOFA);
    String text = jCas.getDocumentText();
    String reverseText = reverse(text);
    JCas reverseView = ViewCreatorAnnotator.createViewSafely(jCas, ViewNames.REVERSE_VIEW);
    reverseView.setDocumentText(reverseText);
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 14
Source File: ViewCreatorAnnotatorTest.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  JCas view;
  try {
    view = jCas.getView(CAS.NAME_DEFAULT_SOFA);
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }

  view.setDocumentText("some text");
  Token token = new Token(view, 0, 4);
  token.addToIndexes();
}
 
Example 15
Source File: Evaluator.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
  try {
    JCas goldView = jCas.getView(GOLD_VIEW);
    JCas systemView = jCas.getView(SYSTEM_VIEW);

    for (Sentence goldSentence : select(goldView, Sentence.class)) {
      List<Token> goldTokens = selectCovered(goldView, Token.class, goldSentence);
      List<Token> systemTokens = selectCovered(systemView, Token.class, goldSentence);
      if (goldTokens.size() == systemTokens.size()) {
        for (int i = 0; i < goldTokens.size(); i++) {
          String goldPos = goldTokens.get(i).getPos();
          String systemPos = systemTokens.get(i).getPos();
          if (goldPos.equals(systemPos)) {
            totalCorrect++;
          } else {
            totalWrong++;
          }
        }
      } else {
        throw new RuntimeException(
                "number of tokens in gold view differs from number of tokens in system view");
      }
    }
  } catch (CASException ce) {
    throw new AnalysisEngineProcessException(ce);
  }

}