org.apache.uima.jcas.JCas Java Examples

The following examples show how to use org.apache.uima.jcas.JCas. 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: 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 #2
Source File: MetaTags.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
public void map(JCas jCas, Element element, AnnotationCollector collector) {

  if ("meta".equalsIgnoreCase(element.tagName())) {
    Metadata md = new Metadata(jCas);

    String name = element.attr("name");
    md.setKey(name);

    String content = element.attr("content");
    String charset = element.attr("charset");
    if (!Strings.isNullOrEmpty(content)) {
      md.setValue(content);
    } else if (!Strings.isNullOrEmpty(charset)) {
      md.setValue(charset);
    }

    collector.add(md);
  }
}
 
Example #3
Source File: SkipSomePosAnnotator2.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {

	for (Token t : select(jCas, Token.class)) {

		// filter by POS
		String pos = t.getPos();
		if (pos == null)
			pos = "UNKNOWN";
		if (SKIP_POS.contains(pos)) {
			for (Annotation a : selectCovered(jCas, Annotation.class,
			        t.getBegin(), t.getEnd())) {
				if (BlueCasUtil.haveSameBeginEnd(t, a)
				        && BIO_ANNOTATIONS.contains(a.getClass()))
					a.removeFromIndexes(jCas);
			}
		}
	}
}
 
Example #4
Source File: WebannoTsv3Reader.java    From webanno with Apache License 2.0 6 votes vote down vote up
private void createSentence(JCas aJCas, String aLine, int aBegin, int aEnd, int aPrevEnd)
{
    // If the next sentence immediately follows the last one without any space or line break
    // in between, then we need to chop off again the linebreak that we added at the end of the
    // last sentence - otherwise offsets will be off on a round-trip.
    if (aPrevEnd == aBegin && coveredText.length() > 0
            && (coveredText.charAt(coveredText.length() - 1) == '\n')) {
        coveredText.deleteCharAt(coveredText.length() - 1);
    }

    if (aPrevEnd + 1 < aBegin) {
        // FIXME This is very slow. Better use StringUtils.repeat()
        StringBuilder pad = new StringBuilder(); // if there is plenty of spaces between
                                                 // sentences
        for (int i = aPrevEnd + 1; i < aBegin; i++) {
            pad.append(" ");
        }
        coveredText.append(pad).append(aLine).append(LF);
    }
    else {
        coveredText.append(aLine).append(LF);
    }
    Sentence sentence = new Sentence(aJCas, aBegin, aEnd);
    sentence.addToIndexes();
}
 
Example #5
Source File: FiltersTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalizeMeasures() throws Exception {
    JCas jCas = getTestCas("ab12 mm");

    Measure m = createAnnot(jCas, Measure.class, 2, 6);
    m.setUnit("mm");
    m.setValue(12f);
    createAnnot(jCas, Keep.class, 2, 6).setEnclosedAnnot(m);

    // -> NOT filtered
    run(jCas, "tokenFrequencyFile", "stopwords_empty", 0, 5000);

    for (Keep select : select(jCas, Keep.class)) {
        Prin.t(select);
    }
    assertTrue(exists(jCas, Keep.class));
    assertEquals(MeasureNormalizerAnnotator.MEASURE_MASK + "mm",
            select(jCas, Keep.class).iterator().next().getNormalizedText());

}
 
Example #6
Source File: WhiteTextCollectionReader.java    From bluima with Apache License 2.0 6 votes vote down vote up
public void getNext(JCas jcas) throws IOException, CollectionException {

        Element articleE = articleIt.next();
        String pmid = articleE.getChildText("PMID");
        LOG.trace("processing pmId {}", pmid);
        currentNrDocs++;

        StringBuilder sb = new StringBuilder();

        int i = addAnnotations(jcas, articleE.getChild("ArticleTitle")
                .getContent(), sb, 0);

        sb.append(" ");// add "space"
        i++;

        addAnnotations(jcas, articleE.getChild("AbstractText").getContent(),
                sb, i);

        jcas.setDocumentText(sb.toString());

        Header h = new Header(jcas);
        h.setDocId(pmid);
        h.addToIndexes();
    }
 
