private void processEntities(JCas jCas, Integer docKey) throws SQLException { // Insert entities Map<ReferenceTarget, List<Entity>> coreferenceEntities = new HashMap<>(); for (Entity ent : JCasUtil.select(jCas, Entity.class)) { ReferenceTarget rt = ent.getReferent(); if (rt == null) { rt = new ReferenceTarget(jCas); } List<Entity> entities = coreferenceEntities.getOrDefault(rt, new ArrayList<>()); entities.add(ent); coreferenceEntities.put(rt, entities); } for (List<Entity> entities : coreferenceEntities.values()) { processCoreferencedEntities(docKey, entities); } }
/** * Creates the referent map - map of referent target to mentions. * * @param <T> the generic type * @param jCas the j cas * @param mentions the mentions to map * @param filterEmptyReference true to filter empty references, false to add a stand in reference * @return the multimap */ public static <T extends Base> Multimap<ReferenceTarget, T> createReferentMap( final JCas jCas, final Collection<T> mentions, final boolean filterEmptyReference) { final Multimap<ReferenceTarget, T> targets = HashMultimap.create(); mentions .stream() .filter(p -> !filterEmptyReference || p.getReferent() != null) .forEach( e -> { final ReferenceTarget referent = Optional.ofNullable(e.getReferent()).orElse(new ReferenceTarget(jCas)); targets.put(referent, e); }); return targets; }
/** * Creates the referent map - map of referent target to mentions. * * @param <T> the generic type * @param jCas the j cas * @param mentions the mentions to map * @param filterEmptyReference true to filter empty references, false to add a stand in reference * @return the multimap */ public static <T extends Base> Multimap<ReferenceTarget, T> createReferentMap( final JCas jCas, final Collection<T> mentions, final boolean filterEmptyReference) { final Multimap<ReferenceTarget, T> targets = HashMultimap.create(); mentions .stream() .filter(p -> !filterEmptyReference || p.getReferent() != null) .forEach( e -> { final ReferenceTarget referent = Optional.ofNullable(e.getReferent()).orElse(new ReferenceTarget(jCas)); targets.put(referent, e); }); return targets; }
public static ReferenceTarget createReferenceTarget(JCas jCas) { ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); return rt; }
@SafeVarargs public static <T extends Entity> ReferenceTarget createReferenceTarget(JCas jCas, T... entities) { ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); Arrays.stream(entities).forEach(e -> e.setReferent(rt)); return rt; }
private void setLinking(JCas jCas, EntityInformation<T> entityInformation, String linking) { removeFromJCasIndex(entityInformation.getReferenceTarget()); ReferenceTarget referenceTarget = new ReferenceTarget(jCas); referenceTarget.setLinking(linking); for (Entity e : entityInformation.getMentions()) { e.setReferent(referenceTarget); } addToJCasIndex(referenceTarget); addToJCasIndex(entityInformation.getMentions()); }
private void setLinking(JCas jCas, EntityInformation<T> entityInformation, String linking) { removeFromJCasIndex(entityInformation.getReferenceTarget()); ReferenceTarget referenceTarget = new ReferenceTarget(jCas); referenceTarget.setLinking(linking); for (Entity e : entityInformation.getMentions()) { e.setReferent(referenceTarget); } addToJCasIndex(referenceTarget); addToJCasIndex(entityInformation.getMentions()); }
@Test public void testLocationRT1() throws Exception { jCas.setDocumentText("The former school house at GR 1234 5678."); ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); Location l = new Location(jCas, 4, 23); l.setReferent(rt); l.addToIndexes(); Coordinate c = new Coordinate(jCas, 27, 39); c.addToIndexes(); processJCas(); assertEquals(2, JCasUtil.select(jCas, Location.class).size()); assertEquals(rt, c.getReferent()); }
@Test public void testLocationRT2() throws Exception { jCas.setDocumentText("The former school house at GR 1234 5678."); ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); Location l = new Location(jCas, 4, 23); l.addToIndexes(); Coordinate c = new Coordinate(jCas, 27, 39); c.setReferent(rt); c.addToIndexes(); processJCas(); assertEquals(2, JCasUtil.select(jCas, Location.class).size()); assertEquals(rt, l.getReferent()); }
@Test public void testExtractReferenceTargets() throws AnalysisEngineProcessException, ResourceInitializationException { String text = "Chris went to London and he saw Big Ben there."; // there - london jCas.setDocumentText(text); Person chris = new Person(jCas); chris.setBegin(text.indexOf("Chris")); chris.setEnd(chris.getBegin() + "Chris".length()); chris.addToIndexes(); ReferenceTarget target = new ReferenceTarget(jCas); target.addToIndexes(); Location london = new Location(jCas); london.setBegin(text.indexOf("London")); london.setEnd(london.getBegin() + "London".length()); london.setReferent(target); london.addToIndexes(); Location there = new Location(jCas); there.setBegin(text.indexOf("there")); there.setEnd(there.getBegin() + "there".length()); there.setReferent(target); there.addToIndexes(); processJCas(); processJCasWithSieve(0); // We should have a reference target and it should be different to the previous, as its been // recreated. Collection<ReferenceTarget> targets = JCasUtil.select(jCas, ReferenceTarget.class); assertEquals(1, targets.size()); assertTrue(targets.iterator().next().getInternalId() != target.getInternalId()); }
@Test public void test() throws AnalysisEngineProcessException, ResourceInitializationException { String text = "John say that he would visit London"; jCas.setDocumentText(text); ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); Person p = new Person(jCas); p.setBegin(0); p.setEnd(4); p.setReferent(rt); p.setValue("John"); p.addToIndexes(); WordToken he = new WordToken(jCas); he.setBegin(text.indexOf("he")); he.setEnd(he.getBegin() + "he".length()); he.setReferent(rt); he.addToIndexes(); Location l = new Location(jCas); l.setBegin(text.indexOf("London")); l.setEnd(l.getBegin() + "London".length()); l.setValue("London"); l.addToIndexes(); processJCas(); List<Entity> list = new ArrayList<>(JCasUtil.select(jCas, Entity.class)); assertEquals(3, list.size()); assertEquals("John", list.get(0).getValue()); assertEquals("John", list.get(1).getValue()); assertTrue(list.get(1) instanceof Person); assertEquals("London", list.get(2).getValue()); }
@Test public void testMultiplePersonWithReferences() throws Exception { jCas.setDocumentText( "Mr Simon Brown, was caught stealing sausages. Brown was found guilty. Mr Peter Brown was acquitted."); Person pSimon = new Person(jCas, 0, 14); pSimon.setReferent(new ReferenceTarget(jCas)); pSimon.addToIndexes(); Person pPeter = new Person(jCas, 70, 84); pPeter.setReferent(new ReferenceTarget(jCas)); pPeter.addToIndexes(); processJCas(); assertEquals(3, JCasUtil.select(jCas, Person.class).size()); Person p1 = JCasUtil.selectByIndex(jCas, Person.class, 0); assertEquals("Mr Simon Brown", p1.getCoveredText()); Person p2 = JCasUtil.selectByIndex(jCas, Person.class, 2); assertEquals("Mr Peter Brown", p2.getCoveredText()); Person p3 = JCasUtil.selectByIndex(jCas, Person.class, 1); assertEquals("Brown", p3.getCoveredText()); assertNull(p3.getReferent()); }
UimaSupport support = new UimaSupport(PIPELINE, UimaSupportTest.class, history, monitor, true); ReferenceTarget rt1 = new ReferenceTarget(jCas); rt1.addToIndexes(); ReferenceTarget rt2 = new ReferenceTarget(jCas); rt2.addToIndexes();
jCas.setDocumentText("Chris when to London and he saw Big Ben there"); chrisRT = new ReferenceTarget(jCas); chrisRT.addToIndexes(); londonRT = new ReferenceTarget(jCas); londonRT.addToIndexes();
@Test public void testLocationBothRT() throws Exception { jCas.setDocumentText( "The former school house at GR 1234 5678. The school house has a blue door."); ReferenceTarget rt1 = new ReferenceTarget(jCas); rt1.addToIndexes(); ReferenceTarget rt2 = new ReferenceTarget(jCas); rt2.addToIndexes(); Location l1 = new Location(jCas, 4, 23); l1.setReferent(rt1); l1.addToIndexes(); Coordinate c = new Coordinate(jCas, 27, 39); c.setReferent(rt2); c.addToIndexes(); Location l2 = new Location(jCas, 45, 57); l2.setReferent(rt1); l2.addToIndexes(); processJCas(); assertEquals(3, JCasUtil.select(jCas, Location.class).size()); assertEquals(rt2, c.getReferent()); assertEquals(rt2, l1.getReferent()); assertEquals(rt2, l2.getReferent()); }
@Test public void testSinglePersonWithReferences() throws Exception { jCas.setDocumentText("Mr Simon Brown, was caught stealing sausages. Brown was found guilty."); Person p = new Person(jCas, 0, 14); p.setReferent(new ReferenceTarget(jCas)); p.addToIndexes(); processJCas(); assertEquals(2, JCasUtil.select(jCas, Person.class).size()); Person p1 = JCasUtil.selectByIndex(jCas, Person.class, 0); assertEquals("Mr Simon Brown", p1.getCoveredText()); Person p2 = JCasUtil.selectByIndex(jCas, Person.class, 1); assertEquals("Brown", p2.getCoveredText()); assertNotNull(p1.getReferent()); assertEquals(p1.getReferent(), p2.getReferent()); }
@Test public void testAlreadyExists() throws AnalysisEngineProcessException, ResourceInitializationException { String text = "John say that he would visit London"; jCas.setDocumentText(text); ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes(); Person p = new Person(jCas); p.setBegin(0); p.setEnd(4); p.setReferent(rt); p.setValue("John"); p.addToIndexes(); Person pHe = new Person(jCas); pHe.setBegin(14); pHe.setEnd(16); pHe.setReferent(rt); pHe.setValue("he"); pHe.addToIndexes(); Location l = new Location(jCas); l.setBegin(text.indexOf("London")); l.setEnd(l.getBegin() + "London".length()); l.setValue("London"); l.addToIndexes(); processJCas(); List<Entity> list = new ArrayList<>(JCasUtil.select(jCas, Entity.class)); assertEquals(3, list.size()); // Check no additional entities have been created }
@Test public void testMultiplePersonWithSameReference() throws Exception { jCas.setDocumentText( "Mr Simon Brown, was caught stealing sausages. Brown was found guilty. Mr Simon Brown was sentenced to 5 years."); ReferenceTarget rt = new ReferenceTarget(jCas); Person pSimon = new Person(jCas, 0, 14); pSimon.setReferent(rt); pSimon.addToIndexes(); Person pPeter = new Person(jCas, 70, 84); pPeter.setReferent(rt); pPeter.addToIndexes(); processJCas(); assertEquals(3, JCasUtil.select(jCas, Person.class).size()); Person p1 = JCasUtil.selectByIndex(jCas, Person.class, 0); assertEquals("Mr Simon Brown", p1.getCoveredText()); Person p2 = JCasUtil.selectByIndex(jCas, Person.class, 2); assertEquals("Mr Simon Brown", p2.getCoveredText()); Person p3 = JCasUtil.selectByIndex(jCas, Person.class, 1); assertEquals("Brown", p3.getCoveredText()); assertEquals(rt, p3.getReferent()); } }
da.setDocumentCaveats(sa); ReferenceTarget rt = new ReferenceTarget(jCas); rt.addToIndexes();
public JCasSerializationTester() throws UIMAException { out = JCasFactory.createJCas(); in = JCasFactory.createJCas(); in.setDocumentText("This is a test"); final DocumentAnnotation inDA = (DocumentAnnotation) in.getDocumentAnnotationFs(); inDA.setDocumentClassification("CLASS"); final Location l = new Location(in); l.setBegin(10); l.setEnd(12); l.setGeoJson(GEO_JSON); l.setValue("Dinagat Islands"); l.setConfidence(0.9d); l.addToIndexes(in); final ReferenceTarget rt = new ReferenceTarget(in); rt.setBegin(0); rt.setEnd(1); rt.addToIndexes(in); final Person p = new Person(in); p.setBegin(2); p.setEnd(8); p.setGender("Male"); p.setValue("Peter"); p.setConfidence(0.5d); p.setReferent(rt); p.addToIndexes(in); }