Java Code Examples for org.apache.uima.fit.util.JCasUtil#selectSingle()

The following examples show how to use org.apache.uima.fit.util.JCasUtil#selectSingle() . 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: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoFieldsInRecord()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createNoFieldsRecordDefinition();

  try {
    processJCas(TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY, tempDirectory.toString());

    TemplateRecord record = JCasUtil.selectSingle(jCas, TemplateRecord.class);
    assertEquals(158, record.getBegin());
    assertEquals(212, record.getEnd());
    assertEquals(String.join("\n", "", PARA4, ""), record.getCoveredText());

    Collection<TemplateField> fields = JCasUtil.select(jCas, TemplateField.class);
    assertEquals(0, fields.size());

    assertFalse(JCasUtil.contains(jCas, record, TemplateField.class));

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 2
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultRecord()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createDefaultRecordDefinition();

  try {
    processJCas(TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY, tempDirectory.toString());

    assertEquals(0, JCasUtil.select(jCas, TemplateRecord.class).size());

    TemplateField field1 = JCasUtil.selectSingle(jCas, TemplateField.class);
    assertEquals(53, field1.getBegin());
    assertEquals(105, field1.getEnd());
    assertEquals(PARA2, field1.getCoveredText());
    assertEquals(PARA2, field1.getValue());

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 3
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFieldAnnotationsFromSelectorFileWithRegexDefaultNotNeeded()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createGoodRecordDefinitionWithRegexDefaultNotNeeded();
  try {
    processJCas(TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY, tempDirectory.toString());

    assertRecordCoversParas2to4();

    TemplateField field1 = JCasUtil.selectSingle(jCas, TemplateField.class);
    assertEquals(179, field1.getBegin());
    assertEquals(185, field1.getEnd());
    assertEquals("jumped", field1.getCoveredText());
    assertEquals("jumped", field1.getValue());

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 4
Source File: JCasSerializationTester.java    From baleen with Apache License 2.0 6 votes vote down vote up
public void assertPersonMatches() {
  final Person inPerson = JCasUtil.selectSingle(in, Person.class);
  final Person outPerson = JCasUtil.selectSingle(out, Person.class);
  assertEquals(inPerson.getGender(), outPerson.getGender());
  assertEquals(inPerson.getBegin(), outPerson.getBegin());
  assertEquals(inPerson.getEnd(), outPerson.getEnd());
  assertEquals(inPerson.getValue(), outPerson.getValue());
  assertEquals(0, inPerson.getConfidence(), outPerson.getConfidence());

  // Check that person to entity is deferenced and its the same as the one we get...
  final ReferenceTarget inRt = JCasUtil.selectSingle(in, ReferenceTarget.class);
  final ReferenceTarget outRtFromJCas = JCasUtil.selectSingle(out, ReferenceTarget.class);
  final ReferenceTarget outRt = outPerson.getReferent();
  assertNotNull(outRt);
  assertEquals(inRt.getBegin(), outRt.getBegin());
  assertEquals(inRt.getEnd(), outRt.getEnd());
  assertSame(outRt, outRtFromJCas);
}
 
Example 5
Source File: PubmedDatabaseCRTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthors() throws Exception {

    // http://www.ncbi.nlm.nih.gov/pubmed/?term=1&report=xml&format=text
    CollectionReader cr = createReader(PubmedDatabaseCR.class,
            BlueUima.PARAM_BETWEEN, new int[] { 0, 1 },
            BlueUima.PARAM_SKIP_EMPTY_DOCS, false);

    String[] lastNames = { "Makar", "McMartin", "Palese", "Tephly" };
    String[] foreNames = { "A B", "K E", "M", "T R" };
    // AB___A B___Makar__-__KE___K
    // E___McMartin__-__M___M___Palese__-__TR___T R___Tephly
    for (JCas jCas : asList(cr)) {
        Header header = JCasUtil.selectSingle(jCas, Header.class);

        FSArray authors = header.getAuthors();
        for (int i = 0; i < authors.size(); i++) {
            AuthorInfo a = (AuthorInfo) authors.get(i);
            assertEquals(foreNames[i], a.getForeName());
            assertEquals(lastNames[i], a.getLastName());
        }

        assertEquals("1976-01-16", header.getCopyright());
    }
}
 
Example 6
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDefaultFieldAnnotationsWithDefault()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createGoodRecordDefinitionWithDefaultAndMissing();
  try {
    processJCas(TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY, tempDirectory.toString());

    TemplateField field1 = JCasUtil.selectSingle(jCas, TemplateField.class);
    assertEquals(212, field1.getBegin());
    assertEquals(212, field1.getEnd());
    assertEquals("", field1.getCoveredText());
    assertEquals("default value", field1.getValue());

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 7
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFieldAnnotationsFromSelectorFile()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createGoodRecordDefinition();
  try {
    processJCas(TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY, tempDirectory.toString());

    assertRecordCoversParas2to4();

    TemplateField field1 = JCasUtil.selectSingle(jCas, TemplateField.class);
    assertEquals(53, field1.getBegin());
    assertEquals(105, field1.getEnd());
    assertEquals(PARA2, field1.getCoveredText());
    assertEquals(PARA2, field1.getValue());

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 8
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateRecordWhenNoFollowingPath()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  Path definitionFile = createNoFollowingRecordDefinition();
  try {
    processJCas(
        TemplateAnnotator.PARAM_RECORD_DEFINITIONS_DIRECTORY,
        tempDirectory.toString(),
        TemplateAnnotator.PARAM_TYPE_NAMES,
        new String[] {"Paragraph"});

    TemplateRecord record = JCasUtil.selectSingle(jCas, TemplateRecord.class);
    assertEquals(212, record.getBegin());
    assertEquals(265, record.getEnd());
    assertEquals("\n" + PARA5, record.getCoveredText());

    TemplateField field1 = JCasUtil.selectSingle(jCas, TemplateField.class);
    assertEquals(213, field1.getBegin());
    assertEquals(265, field1.getEnd());
    assertEquals(PARA5, field1.getCoveredText());
    assertEquals(PARA5, field1.getValue());

    assertEquals(2, JCasUtil.select(jCas, Metadata.class).size());

  } finally {
    Files.delete(definitionFile);
  }
}
 
Example 9
Source File: TemplateAnnotatorTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
protected TemplateRecord assertRecordCoversParas2to4() {
  TemplateRecord record = JCasUtil.selectSingle(jCas, TemplateRecord.class);
  assertEquals(52, record.getBegin());
  assertEquals(212, record.getEnd());
  assertEquals(String.join("\n", "", PARA2, PARA3, PARA4, ""), record.getCoveredText());
  return record;
}
 
Example 10
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * @return the value of the {@link Header#getDocId()} for this jCas as an
 *         int, if set; -1 otherwise
 */
public static int getHeaderIntDocId(JCas jCas) {
    try {
        Header header = JCasUtil.selectSingle(jCas, Header.class);
        return Integer.parseInt(header.getDocId());
    } catch (Exception e) {
        return -1;
    }
}
 
Example 11
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * @return the value of the {@link Header#getTitle()} for this jCas, if set;
 *         empty string otherwise
 */
public static String getTitle(JCas jCas) {
    try {
        Header header = JCasUtil.selectSingle(jCas, Header.class);
        return header.getTitle();
    } catch (Exception e) {
        return "";
    }
}
 
Example 12
Source File: JCasSerializationTester.java    From baleen with Apache License 2.0 5 votes vote down vote up
public void assertLocationMatches() {
  final Location inLocation = JCasUtil.selectSingle(in, Location.class);
  final Location outLocation = JCasUtil.selectSingle(out, Location.class);
  assertEquals(inLocation.getGeoJson(), outLocation.getGeoJson());
  assertEquals(inLocation.getBegin(), outLocation.getBegin());
  assertEquals(inLocation.getEnd(), outLocation.getEnd());
  assertEquals(inLocation.getValue(), outLocation.getValue());
  assertEquals(0, inLocation.getConfidence(), outLocation.getConfidence());
}
 
Example 13
Source File: ExternalRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
private CASMetadata getCasMetadata(CAS aCas) throws RecommendationException
{
    try {
        return JCasUtil.selectSingle(aCas.getJCas(), CASMetadata.class);
    } catch (CASException | IllegalArgumentException e) {
        throw new RecommendationException("Error while reading CAS metadata!", e);
    }
}
 
Example 14
Source File: SynchronizedTcuLookUpTable.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private boolean isTheSameDocument(JCas aView) {
    DocumentMetaData meta = JCasUtil.selectSingle(aView,
            DocumentMetaData.class);
    String currentId = meta.getDocumentId();
    boolean isSame = currentId.equals(lastSeenDocumentIdTL.get());
    lastSeenDocumentIdTL.set(currentId);
    return isSame;
}
 
Example 15
Source File: DictionariesExtractor.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private static boolean isTheSameDocument(JCas jcas) {
    DocumentMetaData meta = JCasUtil.selectSingle(jcas,
            DocumentMetaData.class);
    String currentId = meta.getDocumentTitle();

    if (currentId == null) return false;

    boolean isSame = currentId.equals(lastSeenDocumentId);
    logger.trace(lastSeenDocumentId + ", " + currentId);
    lastSeenDocumentId = currentId;
    return isSame;
}
 
Example 16
Source File: Biocreative2GeneCollectionReaderTest.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * GENE.eval
 * P00001606T0076|14 33|alkaline phosphatases
 * P00001606T0076|37 50|5-nucleotidase
 * 
 * train.in:
 * P00001606T0076 Comparison with alkaline phosphatases and 5-nucleotidase
 * </pre>
 */
@Test
public void testTrainCorpus() throws Exception {

    CollectionReader cr = CollectionReaderFactory.createReader(
            Biocreative2GeneCollectionReader.class,
            BlueUima.PARAM_MODE, "train");

    CAS cas = CasCreationUtils
            .createCas(cr.getProcessingResourceMetaData());
    cr.getNext(cas);

    Collection<BioEntityMention> genes = JCasUtil.select(cas.getJCas(),
            BioEntityMention.class);
    assertEquals(2, genes.size());
    Iterator<BioEntityMention> iterator = genes.iterator();
    BioEntityMention gene = iterator.next();
    assertEquals("alkaline phosphatases", gene.getCoveredText());
    gene = iterator.next();
    assertEquals("5-nucleotidase", gene.getCoveredText());

    // move to 'P00027739T0000 Serum gamma glutamyltransferase in the
    // diagnosis of liver disease in cattle.' to test ALTGENE annotations
    for (int i = 0; i < 11; i++) {
        cas = CasCreationUtils
                .createCas(cr.getProcessingResourceMetaData());
        cr.hasNext();
        cr.getNext(cas);
        Header header = JCasUtil.selectSingle(cas.getJCas(), Header.class);
        LOG.debug("docid:{}, text:{}", header.getDocId(),
                cas.getDocumentText());
    }

    genes = JCasUtil.select(cas.getJCas(), BioEntityMention.class);
    iterator = genes.iterator();

    // check the 2 alternate forms
    assertEquals(2, genes.size());
    gene = iterator.next();
    LOG.debug(gene.getCoveredText());
    assertEquals("Serum gamma glutamyltransferase", gene.getCoveredText());
    gene = iterator.next();
    LOG.debug(gene.getCoveredText());
    assertEquals("gamma glutamyltransferase", gene.getCoveredText());
}
 
Example 17
Source File: MongoUpdateWriter.java    From bluima with Apache License 2.0 4 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    try {
        // LOG.debug("updating docId {}", getHeaderIntDocId(jCas));

        Map<String, BasicDBList> dbLists = new HashMap<String, BasicDBList>();

        FSIterator<Annotation> it = jCas.getAnnotationIndex().iterator();
        while (it.hasNext()) {
            Annotation a = it.next();
            String typeName = a.getType().getName();

            if (updateAllAnnotations) {
                if (ALL_MAPPINGS_KEYS.contains(typeName)) {
                    processAnnotaion(a, dbLists, typeName);
                } else {
                    LOG.warn(
                            "no mapping for {}, could not write annotation",
                            typeName);
                }
            } else { // only specific annotations
                if (ALL_MAPPINGS_KEYS.contains(typeName)
                        && updateAnnotationsSet.contains(typeName)) {
                    processAnnotaion(a, dbLists, typeName);
                }
            }
        }

        // insert all dbLists
        BasicDBObject updateQuery = new BasicDBObject(ID,
                getHeaderIntDocId(jCas) + "");
        BasicDBObject updateCommands = new BasicDBObject();
        updateCommands.put("$set", dbLists);
        coll.update(updateQuery, updateCommands, true, false);

    } catch (Throwable t) {
        // e.g. with "DBObject of size  is over Max BSON size"
        String sourceFile = "unknown";
        try {
            Header header = JCasUtil.selectSingle(jCas, Header.class);
            sourceFile = header.getSource();
        } catch (Throwable t2) {// nope
        }
        LOG.error("inserting doc " + sourceFile + StringUtils.print(t), t);
    }
}
 
Example 18
Source File: EntityGraphConsumerTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiGraphTransform()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  processJCas(
      EntityGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      EntityGraphConsumer.PARAM_MULTI_VALUE_PROPERTIES,
      true);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  String documentId = ConsumerUtils.getExternalId(getDocumentAnnotation(jCas), false);

  final GraphTraversalSource traversal = graph.traversal();

  assertEquals(3, traversal.V().hasLabel(ENTITY).count().next().intValue());
  assertEquals(1, traversal.V().hasLabel(EVENT).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(PARTICIPANT_IN).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(RELATION).count().next().intValue());

  assertEquals(4, IteratorUtils.count(graph.vertices()));
  assertEquals(4, IteratorUtils.count(graph.edges()));

  Multimap<ReferenceTarget, Entity> targets =
      ReferentUtils.createReferentMap(jCas, Entity.class, false);
  targets
      .entries()
      .forEach(
          e ->
              assertTrue(
                  traversal
                      .V()
                      .hasLabel(ENTITY)
                      .has("type")
                      .has(MENTIONS_PROPERTY)
                      .has("value")
                      .not(has("begin"))
                      .not(has("end"))
                      .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
                      .hasNext()));

  Event event = JCasUtil.selectSingle(jCas, Event.class);
  assertTrue(
      traversal
          .V(event.getExternalId())
          .hasLabel(EVENT)
          .has("value", event.getValue())
          .not(has("begin"))
          .not(has("end"))
          .has(MENTIONS_PROPERTY)
          .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
          .hasNext());

  JCasUtil.select(jCas, Relation.class)
      .forEach(
          r ->
              assertTrue(
                  traversal
                      .E(r.getExternalId())
                      .hasLabel(RELATION)
                      .has("relationshipType", r.getRelationshipType())
                      .has("value", r.getValue())
                      .not(has("begin"))
                      .not(has("end"))
                      .has(MENTIONS_PROPERTY)
                      .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
                      .hasNext()));

  Map<String, Object> mention = new HashMap<>();
  mention.put("id", "cb7ba8e02c88dcdc832f181c1336ce54334f9bb125bd90371a6d59d098844f23");
  mention.put("begin", 25);
  mention.put("end", 35);
  mention.put("confidence", 0.9d);
  assertTrue(
      traversal
          .V()
          .has("value", "He")
          .has("value", "John Smith")
          .has(MENTIONS_PROPERTY, mention)
          .hasNext());
}
 
Example 19
Source File: HeaderDocIdFileNamer.java    From bluima with Apache License 2.0 4 votes vote down vote up
public String nameFile(JCas jCas) {
Header header = JCasUtil.selectSingle(jCas, Header.class);
return header.getDocId();
   }
 
Example 20
Source File: WriteCoocurrencesToLoadfile2Test.java    From bluima with Apache License 2.0 4 votes vote down vote up
@Test
public void testAEFilter() throws Exception {

    JCas jCas = FilterCoocurrencesByDistanceTest.doTest(5);
    assertTrue("one cooc, distance ok", exists(jCas, Cooccurrence.class));
    Cooccurrence cooc = JCasUtil.selectSingle(jCas, Cooccurrence.class);
    cooc.setCooccurrenceType("bli bli");

    String ouputFile = "target/WriteCoocurrencesToLoadfile2Test_"
            + currentTimeMillis() + ".txt";
    AnalysisEngine wc = createEngine(
            WriteCoocurrencesToLoadfile2.class,//
            PARAM_OUTPUT_FILE,
            ouputFile,//
            PARAM_FIRST_ANNOT_TYPE,
            ProteinDictTerm.class.getSimpleName(),//
            PARAM_SECOND_ANNOT_TYPE, Measure.class.getSimpleName(),
            PARAM_COOCCURRENCE_TYPE, "bli bli", PARAM_VERBOSE, false);
    runPipeline(jCas, wc);
    wc.collectionProcessComplete();// manual call needed

    // verify file
    List<String> lines = LineReader.linesFrom(ouputFile);
    assertEquals(1, lines.size());
    assertEquals(
            "-1\t0.0\tAMPA\t9\t13\tProteinDictTerm\tmM\t0\t5\tMeasure\t0\t24",
            lines.get(0));

    // does not pass filtering
    ouputFile = "target/WriteCoocurrencesToLoadfile2Test_"
            + currentTimeMillis() + ".txt";
    wc = createEngine(
            WriteCoocurrencesToLoadfile2.class,//
            PARAM_OUTPUT_FILE,
            ouputFile,//
            PARAM_FIRST_ANNOT_TYPE,
            ProteinDictTerm.class.getSimpleName(),//
            PARAM_SECOND_ANNOT_TYPE, Measure.class.getSimpleName(),
            PARAM_COOCCURRENCE_TYPE, "bla bla");
    runPipeline(jCas, wc);
    wc.collectionProcessComplete();// manual call needed

    // verify file
    lines = LineReader.linesFrom(ouputFile);
    assertEquals("other files get filtered out", 0,
            lines.size());
}