private void saveRelations(String documentId, JCas jCas) { EntityRelationConverter converter = new EntityRelationConverter( getMonitor(), outputHistory, getSupport().getDocumentHistory(jCas), stopFeatures, fields); List<Document> rels = JCasUtil.select(jCas, Relation.class) .stream() .map(converter::convertRelation) .map(Document::new) .peek(d -> d.append(FIELD_DOCUMENT_ID, documentId)) .collect(toList()); if (!rels.isEmpty()) { relationsCollection.insertMany(rels); } } }
private void processFeature(Map<String, Object> map, Base base, Feature f) { if (stopFeatures.contains(f.getName()) || stopFeatures.contains(f.getShortName())) { return; } try { convertFeature(map, base, f); } catch (Exception e) { getMonitor() .warn( "Couldn't output {} to map. Type '{}' isn't supported.", f.getName(), f.getRange().getShortName(), e); } }
/** * Convert from a relation to a map. * * @param relation the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertRelation(Relation relation) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, relation); if (outputHistory && documentHistory != null) { convertHistory(map, relation); } map.put(fields.getExternalId(), relation.getExternalId()); return map; }
new EntityRelationConverter( monitor, false, support.getDocumentHistory(jCas), stopFeatures, fields); entitiesList.add(entityRelationConverter.convertEntity(ent)); relationsList.add(entityRelationConverter.convertRelation(rel));
stopFeatures.add("uk.gov.dstl.baleen.types.BaleenAnnotation:internalId"); EntityRelationConverter erc = new EntityRelationConverter( getMonitor(), false, Map<String, Object> entity = erc.convertEntity(e); entity.put("processedAt", da.getTimestamp()); entities.add(entity);
private void convertFeature(Map<String, Object> map, Base base, Feature f) { if (f.getRange().isPrimitive()) { if (mapGeoJsonToObject && GEO_JSON.equals(f.getShortName())) { getMonitor().trace("Feature is GeoJSON - parsing to a database object"); putGeoJson(map, base.getFeatureValueAsString(f)); } else { getMonitor().trace("Converting primitive feature to an object"); map.put(ConsumerUtils.toCamelCase(f.getShortName()), FeatureUtils.featureToObject(f, base)); && f.getRange().getComponentType() != null && f.getRange().getComponentType().isPrimitive()) { getMonitor().trace("Converting primitive feature to an array"); map.put(ConsumerUtils.toCamelCase(f.getShortName()), FeatureUtils.featureToList(f, base)); } else { getMonitor() .trace("Feature is not a primitive type - will try to treat the feature as an entity"); if (f.getRange().isArray()) { getMonitor().trace("Feature is an array - attempting converstion to an array of entities"); FSArray fArr = (FSArray) base.getFeatureValue(f); if (fArr != null) { map.put(ConsumerUtils.toCamelCase(f.getShortName()), getEntityIds(fArr)); getMonitor().trace("Feature is singular - attempting conversion to a single entity"); FeatureStructure ent = base.getFeatureValue(f); if (ent == null) { map.put(ConsumerUtils.toCamelCase(f.getShortName()), ((Entity) ent).getExternalId()); } else { getMonitor().trace("Unable to persist feature {}", f.getShortName());
private void putGeoJson(Map<String, Object> map, String geojson) { try { if (!Strings.isNullOrEmpty(geojson)) { map.put(fields.getGeoJSON(), mapper.readValue(geojson, mapLikeType)); } } catch (IOException e) { getMonitor().warn("Unable to persist geoJson", e); } }
private void convertFeatures(Map<String, Object> map, Base base) { for (Feature f : base.getType().getFeatures()) { processFeature(map, base, f); } map.put(fields.getType(), base.getType().getShortName()); if (map.get(FIELD_VALUE) == null || Strings.isNullOrEmpty(map.get(FIELD_VALUE).toString())) { map.put(FIELD_VALUE, base.getCoveredText()); } }
final Map<String, Object> map = converter.convertEntity(e); map.remove("begin"); map.remove("end");
final Map<String, Object> map = converter.convertRelation(relation);
private void createERC() { final IEntityConverterFields fields = new DefaultFields(); final HashSet<String> stopFeatures = new HashSet<>(); stopFeatures.add("uima.cas.AnnotationBase:sofa"); stopFeatures.add("uk.gov.dstl.baleen.types.BaleenAnnotation:internalId"); erc = new EntityRelationConverter(monitor, stopFeatures, fields, false); }
/** * Convert from an entity to a map. * * @param entity the entity to convert * @return a map containing the entity's fields (and history is required) */ public Map<String, Object> convertEntity(Entity entity) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, entity); if (outputHistory && documentHistory != null) { convertHistory(map, entity); } map.put(fields.getExternalId(), entity.getExternalId()); return map; }
private void saveEntities(String documentId, JCas jCas) { EntityRelationConverter converter = new EntityRelationConverter( getMonitor(), outputHistory,
/** * Convert from an event to a map. * * @param event the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertEvent(Event event) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, event); if (outputHistory && documentHistory != null) { convertHistory(map, event); } map.put(fields.getExternalId(), event.getExternalId()); return map; }
@Override public void doInitialize(final UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); final IEntityConverterFields fields = new DefaultFields(); final HashSet<String> stopFeatures = new HashSet<>(); stopFeatures.add("uima.cas.AnnotationBase:sofa"); stopFeatures.add("uk.gov.dstl.baleen.types.BaleenAnnotation:internalId"); entityRelationConverter = new EntityRelationConverter(getMonitor(), stopFeatures, fields, false); if (outputDocuments) { initialiseForDocuments(); } if (outputMentions) { initialiseForMentions(); } if (outputEntities) { initialiseForEntities(); } if (outputRelations) { initialiseForRelations(); } }
private Optional<Vertex> traversal(JCas jCas, GraphTraversalSource traversal) { EntityRelationConverter converter = new EntityRelationConverter(monitor, options.getStopFeatures(), fields, false); loadMentions(jCas, traversal, converter); if (options.isOutputRelations()) { loadRelations(jCas, traversal, converter); } else { monitor.debug("DocumentGraph relations ommitted"); } if (options.isOutputEvents()) { loadEvents(jCas, traversal, converter); } else { monitor.debug("DocumentGraph events ommitted"); } return loadDocument(jCas, traversal); }
private Optional<Vertex> traversal(JCas jCas, GraphTraversalSource traversal) { EntityRelationConverter converter = new EntityRelationConverter(monitor, options.getStopFeatures(), fields, false); loadMentions(jCas, traversal, converter); if (options.isOutputRelations()) { loadRelations(jCas, traversal, converter); } else { monitor.debug("DocumentGraph relations ommitted"); } if (options.isOutputEvents()) { loadEvents(jCas, traversal, converter); } else { monitor.debug("DocumentGraph events ommitted"); } return loadDocument(jCas, traversal); }