Example #7
Source File: ExternalResourceFactoryTest.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
  assertNotNull("Resource array is null", resourceArray);
  assertEquals(2, resourceArray.length);
  assertTrue("Resource array element 0 is not a DummyResource",
          resourceArray[0] instanceof ResourceWithAssert);
  assertTrue("Resource array element 1 is not a DummyResource",
          resourceArray[1] instanceof ResourceWithAssert);
  assertTrue(resourceArray[0] != resourceArray[1]);
  
  resources.add(resourceArray[0]);
  resources.add(resourceArray[1]);
  
  System.out.printf("Element object 0: %d%n", resourceArray[0].hashCode());
  System.out.printf("Element object 1: %d%n", resourceArray[1].hashCode());
  
  for (ResourceWithAssert res : resourceArray) {
    res.doAsserts();
  }
}
 
Example #8
Source File: MongoTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWithBetweenQuery() throws Exception {

    String query = "{pmid: { $gt: 12, $lt: 19 }}";
    List<JCas> l = asList(createReader(
            MongoCollectionReader.class, 
            BlueUima.PARAM_DB_CONNECTION, conn, BlueUima.PARAM_QUERY, query));
    assertEquals(1, l.size());

    query = "{pmid: { $gt: 18, $lt: 19 }}";
    l = asList(createReader(MongoCollectionReader.class,
             BlueUima.PARAM_DB_CONNECTION, conn,
            BlueUima.PARAM_QUERY, query));
    assertEquals(0, l.size());

    query = "{pmid: { $gt: 8, $lt: 11 }}";
    l = asList(createReader(MongoCollectionReader.class,
             BlueUima.PARAM_DB_CONNECTION, conn,
            BlueUima.PARAM_QUERY, query));
    assertEquals(0, l.size());
}
 
Example #9
Source File: BinaryCasSerDes4.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param cas CAS to serialize
 * @param out output object
 * @param trackingMark tracking mark (for delta serialization)
 * @param compressLevel -
 * @param compressStrategy - 
 * @return null or serialization measurements (depending on setting of doMeasurements)
 * @throws IOException if the marker is invalid
 */
public SerializationMeasures serialize(AbstractCas cas, Object out, Marker trackingMark,
    CompressLevel compressLevel, CompressStrat compressStrategy) throws IOException {
  SerializationMeasures sm = (doMeasurements) ? new SerializationMeasures() : null;
  CASImpl casImpl = (CASImpl) ((cas instanceof JCas) ? ((JCas)cas).getCas(): cas);
  if (null != trackingMark && !trackingMark.isValid() ) {
    throw new CASRuntimeException(CASRuntimeException.INVALID_MARKER, "Invalid Marker.");
  }
  
  Serializer serializer = new Serializer(
      casImpl, makeDataOutputStream(out), (MarkerImpl) trackingMark, sm,
      compressLevel, compressStrategy, false);
 
  serializer.serialize();
  return sm;
}
 
Example #10
Source File: ReNounSeedGenerator.java    From baleen with Apache License 2.0 5 votes vote down vote up
void addEntities(JCas jCas, String match) {
  Matcher matcher = Pattern.compile(match).matcher(jCas.getDocumentText());
  while (matcher.find()) {
    Entity a = new Entity(jCas);
    a.setBegin(matcher.start());
    a.setEnd(matcher.end());
    a.setValue(match);
    addToJCasIndex(a);
  }
}
 
Example #11
Source File: BioASQUtil.java    From bioasq with Apache License 2.0 5 votes vote down vote up
public static List<ConceptSearchResult> searchOntology(GoPubMedService service, JCas jcas,
        String keywords, int pages, int conceptsPerPage, Ontology ontology) throws IOException {
  switch (ontology) {
    case DISEASE: return searchDiseaseOntology(service, jcas, keywords, pages, conceptsPerPage);
    case GENE: return searchGeneOntology(service, jcas, keywords, pages, conceptsPerPage);
    case JOCHEM: return searchJochem(service, jcas, keywords, pages, conceptsPerPage);
    case MESH: return searchMesh(service, jcas, keywords, pages, conceptsPerPage);
    case UNIPROT: return searchUniprot(service, jcas, keywords, pages, conceptsPerPage);
  }
  return ImmutableList.of();
}
 
