Files
KaifAhmad1 01791562f1 refactor(kg): Remove ConflictDetector and Deduplicator from kg module
- Remove ConflictDetector and Deduplicator from semantica.kg module
- Update all imports to use semantica.conflicts and semantica.deduplication
- Update all notebooks to use class-based API (no convenience functions)
- Fix method signatures: pass graph parameter to methods instead of constructor
- Update calculate_centrality calls to use specific methods (calculate_degree_centrality, etc.)
- Fix detect_communities and analyze_connectivity return value handling
- Update all documentation (kg_usage.md, docs/reference/kg.md)
- Remove conflict_detector.py and deduplicator.py from kg module
- Update registry.py to remove conflict and deduplicate task types
2025-12-06 16:17:27 +05:30

5.3 KiB

Open In Colab

Graph Analytics

Overview

This notebook demonstrates how to analyze knowledge graphs using Semantica's analytics modules. You'll learn to use GraphAnalyzer, CentralityCalculator, CommunityDetector, and ConnectivityAnalyzer to understand graph structure and properties.

Documentation: API Reference

Learning Objectives

  • Use GraphAnalyzer for comprehensive graph analysis
  • Use CentralityCalculator to compute centrality measures
  • Use CommunityDetector to find communities in graphs
  • Use ConnectivityAnalyzer to analyze graph connectivity

Installation

Install Semantica from PyPI:

pip install semantica
# Or with all optional dependencies:
pip install semantica[all]

Step 1: Graph Analysis

Analyze graph structure and properties.

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

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}")

Step 2: Centrality Measures

Calculate centrality measures for entities.

In [ ]:
from semantica.kg import CentralityCalculator

centrality_calculator = CentralityCalculator()

centrality_result = centrality_calculator.calculate_degree_centrality(kg)
centrality_scores = centrality_result.get('centrality', {})

print(f"Centrality scores:")
for entity_id, score in list(centrality_scores.items())[:5]:
    print(f"  {entity_id}: {score:.3f}")

Step 3: Community Detection

Detect communities in the graph.

In [ ]:
from semantica.kg import CommunityDetector

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")

Step 4: Connectivity Analysis

Analyze graph connectivity.

In [ ]:
from semantica.kg import ConnectivityAnalyzer

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', []))}")

Summary

You've learned how to analyze knowledge graphs:

  • GraphAnalyzer: Comprehensive graph analysis and metrics
  • CentralityCalculator: Calculate centrality measures
  • CommunityDetector: Detect communities in graphs
  • ConnectivityAnalyzer: Analyze graph connectivity

Next: Learn how to assess graph quality in the Graph_Quality notebook.