/** * Creates the referent map - map of referent target to entities. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @return the multimap */ public static <T extends Base> Multimap<ReferenceTarget, T> createReferentMap( final JCas jCas, final Class<T> clazz) { return createReferentMap(jCas, clazz, true); }
/** * Gets the all annotation which are an entity or which have a referent target which is also the * referent target of an entity. * * <p>Effectively get any annotation where the covered text is considered to be an entity. * * @param jCas the j cas * @param referentMap the referent map * @return the all entity or referent to entity */ public static List<Base> getAllEntityOrReferentToEntity( JCas jCas, Map<ReferenceTarget, Entity> referentMap) { return getAllAndReferents(jCas, Entity.class, referentMap); }
/** * Gets the longest annotation (longest by covered text size). * * @param <T> the generic type * @param list the list * @return the longest single */ public static <T extends Base> T getLongestSingle(Collection<T> list) { return singleViaCompare( list, (a, b) -> Integer.compare(a.getCoveredText().length(), b.getCoveredText().length())); }
/** * Gets the all the annotation type and all the other annotations which have a referent target * which is the referent target for an annotation of this type. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @param referentMap the referent map * @return the all and referents */ @SuppressWarnings("unlikely-arg-type") public static <T extends Base> List<Base> getAllAndReferents( JCas jCas, Class<T> clazz, Map<ReferenceTarget, T> referentMap) { final List<Base> list = new ArrayList<>(); // Add all of the original class list.addAll(JCasUtil.select(jCas, clazz)); // Now find all the referents which point to the same entity streamReferent(jCas, referentMap) // Filter out any existing classes .filter(p -> clazz.isAssignableFrom(p.getClass())) .map(referentMap::get) .forEach(list::add); return list; }
@Test public void testFilterToSingle() { final Map<ReferenceTarget, Entity> map = ReferentUtils.filterToSingle(referentMap, l -> l.iterator().next()); // NB: HashMultimap doesn't preserve the order of elements, so testing for exact elements may // fail. // Instead, we'll just check the type here. assertEquals(chris.getTypeName(), map.get(chrisRT).getTypeName()); assertEquals(london.getTypeName(), map.get(londonRT).getTypeName()); }
/** * Gets the all the annotation type and all the other annotations which have a referent target * which is the referent target for an annotation of this type. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @param referentMap the referent map * @return the all and referents */ @SuppressWarnings("unlikely-arg-type") public static <T extends Base> List<Base> getAllAndReferents( JCas jCas, Class<T> clazz, Map<ReferenceTarget, T> referentMap) { final List<Base> list = new ArrayList<>(); // Add all of the original class list.addAll(JCasUtil.select(jCas, clazz)); // Now find all the referents which point to the same entity streamReferent(jCas, referentMap) // Filter out any existing classes .filter(p -> clazz.isAssignableFrom(p.getClass())) .map(referentMap::get) .forEach(list::add); return list; }
/** * Creates the referent map - map of referent target to entities. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @return the multimap */ public static <T extends Base> Multimap<ReferenceTarget, T> createReferentMap( final JCas jCas, final Class<T> clazz) { return createReferentMap(jCas, clazz, true); }
/** * Gets the all annotation which are an entity or which have a referent target which is also the * referent target of an entity. * * <p>Effectively get any annotation where the covered text is considered to be an entity. * * @param jCas the j cas * @param referentMap the referent map * @return the all entity or referent to entity */ public static List<Base> getAllEntityOrReferentToEntity( JCas jCas, Map<ReferenceTarget, Entity> referentMap) { return getAllAndReferents(jCas, Entity.class, referentMap); }
/** * Gets the longest annotation (longest by covered text size). * * @param <T> the generic type * @param list the list * @return the longest single */ public static <T extends Base> T getLongestSingle(Collection<T> list) { return singleViaCompare( list, (a, b) -> Integer.compare(a.getCoveredText().length(), b.getCoveredText().length())); }
@Test public void testStreamReferent() { final Map<ReferenceTarget, Entity> referentMap = new HashMap<>(); referentMap.put(chrisRT, chris); referentMap.put(londonRT, london); final Stream<Base> stream = ReferentUtils.streamReferent(jCas, referentMap); assertEquals(4, stream.count()); }
/** * Creates the referent map - map of referent target to entities. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @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 Class<T> clazz, final boolean filterEmptyReference) { final Collection<T> potentialReferences = JCasUtil.select(jCas, clazz); return createReferentMap(jCas, potentialReferences, filterEmptyReference); }
@Test public void testGetAllAndReferents() { final Map<ReferenceTarget, Entity> referentMap = new HashMap<>(); final List<Base> list = ReferentUtils.getAllAndReferents(jCas, Entity.class, referentMap); assertEquals(5, list.size()); }
@Test public void testSingleViaCompare() { final Collection<Entity> list = Arrays.asList(chris, he); final Entity entity = ReferentUtils.singleViaCompare( list, (a, b) -> Integer.compare(a.getCoveredText().length(), b.getCoveredText().length())); assertSame(chris, entity); }
/** * Creates the referent map - map of referent target to entities. * * @param <T> the generic type * @param jCas the j cas * @param clazz the clazz * @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 Class<T> clazz, final boolean filterEmptyReference) { final Collection<T> potentialReferences = JCasUtil.select(jCas, clazz); return createReferentMap(jCas, potentialReferences, filterEmptyReference); }
/** * Construct the document factory for the given jCas and {@link SentenceFactory} * * @param jCas to base the document on * @param sentenceFactory to use */ public DocumentFactory(JCas jCas, SentenceFactory sentenceFactory) { this( jCas, JCasUtil.indexCovering(jCas, Entity.class, Sentence.class), ReferentUtils.createReferentMap(jCas, Entity.class), sentenceFactory); }
/** * Construct the document factory for the given jCas and {@link SentenceFactory} * * @param jCas to base the document on * @param sentenceFactory to use */ public DocumentFactory(JCas jCas, SentenceFactory sentenceFactory) { this( jCas, JCasUtil.indexCovering(jCas, Entity.class, Sentence.class), ReferentUtils.createReferentMap(jCas, Entity.class), sentenceFactory); }
@Override public <T extends Entity> Set<EntityInformation<T>> getEntityInformation( JCas jCas, Class<T> clazz) { Multimap<ReferenceTarget, T> map = ReferentUtils.createReferentMap(jCas, clazz); Map<T, Collection<Sentence>> index = JCasUtil.indexCovering(jCas, clazz, Sentence.class); Set<EntityInformation<T>> infos = new HashSet<>(); for (Map.Entry<ReferenceTarget, Collection<T>> entry : map.asMap().entrySet()) { Collection<Sentence> sentences = entry.getValue().stream().flatMap(m -> index.get(m).stream()).collect(Collectors.toSet()); infos.add(new EntityInformation<T>(entry.getKey(), entry.getValue(), sentences)); } return infos; } }
@Override public <T extends Entity> Set<EntityInformation<T>> getEntityInformation( JCas jCas, Class<T> clazz) { Multimap<ReferenceTarget, T> map = ReferentUtils.createReferentMap(jCas, clazz); Map<T, Collection<Sentence>> index = JCasUtil.indexCovering(jCas, clazz, Sentence.class); Set<EntityInformation<T>> infos = new HashSet<>(); for (Map.Entry<ReferenceTarget, Collection<T>> entry : map.asMap().entrySet()) { Collection<Sentence> sentences = entry.getValue().stream().flatMap(m -> index.get(m).stream()).collect(Collectors.toSet()); infos.add(new EntityInformation<T>(entry.getKey(), entry.getValue(), sentences)); } return infos; } }
@Override public <T extends Entity> Set<EntityInformation<T>> getEntityInformation( JCas jCas, Class<T> clazz) { Multimap<ReferenceTarget, T> map = ReferentUtils.createReferentMap(jCas, clazz); Map<T, Collection<Sentence>> index = JCasUtil.indexCovering(jCas, clazz, Sentence.class); Map<T, Collection<WordToken>> tokens = JCasUtil.indexCovered(jCas, clazz, WordToken.class);
@Override public <T extends Entity> Set<EntityInformation<T>> getEntityInformation( JCas jCas, Class<T> clazz) { Multimap<ReferenceTarget, T> map = ReferentUtils.createReferentMap(jCas, clazz); Map<T, Collection<Sentence>> index = JCasUtil.indexCovering(jCas, clazz, Sentence.class); Map<T, Collection<WordToken>> tokens = JCasUtil.indexCovered(jCas, clazz, WordToken.class);