mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-30 04:40:16 +00:00
- Add new Graph_Store.ipynb introduction notebook - Update Advanced_Graph_Analytics.ipynb with graph store persistence - Update Fraud_Detection.ipynb with graph database storage - Update Transaction_Network_Analysis.ipynb with blockchain graph storage - Update Criminal_Network_Analysis.ipynb with criminal network persistence - Update Welcome_to_Semantica.ipynb with Graph Store module documentation - Update docs/cookbook.md, docs/examples.md, docs/CodeExamples.md - Sync all notebooks to docs/cookbook directory
7.8 KiB
7.8 KiB
In [ ]:
from semantica.kg import GraphBuilder, GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, Deduplicator
builder = GraphBuilder()
analyzer = GraphAnalyzer()
entities = [
{"id": "e1", "type": "Organization", "name": "Apple Inc.", "properties": {}},
{"id": "e2", "type": "Person", "name": "Tim Cook", "properties": {}},
{"id": "e3", "type": "Location", "name": "Cupertino", "properties": {}}
]
relationships = [
{"source": "e2", "target": "e1", "type": "CEO_of", "properties": {}},
{"source": "e1", "target": "e3", "type": "located_in", "properties": {}}
]
kg = builder.build(entities, relationships)
metrics = analyzer.compute_metrics(kg)
print(f"Graph metrics:")
print(f" Entities: {metrics.get('entity_count', 0)}")
print(f" Relationships: {metrics.get('relationship_count', 0)}")
print(f" Density: {metrics.get('density', 0):.3f}")
In [ ]:
centrality_calculator = CentralityCalculator()
degree_centrality = centrality_calculator.calculate_centrality(kg, measure="degree")
betweenness_centrality = centrality_calculator.calculate_centrality(kg, measure="betweenness")
print(f"Degree centrality: {len(degree_centrality)} entities")
print(f"Betweenness centrality: {len(betweenness_centrality)} entities")
In [ ]:
community_detector = CommunityDetector()
communities = community_detector.detect_communities(kg)
print(f"Detected {len(communities)} communities")
for i, community in enumerate(communities[:3], 1):
print(f" Community {i}: {len(community)} entities")
In [ ]:
connectivity_analyzer = ConnectivityAnalyzer()
connectivity = connectivity_analyzer.analyze_connectivity(kg)
print(f"Connectivity analysis:")
print(f" Is connected: {connectivity.get('is_connected', False)}")
print(f" Components: {len(connectivity.get('components', []))}")
In [ ]:
graph_validator = GraphValidator()
deduplicator = Deduplicator()
validation_result = graph_validator.validate(kg)
deduplicated_kg = deduplicator.deduplicate(kg)
print(f"Graph validation: {validation_result.get('valid', False)}")
print(f"Deduplicated entities: {len(deduplicated_kg.get('entities', []))}")
In [ ]:
from semantica.graph_store import GraphStore
# Initialize graph store (using KuzuDB for embedded storage)
graph_store = GraphStore(backend="kuzu", database_path="./analytics_graph_db")
graph_store.connect()
# Store entities as nodes
for entity in entities:
node = graph_store.create_node(
labels=[entity["type"]],
properties={"name": entity["name"], "original_id": entity["id"]}
)
print(f"Stored node: {entity['name']}")
# Store relationships
for rel in relationships:
# In a real scenario, you'd lookup node IDs first
print(f"Relationship: {rel['source']} -{rel['type']}-> {rel['target']}")
# Query using Cypher
results = graph_store.execute_query("MATCH (n) RETURN n.name, labels(n) LIMIT 10")
print(f"\nStored {len(results.get('records', []))} nodes in graph store")
# Get statistics
stats = graph_store.get_stats()
print(f"Graph store stats: {stats}")
graph_store.close()