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

The following examples show how to use org.apache.uima.fit.util.JCasUtil#selectByIndex() . 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: CorefBracketsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testExistingReferentsNoMerge() throws Exception {
  AnalysisEngine ae = AnalysisEngineFactory.createEngine(CorefBrackets.class);

  populateJCasMergeTest(jCas);
  ae.process(jCas);

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

  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  Coordinate c = JCasUtil.selectByIndex(jCas, Coordinate.class, 0);

  ReferenceTarget lRt = l.getReferent();
  ReferenceTarget cRt = c.getReferent();

  assertNotEquals(lRt, cRt);
}
 
Example 2
Source File: ListTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testmultipleHitsWithText() throws Exception {

  AnalysisEngineDescription aed =
      AnalysisEngineFactory.createEngineDescription(
          List.class, List.PARAM_TERMS, terms, List.PARAM_TYPE, LOCATION);

  AnalysisEngine ae = AnalysisEngineFactory.createEngine(aed);

  // the same search term appears multiple times in text...
  jCas.setDocumentText("Hello world, and hello world again.");
  // but then subset using a Text annotation
  new Text(jCas, 10, jCas.getDocumentText().length()).addToIndexes();

  ae.process(jCas);

  assertEquals(1, JCasUtil.select(jCas, Location.class).size());
  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  assertEquals(WORLD, l.getValue());
  assertEquals(WORLD, l.getCoveredText());
  assertTrue(l.getBegin() > 10);

  ae.destroy();
}
 
Example 3
Source File: CorefBracketsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testExistingLocReferent() throws Exception {
  AnalysisEngine ae = AnalysisEngineFactory.createEngine(CorefBrackets.class);

  jCas.setDocumentText(LOC_TEXT);

  ReferenceTarget rt1 = Annotations.createReferenceTarget(jCas);

  Location l1 = Annotations.createLocation(jCas, 0, 9, SOMEWHERE, null);
  l1.setReferent(rt1);
  Annotations.createCoordinate(jCas, 11, 19, MRGS);

  ae.process(jCas);

  assertEquals(1, JCasUtil.select(jCas, ReferenceTarget.class).size());

  ReferenceTarget rt = JCasUtil.selectByIndex(jCas, ReferenceTarget.class, 0);
  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  Coordinate c = JCasUtil.selectByIndex(jCas, Coordinate.class, 0);

  assertEquals(rt, l.getReferent());
  assertEquals(rt, c.getReferent());
  assertEquals(l.getReferent(), c.getReferent());
}
 
Example 4
Source File: SplitBracketsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiple() throws Exception {
  jCas.setDocumentText("His name was Andrew Smith (Drew) (Smithy)");

  Person p = new Person(jCas, 13, 41);
  p.addToIndexes();

  processJCas();

  assertEquals(3, JCasUtil.select(jCas, Person.class).size());

  Person p1 = JCasUtil.selectByIndex(jCas, Person.class, 0);
  assertEquals("Andrew Smith", p1.getCoveredText());

  Person p2 = JCasUtil.selectByIndex(jCas, Person.class, 1);
  assertEquals("Drew", p2.getCoveredText());

  Person p3 = JCasUtil.selectByIndex(jCas, Person.class, 2);
  assertEquals("Smithy", p3.getCoveredText());

  assertEquals(p1.getReferent(), p2.getReferent());
  assertEquals(p1.getReferent(), p3.getReferent());
}
 
Example 5
Source File: ListTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testmultipleHits() throws Exception {

  AnalysisEngineDescription aed =
      AnalysisEngineFactory.createEngineDescription(
          List.class, List.PARAM_TERMS, terms, List.PARAM_TYPE, LOCATION);

  AnalysisEngine ae = AnalysisEngineFactory.createEngine(aed);

  // the same search term appears multiple times in text...
  jCas.setDocumentText("Hello world, and hello world again.");

  ae.process(jCas);

  assertEquals(2, JCasUtil.select(jCas, Location.class).size());
  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  assertEquals(WORLD, l.getValue());
  assertEquals(WORLD, l.getCoveredText());

  ae.destroy();
}
 
