private DocumentGraphFactory createfactory(DocumentGraphOptions options) { UimaMonitor monitor = new UimaMonitor("test", DocumentGraphFactoryTest.class); return new DocumentGraphFactory(monitor, options); }
@Override protected void doProcess(JCas jCas) throws AnalysisEngineProcessException { String documentSourceName = SourceUtils.getDocumentSourceBaseName(jCas); Graph documentGraph = factory.create(jCas); processGraph(documentSourceName, documentGraph); }
private Optional<Vertex> loadDocument(JCas jCas, GraphTraversalSource traversal) { String documentId = getDocumentId(jCas); if (options.isOutputDocument()) { Vertex documentVert = traversal .addV(DOCUMENT) .property(T.id, coerce(documentId)) .sideEffect( tv -> { Vertex documentVertex = tv.get(); getGraphMetadata(jCas) .entrySet() .stream() .forEach(e -> setProperty(documentVertex, e.getKey(), e.getValue())); }) .next(); traversal .V() .filter(v -> !v.get().equals(documentVert)) .addE(MENTION_IN) .to(documentVert) .iterate(); return Optional.of(documentVert); } else { traversal.V().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); traversal.E().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); return Optional.empty(); } }
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into */ public void load(JCas jCas, Graph graph) { GraphTraversalSource traversal = graph.traversal(); Optional<Vertex> document = traversal(jCas, traversal); if (options.isOutputContent()) { loadDocumentContent(jCas, graph, document); } if (options.isOutputMeta()) { loadGraphMetadata(jCas, graph, document); } else { monitor.debug("DocumentGraph metadata skiped"); } }
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); }
DocumentAnnotation da = UimaSupport.getDocumentAnnotation(jCas); setIfValue(variables, FIELD_DOCUMENT_TYPE, da.getDocType()); setIfValue(variables, FIELD_DOCUMENT_SOURCE, da.getSourceUri()); setIfValue(variables, FIELD_DOCUMENT_LANGUAGE, da.getLanguage()); setIfValue(variables, FIELD_DOCUMENT_TIMESTAMP, new Date(da.getTimestamp())); setIfValue(variables, FIELD_DOCUMENT_CLASSIFICATION, da.getDocumentClassification()); setIfListValue( variables, FIELD_DOCUMENT_CAVEATS, UimaTypesUtils.toList(da.getDocumentCaveats())); setIfListValue( variables, FIELD_DOCUMENT_RELEASABILITY, setIfValue(variables, fields.getExternalId(), getDocumentId(jCas));
if (options.isOutputRelationAsLinks() && !traversal.E(coerce(relationId)).hasNext()) { relationTraversal .addE(RELATION) .from(SOURCE) .to(TARGET) .property(T.id, coerce(relationId)) .sideEffect(rt -> addRelationProperties(relation, rt)) .tryNext(); } else if (!options.isOutputRelationAsLinks() && !traversal.V(coerce(relationId)).hasNext()) { relationTraversal .addV(RELATION) .property(T.id, coerce(relationId)) .as(RELATION) .sideEffect(vt -> addRelationProperties(relation, vt)) .addE(SOURCE) .from(SOURCE)
.stream() .map(converter::convertEvent) .filter(e -> !traversal.V(coerce(e.get(fields.getExternalId()))).hasNext()) .forEach( e -> { traversal .addV(EVENT) .property(T.id, coerce(e.get(fields.getExternalId()))) .sideEffect( vt -> .forEach( property -> setProperty( vt.get(), property.getKey(), coerce(property.getValue())))) .next();
.filter( mention -> !traversal.V(coerce(mention.get(fields.getExternalId()))).hasNext()) .map( mention -> traversal .addV(MENTION) .property(T.id, coerce(mention.get(fields.getExternalId()))) .sideEffect(vt -> addMentionProperties(mention, vt)) .next()) .collect(Collectors.toList()); if (!traversal.V(coerce(refId)).hasNext()) { String linking = e.getKey().getLinking() == null ? "" : e.getKey().getLinking(); traversal .addV(REFERENCE_TARGET) .property(FIELD_LINKING, coerce(linking)) .property(T.id, coerce(refId)) .next(); mentions.forEach(
protected Object setProperty(Element v, String key, Object value) { return v.property(key, coerce(value)); } }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph */ public Graph create(JCas jCas) { TinkerGraph graph = TinkerGraph.open(); load(jCas, graph); return graph; }
private void loadGraphMetadata(JCas jCas, Graph graph, Optional<Vertex> document) { if (!document.isPresent()) { Variables variables = graph.variables(); getGraphMetadata(jCas) .entrySet() .stream() .forEach(e -> variables.set(e.getKey(), e.getValue())); } }
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); }
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into */ public void load(JCas jCas, Graph graph) { GraphTraversalSource traversal = graph.traversal(); Optional<Vertex> document = traversal(jCas, traversal); if (options.isOutputContent()) { loadDocumentContent(jCas, graph, document); } if (options.isOutputMeta()) { loadGraphMetadata(jCas, graph, document); } else { monitor.debug("DocumentGraph metadata skiped"); } }
DocumentAnnotation da = UimaSupport.getDocumentAnnotation(jCas); setIfValue(variables, FIELD_DOCUMENT_TYPE, da.getDocType()); setIfValue(variables, FIELD_DOCUMENT_SOURCE, da.getSourceUri()); setIfValue(variables, FIELD_DOCUMENT_LANGUAGE, da.getLanguage()); setIfValue(variables, FIELD_DOCUMENT_TIMESTAMP, new Date(da.getTimestamp())); setIfValue(variables, FIELD_DOCUMENT_CLASSIFICATION, da.getDocumentClassification()); setIfListValue( variables, FIELD_DOCUMENT_CAVEATS, UimaTypesUtils.toList(da.getDocumentCaveats())); setIfListValue( variables, FIELD_DOCUMENT_RELEASABILITY, setIfValue(variables, fields.getExternalId(), getDocumentId(jCas));
if (options.isOutputRelationAsLinks() && !traversal.E(coerce(relationId)).hasNext()) { relationTraversal .addE(RELATION) .from(SOURCE) .to(TARGET) .property(T.id, coerce(relationId)) .sideEffect(rt -> addRelationProperties(relation, rt)) .tryNext(); } else if (!options.isOutputRelationAsLinks() && !traversal.V(coerce(relationId)).hasNext()) { relationTraversal .addV(RELATION) .property(T.id, coerce(relationId)) .as(RELATION) .sideEffect(vt -> addRelationProperties(relation, vt)) .addE(SOURCE) .from(SOURCE)
.stream() .map(converter::convertEvent) .filter(e -> !traversal.V(coerce(e.get(fields.getExternalId()))).hasNext()) .forEach( e -> { traversal .addV(EVENT) .property(T.id, coerce(e.get(fields.getExternalId()))) .sideEffect( vt -> .forEach( property -> setProperty( vt.get(), property.getKey(), coerce(property.getValue())))) .next();
.filter( mention -> !traversal.V(coerce(mention.get(fields.getExternalId()))).hasNext()) .map( mention -> traversal .addV(MENTION) .property(T.id, coerce(mention.get(fields.getExternalId()))) .sideEffect(vt -> addMentionProperties(mention, vt)) .next()) .collect(Collectors.toList()); if (!traversal.V(coerce(refId)).hasNext()) { String linking = e.getKey().getLinking() == null ? "" : e.getKey().getLinking(); traversal .addV(REFERENCE_TARGET) .property(FIELD_LINKING, coerce(linking)) .property(T.id, coerce(refId)) .next(); mentions.forEach(
protected Object setProperty(Element v, String key, Object value) { return v.property(key, coerce(value)); } }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph */ public Graph create(JCas jCas) { TinkerGraph graph = TinkerGraph.open(); load(jCas, graph); return graph; }