Example #12
Source File: Pipeline.java    From bluima with Apache License 2.0 5 votes vote down vote up
public void run(JCas jCas) throws UIMAException, IOException {
    try {
        JcasPipelineBuilder builder = new JcasPipelineBuilder(jCas);
        build(builder);
        builder.process();
    } catch (Exception e) {
        throw new UIMAException(e);
    }
}
 
Example #13
Source File: Biocreative2GeneCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
public void getNext(JCas jcas) throws IOException, CollectionException {

        // P00027739T0000 Serum gamma glutamyltransferase in the diagnosis...
        String article = articleIt.next();
        int firstSpace = article.indexOf(" ");
        String articleId = article.substring(0, firstSpace);
        String articleText = article.substring(firstSpace + 1);
        jcas.setDocumentText(articleText);

        Header header = new Header(jcas);
        header.setDocId(articleId);
        header.setComponentId(COMPONENT_ID);
        header.addToIndexes();

        Collection<CorpusEntryDTO> collection = corpusAnnotations
                .get(articleId);
        for (CorpusEntryDTO corpusEntry : collection) {

            // to correct for weird indexing...
            int start = articleText
                    .indexOf(corpusEntry.text, corpusEntry.start);

            BioEntityMention entity = new BioEntityMention(jcas, start, start
                    + corpusEntry.text.length());
            entity.setComponentId(COMPONENT_ID);
            entity.addToIndexes();
            assert (entity.getCoveredText().equals(corpusEntry.text));
        }
    }
 
Example #14
Source File: Unordered.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA
 */