Example 6
Source File: NormalizeTemporalTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrongType() throws Exception {
  jCas.setDocumentText("It was midnight, and all was quiet");

  Temporal t = new Temporal(jCas, 7, 15);
  t.setTimestampStart(LocalDateTime.of(2016, 10, 4, 0, 0, 0).toEpochSecond(ZoneOffset.UTC));
  t.setTimestampStop(LocalDateTime.of(2016, 10, 4, 0, 0, 0).toEpochSecond(ZoneOffset.UTC));
  t.setScope("SINGLE");
  t.setTemporalType("TIME");
  t.setValue("midnight");
  t.addToIndexes();

  processJCas(
      NormalizeTemporal.PARAM_DATE_FORMAT,
      "HH:mm",
      NormalizeTemporal.PARAM_TEMPORAL_TYPE,
      "DATE");

  assertEquals(1, JCasUtil.select(jCas, Temporal.class).size());
  Temporal tTest = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("midnight", tTest.getValue());
  assertFalse(tTest.getIsNormalised());
}
 
Example 7
Source File: RemoveNestedEntitiesTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
  AnalysisEngine rneAE = AnalysisEngineFactory.createEngine(RemoveNestedEntities.class);

  populateJCas(jCas);
  rneAE.process(jCas);

  assertEquals(1, JCasUtil.select(jCas, Person.class).size());
  assertEquals(1, JCasUtil.select(jCas, Temporal.class).size());
  assertEquals(1, JCasUtil.select(jCas, Location.class).size());

  Temporal dt = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("December 1972", dt.getCoveredText());

  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  assertEquals("Oxford", l.getCoveredText());
}
 
Example 8
Source File: NationalityTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
  jCas.setDocumentText(
      "James is a BRITISH national. Last month, he met an Irish bloke in the pub. He is friends with Bob, who is an Spanish.");
  ae.process(jCas);

  assertEquals(3, JCasUtil.select(jCas, Nationality.class).size());

  Nationality british = JCasUtil.selectByIndex(jCas, Nationality.class, 0);
  assertNotNull(british);
  assertEquals("BRITISH", british.getCoveredText());
  assertEquals("BRITISH", british.getValue());
  assertEquals("GBR", british.getCountryCode());

  Nationality irish = JCasUtil.selectByIndex(jCas, Nationality.class, 1);
  assertNotNull(irish);
  assertEquals("Irish", irish.getCoveredText());
  assertEquals("Irish", irish.getValue());
  assertEquals("IRL", irish.getCountryCode());

  Nationality spanish = JCasUtil.selectByIndex(jCas, Nationality.class, 2);
  assertNotNull(spanish);
  assertEquals("Spanish", spanish.getCoveredText());
  assertEquals("Spanish", spanish.getValue());
  assertEquals("ESP", spanish.getCountryCode());
}
 
Example 9
Source File: CorefBracketsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoExistingReferents() throws Exception {
  AnalysisEngine ae = AnalysisEngineFactory.createEngine(CorefBrackets.class);

  jCas.setDocumentText(LOC_TEXT);

  Annotations.createLocation(jCas, 0, 9, SOMEWHERE, null);
  Annotations.createCoordinate(jCas, 11, 19, MRGS);

  ae.process(jCas);

  assertEquals(1, JCasUtil.select(jCas, ReferenceTarget.class).size());

  ReferenceTarget rt = JCasUtil.selectByIndex(jCas, ReferenceTarget.class, 0);
  Location l = JCasUtil.selectByIndex(jCas, Location.class, 0);
  Coordinate c = JCasUtil.selectByIndex(jCas, Coordinate.class, 0);

  assertEquals(rt, l.getReferent());
  assertEquals(rt, c.getReferent());
}
 
Example 10
Source File: OrganisationPersonRoleTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testNested() throws Exception {
  jCas.setDocumentText("A statement US Army Major J Bloggs said that");

  Organisation o = new Organisation(jCas, 12, 34);
  o.addToIndexes();

  Person p = new Person(jCas, 20, 34);
  p.addToIndexes();

  processJCas();

  assertEquals(1, JCasUtil.select(jCas, Relation.class).size());
  Relation r = JCasUtil.selectByIndex(jCas, Relation.class, 0);
  assertEquals("US Army Major J Bloggs", r.getCoveredText());
  assertEquals("ROLE", r.getRelationshipType());

  assertEquals(1, JCasUtil.select(jCas, Organisation.class).size());
  Organisation org = JCasUtil.selectByIndex(jCas, Organisation.class, 0);
  assertEquals("US Army", org.getCoveredText());
}
 
