Files
semantica/cookbook/introduction/Building_Knowledge_Graphs.ipynb
T
KaifAhmad1 116bbbd700 Update cookbook notebooks with real data sources
- Replace mock data with real feed URLs, APIs, and database patterns
- Add real threat intelligence feeds (CISA, US-CERT, Security Week, Dark Reading)
- Add real financial feeds (Reuters, CNN Money, Bloomberg, Financial Times)
- Add real healthcare feeds (CDC, WHO)
- Add real API endpoints (MITRE ATT&CK, NVD CVE API, Polygon.io, Alpha Vantage, FHIR APIs)
- Add realistic database connection patterns with SQL queries
- Add Kafka/RabbitMQ streaming configurations
- Update all cybersecurity notebooks (5/5) with real sources
- Update finance notebooks (2/2) with real sources
- Update healthcare notebooks (1/1) with real sources
- Create REAL_DATA_SOURCES.md documentation
- Improve error handling with try-except blocks
- Add batch processing for multiple feed URLs
2025-11-13 16:52:51 +05:30

5.4 KiB

Building Knowledge Graphs

Overview

This notebook demonstrates how to build knowledge graphs from entities and relationships using Semantica's graph building modules. You'll learn to use GraphBuilder, EntityResolver, GraphValidator, and Deduplicator.

Learning Objectives

  • Use GraphBuilder to construct knowledge graphs
  • Use EntityResolver to resolve entity conflicts
  • Use GraphValidator to validate graph structure
  • Use Deduplicator to remove duplicate entities

Step 1: Build Knowledge Graph

Construct a knowledge graph from entities and relationships.

In [ ]:
from semantica.kg import GraphBuilder
from semantica.semantic_extract import NERExtractor, RelationExtractor

builder = GraphBuilder()
ner_extractor = NERExtractor()
relation_extractor = RelationExtractor()

text = "Apple Inc. is a technology company. Tim Cook is the CEO of Apple Inc. Apple Inc. is headquartered in Cupertino, California."

entities_list = ner_extractor.extract(text)
relationships_list = relation_extractor.extract(text, entities_list)

entities = []
for i, entity in enumerate(entities_list[:5], 1):
    entities.append({
        "id": f"e{i}",
        "type": entity.get("type", "Entity"),
        "name": entity.get("text", entity.get("entity", "")),
        "properties": {}
    })

relationships = []
for i, rel in enumerate(relationships_list[:3], 1):
    relationships.append({
        "source": f"e{1}",
        "target": f"e{i+1}",
        "type": rel.get("type", "related_to"),
        "properties": {}
    })

knowledge_graph = builder.build(entities, relationships)

print(f"Built knowledge graph with {len(knowledge_graph.get('entities', []))} entities")
print(f"Relationships: {len(knowledge_graph.get('relationships', []))}")

Step 2: Entity Resolution

Resolve entity conflicts and duplicates.

In [ ]:
from semantica.kg import EntityResolver

entity_resolver = EntityResolver()

resolved_entities = entity_resolver.resolve(entities)

print(f"Original entities: {len(entities)}")
print(f"Resolved entities: {len(resolved_entities)}")

Step 3: Graph Validation

Validate the knowledge graph structure.

In [ ]:
from semantica.kg import GraphValidator

graph_validator = GraphValidator()

validation_result = graph_validator.validate(knowledge_graph)

print(f"Graph validation: {validation_result.get('valid', False)}")
print(f"Issues: {len(validation_result.get('issues', []))}")

Step 4: Deduplication

Remove duplicate entities from the graph.

In [ ]:
from semantica.kg import Deduplicator

deduplicator = Deduplicator()

deduplicated_graph = deduplicator.deduplicate(knowledge_graph)

print(f"Original entities: {len(knowledge_graph.get('entities', []))}")
print(f"Deduplicated entities: {len(deduplicated_graph.get('entities', []))}")

Summary

You've learned how to build knowledge graphs:

  • GraphBuilder: Construct knowledge graphs from entities and relationships
  • EntityResolver: Resolve entity conflicts and duplicates
  • GraphValidator: Validate graph structure and quality
  • Deduplicator: Remove duplicate entities

Next: Learn how to analyze graphs in the Graph_Analytics notebook.