/** * Constructor for EntityGraphFactory * * @param monitor to report * @param options to be used during creation of graphs */ public EntityGraphFactory(final UimaMonitor monitor, EntityGraphOptions options) { this.monitor = monitor; this.options = options; DocumentGraphOptions documentOptions = new DocumentGraphOptions.Builder() .withDocument(false) .withRelationsAsLinks(true) .withContentHashAsId(options.isContentHashAsId()) .withEvents(options.isOutputEvents()) .withStopFeatures(options.getStopFeatures()) .withTypeClasses(options.getTypeClasses()) .build(); factory = new DocumentGraphFactory(monitor, documentOptions); }
/** @return the build {@link EntityGraphOptions} */ public EntityGraphOptions build() { if (typeClasses == null) { typeClasses = TypeUtils.getAnnotationClasses(Entity.class); } ValueStrategyProvider valueStrategyProvider = key -> valueStrategies.getOrDefault(key, defaultValueStrategy); return new EntityGraphOptions( contentHashAsId, outputEvents, multiValueProperties, ImmutableSet.copyOf(stopFeatures), typeClasses, valueStrategyProvider, ImmutableList.copyOf(aggregateProperties), valueCoercer); } }
private Map<String, Object> aggregateProperties(Element element) { Map<String, Object> mention = new HashMap<>(); mention.put(ID_PROPERTY, element.id()); options .getAggregateProperties() .forEach(p -> element.property(p).ifPresent(value -> mention.put(p, value))); return mention; }
EntityGraphOptions.builder().withMultiValueProperties(true).build(); UimaSupport.getDocumentAnnotation(jCas), options.isContentHashAsId());
@Test public void testValueCoerser() throws UIMAException { EntityGraphOptions options = EntityGraphOptions.builder().withValueCoercer((v) -> 1).build(); EntityGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); final GraphTraversalSource traversal = graph.traversal(); traversal .V() .forEachRemaining( v -> { Set<Object> values = ElementHelper.propertyValueMap(v).values().stream().collect(Collectors.toSet()); assertAllValuesCoerceTo1(v, values); }); traversal .E() .forEachRemaining( v -> { Set<Object> values = ElementHelper.propertyValueMap(v).values().stream().collect(Collectors.toSet()); assertTrue(values.contains(1) && values.size() == 1 || values.size() == 0); }); }
@SuppressWarnings("rawtypes") protected void addProperty(Features features, Element v, String key, List<?> values) { if (v instanceof Vertex && !Cardinality.single.equals(features.vertex().getCardinality(key))) { values.stream().filter(not(isNull())).forEach(value -> setProperty(v, key, value)); } else { ValueStrategy valueStrategy = options.getValueStrategyProvider().get(key); addSingleProperty(v, key, values, valueStrategy); } }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(options.isMultiValueProperties()); load(jCas, graph, graph.features()); return graph; }
protected Object setProperty(Element v, String key, Object value) { return v.property(key, options.getValueCoercer().coerce(value)); } }
EntityGraphOptions.builder() .withValueStrategy("value", new Longest()) .withValueStrategy("gender", new Mode<String>()) String documentId = ConsumerUtils.getExternalId( UimaSupport.getDocumentAnnotation(jCas), options.isContentHashAsId());
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); try { Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, typeNames); EntityGraphOptions.Builder builder = EntityGraphOptions.builder() .withContentHashAsId(contentHashAsId) .withEvents(outputEvents) .withStopFeatures(filterFeatures) .withAggregateProperties(aggregate) .withTypeClasses(typeClasses) .withMultiValueProperties(multiValueProperties) .withDefaultValueStrategy(createValueStrategy(defaultValueStrategyType)) .withValueCoercer(valueCoercer); if (valueStrategyTypes != null) { for (int i = 0; i < valueStrategyTypes.length; i += 2) { builder.withValueStrategy( valueStrategyTypes[i], createValueStrategy(valueStrategyTypes[i + 1])); } } addOptions(builder); factory = new EntityGraphFactory(getMonitor(), builder.build()); } catch (Exception e) { throw new ResourceInitializationException(e); } }
@SuppressWarnings("rawtypes") protected void addProperty(Features features, Element v, String key, List<?> values) { if (v instanceof Vertex && !Cardinality.single.equals(features.vertex().getCardinality(key))) { values.stream().filter(not(isNull())).forEach(value -> setProperty(v, key, value)); } else { ValueStrategy valueStrategy = options.getValueStrategyProvider().get(key); addSingleProperty(v, key, values, valueStrategy); } }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(options.isMultiValueProperties()); load(jCas, graph, graph.features()); return graph; }
protected Object setProperty(Element v, String key, Object value) { return v.property(key, options.getValueCoercer().coerce(value)); } }
/** * Constructor for EntityGraphFactory * * @param monitor to report * @param options to be used during creation of graphs */ public EntityGraphFactory(final UimaMonitor monitor, EntityGraphOptions options) { this.monitor = monitor; this.options = options; DocumentGraphOptions documentOptions = new DocumentGraphOptions.Builder() .withDocument(false) .withRelationsAsLinks(true) .withContentHashAsId(options.isContentHashAsId()) .withEvents(options.isOutputEvents()) .withStopFeatures(options.getStopFeatures()) .withTypeClasses(options.getTypeClasses()) .build(); factory = new DocumentGraphFactory(monitor, documentOptions); }
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); try { Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, typeNames); EntityGraphOptions.Builder builder = EntityGraphOptions.builder() .withContentHashAsId(contentHashAsId) .withEvents(outputEvents) .withStopFeatures(filterFeatures) .withAggregateProperties(aggregate) .withTypeClasses(typeClasses) .withMultiValueProperties(multiValueProperties) .withDefaultValueStrategy(createValueStrategy(defaultValueStrategyType)) .withValueCoercer(valueCoercer); if (valueStrategyTypes != null) { for (int i = 0; i < valueStrategyTypes.length; i += 2) { builder.withValueStrategy( valueStrategyTypes[i], createValueStrategy(valueStrategyTypes[i + 1])); } } addOptions(builder); factory = new EntityGraphFactory(getMonitor(), builder.build()); } catch (Exception e) { throw new ResourceInitializationException(e); } }
private Map<String, Object> aggregateProperties(Element element) { Map<String, Object> mention = new HashMap<>(); mention.put(ID_PROPERTY, element.id()); options .getAggregateProperties() .forEach(p -> element.property(p).ifPresent(value -> mention.put(p, value))); return mention; }
/** @return the build {@link EntityGraphOptions} */ public EntityGraphOptions build() { if (typeClasses == null) { typeClasses = TypeUtils.getAnnotationClasses(Entity.class); } ValueStrategyProvider valueStrategyProvider = key -> valueStrategies.getOrDefault(key, defaultValueStrategy); return new EntityGraphOptions( contentHashAsId, outputEvents, multiValueProperties, ImmutableSet.copyOf(stopFeatures), typeClasses, valueStrategyProvider, ImmutableList.copyOf(aggregateProperties), valueCoercer); } }
@Test public void testNoEvents() throws UIMAException { EntityGraphOptions options = EntityGraphOptions.builder().withEvents(false).build(); EntityGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); final GraphTraversalSource traversal = graph.traversal(); assertEquals(3, traversal.V().hasLabel(ENTITY).count().next().intValue()); assertEquals(0, traversal.V().hasLabel(EVENT).count().next().intValue()); assertEquals(0, traversal.E().hasLabel(PARTICIPANT_IN).count().next().intValue()); assertEquals(2, traversal.E().hasLabel(RELATION).count().next().intValue()); assertEquals(3, IteratorUtils.count(graph.vertices())); assertEquals(2, IteratorUtils.count(graph.edges())); }
private void copyProperties(Features features, Element from, Element to) { addProperty( features, to, MENTIONS_PROPERTY, Collections.singletonList(aggregateProperties(from))); List<String> aggregateProperties = options.getAggregateProperties(); ElementHelper.propertyValueMap(from) .entrySet() .stream() .filter(e -> !aggregateProperties.contains(e.getKey())) .forEach( property -> { Object value = property.getValue(); if (property instanceof List) { addProperty(features, to, property.getKey(), (List<?>) value); } else { setProperty(to, property.getKey(), value); } }); }
@Test public void testStopFetures() throws UIMAException { EntityGraphOptions options = EntityGraphOptions.builder().withStopFeatures("gender").build(); EntityGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); final GraphTraversalSource traversal = graph.traversal(); assertTrue(traversal.V().not(has("gender")).hasNext()); }