Example 11
Source File: CustomTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingType() throws Exception {
  AnalysisEngine regexAE =
      AnalysisEngineFactory.createEngine(
          Custom.class, Custom.PARAM_PATTERN, DIGIT_REGEX, Custom.PARAM_CASE_SENSITIVE, true);

  jCas.setDocumentText(TEXT);
  regexAE.process(jCas);

  assertEquals(1, JCasUtil.select(jCas, Entity.class).size());

  Entity e1 = JCasUtil.selectByIndex(jCas, Entity.class, 0);
  assertNotNull(e1);
  assertEquals(P123, e1.getCoveredText());
  assertEquals(P123, e1.getValue());

  regexAE.destroy();
}
 
Example 12
Source File: CustomTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testCaseInsensitive() throws Exception {
  AnalysisEngine regexAE =
      AnalysisEngineFactory.createEngine(
          Custom.class,
          Custom.PARAM_TYPE,
          UK_GOV_DSTL_BALEEN_TYPES_COMMON_PERSON,
          Custom.PARAM_PATTERN,
          DIGIT_REGEX,
          Custom.PARAM_CASE_SENSITIVE,
          false);

  jCas.setDocumentText(TEXT);
  regexAE.process(jCas);

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

  Person p1 = JCasUtil.selectByIndex(jCas, Person.class, 0);
  assertNotNull(p1);
  assertEquals(P123, p1.getCoveredText());
  assertEquals(P123, p1.getValue());

  Person p2 = JCasUtil.selectByIndex(jCas, Person.class, 1);
  assertNotNull(p2);
  assertEquals(P456, p2.getCoveredText());
  assertEquals(P456, p2.getValue());

  regexAE.destroy();
}
 
Example 13
Source File: IpV6Test.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testFull() throws Exception {
  jCas.setDocumentText(
      "Here's a full IPv6 address fe80:0000:0000:0000:0204:61ff:fe9d:f156 and some text after it");
  processJCas();

  assertEquals(1, JCasUtil.select(jCas, CommsIdentifier.class).size());
  CommsIdentifier ip = JCasUtil.selectByIndex(jCas, CommsIdentifier.class, 0);
  assertEquals("fe80:0000:0000:0000:0204:61ff:fe9d:f156", ip.getCoveredText());
  assertEquals("ipv6address", ip.getSubType());
}
 
Example 14
Source File: MongoStemmingTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  jCas.setDocumentText("Forty seven knights conspired against the crown.");
  processJCas(MONGO, erd, COLLECTION, MONGO_COLL, TYPE, BUZZWORD);

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

  Buzzword b1 = JCasUtil.selectByIndex(jCas, Buzzword.class, 0);
  assertEquals("knights", b1.getValue());
  assertEquals("knights", b1.getCoveredText());

  Buzzword b2 = JCasUtil.selectByIndex(jCas, Buzzword.class, 1);
  assertEquals("conspired", b2.getValue());
  assertEquals("conspired", b2.getCoveredText());
}
 
Example 15
Source File: DateTimeTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonthDayTime() throws Exception {
  jCas.setDocumentText("It happened at Apr 22, 2014 1529Z");
  processJCas();

  assertEquals(1, JCasUtil.select(jCas, Temporal.class).size());

  Temporal ts1 = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("Apr 22, 2014 1529Z", ts1.getCoveredText());
  assertEquals(1398180540L, ts1.getTimestampStart());
  assertEquals(1398180600L, ts1.getTimestampStop());

  jCas.reset();

  jCas.setDocumentText("It happened at April 22 2014 1529 EST");
  processJCas();

  assertEquals(1, JCasUtil.select(jCas, Temporal.class).size());

  Temporal ts2 = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("April 22 2014 1529 EST", ts2.getCoveredText());
  assertEquals(1398198540L, ts2.getTimestampStart());
  assertEquals(1398198600L, ts2.getTimestampStop());

  jCas.reset();

  jCas.setDocumentText("It happened at April 22, 2014 152947Z");
  processJCas();

  assertEquals(1, JCasUtil.select(jCas, Temporal.class).size());

  Temporal ts3 = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("April 22, 2014 152947Z", ts3.getCoveredText());
  assertEquals(1398180587L, ts3.getTimestampStart());
  assertEquals(1398180588L, ts3.getTimestampStop());
}
 