public Unordered(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #15
Source File: PreNeuron.java    From bluima with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public PreNeuron(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #16
Source File: UiucWikiLocationsRedirects.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public UiucWikiLocationsRedirects(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #17
Source File: RoomNumber.java    From bluima with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public RoomNumber(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #18
Source File: UiucWikiCompetitionsBattlesEvents.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public UiucWikiCompetitionsBattlesEvents(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #19
Source File: AbstractMetadataRegexAnnotator.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected Metadata create(JCas jCas, Matcher matcher) {
  Metadata md = new Metadata(jCas);
  md.setKey(key);

  String value = matcher.group(valueGroup);
  md.setValue(convertValue(value));
  return md;
}
 
Example #20
Source File: TemplateRecord.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA
 */
public TemplateRecord(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #21
Source File: CurationTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static FeatureStructure makeLinkFS(JCas aCas, String aSlotLabel, int aTargetBegin,
        int aTargetEnd)
{
    Token token1 = new Token(aCas, aTargetBegin, aTargetEnd);
    token1.addToIndexes();

    Type linkType = aCas.getTypeSystem().getType(LINK_TYPE);
    FeatureStructure linkA1 = aCas.getCas().createFS(linkType);
    linkA1.setStringValue(linkType.getFeatureByBaseName("role"), aSlotLabel);
    linkA1.setFeatureValue(linkType.getFeatureByBaseName("target"), token1);
    aCas.getCas().addFsToIndexes(linkA1);

    return linkA1;
}
 
Example #22
Source File: NeuronDictTerm.java    From bluima with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public NeuronDictTerm(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #23
Source File: IndexRepositoryTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testMissingSofaRef() throws Exception {
  JCas jcas = cas.getJCas();
  Annotation a = new Annotation(jcas, 0, 4);
  FeatureImpl feat = (FeatureImpl) cas.getTypeSystem().getType(CAS.TYPE_NAME_ANNOTATION_BASE)
                       .getFeatureByBaseName(CAS.FEATURE_BASE_NAME_SOFA);
  a._setFeatureValueNcNj(feat, null);
  try {
    jcas.addFsToIndexes(a);
  } catch (CASRuntimeException e) {
    assertEquals("SOFAREF_NOT_SET", e.getMessageKey());
    return;
  }
  fail("required exception not thrown"); // fail
}
 
Example #24
Source File: Size.java    From bluima with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public Size(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #25
Source File: Sentence.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public Sentence(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #26
Source File: RegexSpotter.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void afterRuleProcessing(JCas jCas) {
	this.sw.stop();
	totalTimeInMillis.addAndGet(this.sw.elapsed(TimeUnit.MILLISECONDS));
	LOGGER.debug("Processed MWT spotting on doc {} in {}ms [Cumulated: {}ms]", 
			JCasUtils.getSourceDocumentAnnotation(jCas).get().getUri(), 
			sw.elapsed(TimeUnit.MILLISECONDS), totalTimeInMillis.get());
	flushOccurrenceBuffer(jCas);
}
 
Example #27
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiTokenSpanWithoutFeatureValue() throws Exception
{
    JCas jcas = makeJCasOneSentence();
    
    Span ne = new Span(jcas, 0, jcas.getDocumentText().length());
    ne.addToIndexes();
    
    writeAndAssertEquals(jcas, 
            WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList(Span.class));
}
 
Example #28
Source File: DocumentRelationshipAnnotator.java    From baleen with Apache License 2.0 5 votes vote down vote up
protected List<Relation> createSentenceRelation(
    final JCas jCas,
    final Collection<Entity> sentence1entities,
    final Collection<Entity> sentence2entities,
    final Offset offset,
    final int distance) {

  final List<Relation> relations = new LinkedList<>();

  for (Entity source : sentence1entities) {
    for (Entity target : sentence2entities) {
      Relation relation =
          createRelation(
              jCas,
              source,
              target,
              offset.getBegin(),
              offset.getEnd(),
              type,
              subType,
              "",
              confidence);
      relation.setSentenceDistance(distance);
      relation.setWordDistance(-1);
      relation.setDependencyDistance(-1);
      relations.add(relation);
    }
  }

  return relations;
}
 
Example #29
Source File: PHYS_Inverse.java    From bluima with Apache License 2.0 5 votes vote down vote up
/** @generated
 * @param jcas JCas to which this Feature Structure belongs
 * @param begin offset to the begin spot in the SofA
 * @param end offset to the end spot in the SofA 
*/  
public PHYS_Inverse(JCas jcas, int begin, int end) {
  super(jcas);
  setBegin(begin);
  setEnd(end);
  readObject();
}
 
Example #30
Source File: WebannoTsv1Reader.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * Add sentence layer to CAS
 */
private void createSentence(JCas aJCas, List<Integer> firstTokenInSentence,
        Map<String, Token> tokensStored)
{
    for (int i = 0; i < firstTokenInSentence.size(); i++) {
        Sentence outSentence = new Sentence(aJCas);
        // Only last sentence, and no the only sentence in the document (i!=0)
        if (i == firstTokenInSentence.size() - 1 && i != 0) {
            outSentence.setBegin(tokensStored.get("t_" + firstTokenInSentence.get(i)).getEnd());
            outSentence.setEnd(tokensStored.get("t_" + (tokensStored.size())).getEnd());
            outSentence.addToIndexes();
            break;
        }
        if (i == firstTokenInSentence.size() - 1 && i == 0) {
            outSentence.setBegin(tokensStored.get("t_" + firstTokenInSentence.get(i))
                    .getBegin());
            outSentence.setEnd(tokensStored.get("t_" + (tokensStored.size())).getEnd());
            outSentence.addToIndexes();
        }
        else if (i == 0) {
            outSentence.setBegin(tokensStored.get("t_" + firstTokenInSentence.get(i))
                    .getBegin());
            outSentence.setEnd(tokensStored.get("t_" + firstTokenInSentence.get(i + 1))
                    .getEnd());
            outSentence.addToIndexes();
        }
        else {
            outSentence.setBegin(
                    tokensStored.get("t_" + firstTokenInSentence.get(i)).getEnd() + 1);
            outSentence
                    .setEnd(tokensStored.get("t_" + firstTokenInSentence.get(i + 1)).getEnd());
            outSentence.addToIndexes();
        }
    }
}