Example 16
Source File: DateTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDayMonthYears() throws Exception {
  jCas.setDocumentText(
      "He is on duty from 3-10 October 2016, whilst she was on duty 27th September - Monday 3 Oct 16. The Christmas break fell between 21st December 2016 and 2 January 17. On 2/3 January '17 there was a storm, and it rained on 2nd and 5th January 2017.");
  processJCas();

  assertEquals(6, JCasUtil.select(jCas, Temporal.class).size());

  Temporal ts1 = JCasUtil.selectByIndex(jCas, Temporal.class, 0);
  assertEquals("3-10 October 2016", ts1.getCoveredText());
  assertEquals(1475452800L, ts1.getTimestampStart());
  assertEquals(1476144000L, ts1.getTimestampStop());

  Temporal ts2 = JCasUtil.selectByIndex(jCas, Temporal.class, 1);
  assertEquals("27th September - Monday 3 Oct 16", ts2.getCoveredText());
  assertEquals(1474934400L, ts2.getTimestampStart());
  assertEquals(1475539200L, ts2.getTimestampStop());

  Temporal ts3 = JCasUtil.selectByIndex(jCas, Temporal.class, 2);
  assertEquals("21st December 2016 and 2 January 17", ts3.getCoveredText());
  assertEquals(1482278400L, ts3.getTimestampStart());
  assertEquals(1483401600L, ts3.getTimestampStop());

  Temporal ts4 = JCasUtil.selectByIndex(jCas, Temporal.class, 3);
  assertEquals("2/3 January '17", ts4.getCoveredText());
  assertEquals(1483315200L, ts4.getTimestampStart());
  assertEquals(1483488000L, ts4.getTimestampStop());

  Temporal ts5 = JCasUtil.selectByIndex(jCas, Temporal.class, 4);
  assertEquals("2nd and 5th January 2017", ts5.getCoveredText());
  assertEquals("2nd January 2017", ts5.getValue());
  assertEquals(1483315200L, ts5.getTimestampStart());
  assertEquals(1483401600L, ts5.getTimestampStop());

  Temporal ts6 = JCasUtil.selectByIndex(jCas, Temporal.class, 5);
  assertEquals("5th January 2017", ts6.getCoveredText());
  assertEquals(1483574400L, ts6.getTimestampStart());
  assertEquals(1483660800L, ts6.getTimestampStop());
}
 
Example 17
Source File: LatLonDDTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalizeLonFirst() throws Exception {
  String docText =
      "London is located in the UK, at 51.507° N, 0.125°W. Edinburgh is also in the UK, at 55.9530°N,3.1880° W."
          + "But darkest Peru is at 9.19° S, 75.0152°W and South Korea at 35.9078°N, 127.7669° E.";
  jCas.setDocumentText(docText);
  processJCas("storeDecimalValue", true, "storeLongitudeFirst", true);

  assertAnnotations(
      4,
      Coordinate.class,
      new TestCoordinate(0, "51.507° N, 0.125°W", "dd", TYPE_POINT_COORDINATES_0_125_51_507),
      new TestCoordinate(1, "55.9530°N,3.1880° W", "dd", TYPE_POINT_COORDINATES_3_188_55_953),
      new TestCoordinate(2, "9.19° S, 75.0152°W", "dd", TYPE_POINT_COORDINATES_75_0152_09_190),
      new TestCoordinate(
          3, "35.9078°N, 127.7669° E", "dd", TYPE_POINT_COORDINATES_127_766_35_907));

  Coordinate t = JCasUtil.selectByIndex(jCas, Coordinate.class, 0);
  assertEquals("-0.125 51.507", t.getValue());
  assertEquals(true, t.getIsNormalised());
  t = JCasUtil.selectByIndex(jCas, Coordinate.class, 1);
  assertEquals("-3.188 55.953", t.getValue());
  assertEquals(true, t.getIsNormalised());
  t = JCasUtil.selectByIndex(jCas, Coordinate.class, 2);
  assertEquals("-75.0152 -9.19", t.getValue());
  assertEquals(true, t.getIsNormalised());
  t = JCasUtil.selectByIndex(jCas, Coordinate.class, 3);
  assertEquals("127.7669 35.9078", t.getValue());
  assertEquals(true, t.getIsNormalised());
}
 
Example 18
Source File: NPVNPTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void test1() throws UIMAException {
  createPOS();

  Person person = new Person(jCas, 0, 2); // He
  person.addToIndexes();

  Money money = new Money(jCas, 59, 71); // $1.8 billion
  money.addToIndexes();

  Temporal date = new Temporal(jCas, 75, 84); // September
  date.addToIndexes();

  processJCas();

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

  Relation r1 = JCasUtil.selectByIndex(jCas, Relation.class, 0);
  assertEquals("reckons", r1.getValue());
  assertEquals(person, r1.getSource());
  assertEquals("uk.gov.dstl.baleen.types.semantic.Entity", r1.getTarget().getTypeName());
  assertEquals("the current account deficit", r1.getTarget().getCoveredText());

  Relation r2 = JCasUtil.selectByIndex(jCas, Relation.class, 1);
  assertEquals("will narrow to", r2.getValue());
  assertEquals("uk.gov.dstl.baleen.types.semantic.Entity", r2.getSource().getTypeName());
  assertEquals("the current account deficit", r2.getSource().getCoveredText());
  assertEquals(money, r2.getTarget());
}
 
Example 19
Source File: LineReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
  File f = new File(getClass().getResource("lineReader.txt").getPath());
  BaleenCollectionReader bcr = getCollectionReader(LineReader.PARAM_FILE, f.getPath());

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas.getCas());
  assertEquals("This is the first line", jCas.getDocumentText());
  assertEquals(2, JCasUtil.select(jCas, Metadata.class).size());
  Metadata md = JCasUtil.selectByIndex(jCas, Metadata.class, 1);
  assertEquals("lineNumber", md.getKey());
  assertEquals("1", md.getValue());
  assertTrue(getSource(jCas).endsWith("#1"));
  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas.getCas());
  assertEquals("This is the second line", jCas.getDocumentText());
  assertEquals(2, JCasUtil.select(jCas, Metadata.class).size());
  md = JCasUtil.selectByIndex(jCas, Metadata.class, 1);
  assertEquals("lineNumber", md.getKey());
  assertEquals("2", md.getValue());
  assertTrue(getSource(jCas).endsWith("#2"));
  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas.getCas());
  assertEquals("This is the fourth line, but the third one we pick out", jCas.getDocumentText());
  md = JCasUtil.selectByIndex(jCas, Metadata.class, 1);
  assertEquals("lineNumber", md.getKey());
  assertEquals("4", md.getValue());
  assertTrue(getSource(jCas).endsWith("#4"));
  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas.getCas());
  assertEquals(
      "This is the sixth line, but the fourth and final one we pick out", jCas.getDocumentText());
  md = JCasUtil.selectByIndex(jCas, Metadata.class, 1);
  assertEquals("lineNumber", md.getKey());
  assertEquals("6", md.getValue());
  assertTrue(getSource(jCas).endsWith("#6"));
  jCas.reset();

  assertFalse(bcr.doHasNext());
  assertFalse(bcr.doHasNext());

  bcr.close();
}
 
Example 20
Source File: MongoRegexTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testCoref() throws Exception {
  ExternalResourceDescription erd =
      ExternalResourceFactory.createNamedResourceDescription(
          MONGO,
          SharedFongoResource.class,
          FONGO_COLLECTION,
          MONGO_COLL,
          FONGO_DATA,
          objectMapper.writeValueAsString(GAZ_DATA));
  AnalysisEngineDescription aed =
      AnalysisEngineFactory.createEngineDescription(
          MongoRegex.class,
          MONGO,
          erd,
          COLLECTION,
          MONGO_COLL,
          TYPE,
          LOCATION,
          REGEX,
          "\\b[A-Z][a-z]*\\b");

  AnalysisEngine ae = AnalysisEngineFactory.createEngine(aed);

  jCas.setDocumentText("Hello World, Hello Earth");

  ae.process(jCas);

  assertEquals(2, JCasUtil.select(jCas, Location.class).size());
  assertEquals(1, JCasUtil.select(jCas, ReferenceTarget.class).size());

  ReferenceTarget rt = JCasUtil.selectByIndex(jCas, ReferenceTarget.class, 0);

  Location l1 = JCasUtil.selectByIndex(jCas, Location.class, 0);
  assertEquals("World", l1.getValue());
  assertEquals("World", l1.getCoveredText());
  assertEquals(rt, l1.getReferent());

  Location l2 = JCasUtil.selectByIndex(jCas, Location.class, 1);
  assertEquals("Earth", l2.getValue());
  assertEquals("Earth", l2.getCoveredText());
  assertEquals(rt, l2.getReferent());

  ae.destroy();
}