mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
17 KiB
17 KiB
In [ ]:
!pip install -q semantica In [ ]:
import json
from datetime import datetimeIn [ ]:
# 1. Define source metadata
sources_metadata = {
"hr_db": {"credibility": 0.95, "type": "internal_database"},
"linkedin_scrape": {"credibility": 0.60, "type": "web_scrape"},
"public_dir": {"credibility": 0.40, "type": "public_api"}
}
# 2. Define entity records from these sources
entity_records = [
{
"id": "emp_001",
"name": "John Doe",
"birth_date": "1980-05-15",
"department": "Engineering",
"source": "hr_db",
"timestamp": "2023-01-01T10:00:00"
},
{
"id": "emp_001",
"name": "Jonathan Doe",
"birth_date": "1980-05-15",
"department": "Software Engineering",
"source": "linkedin_scrape",
"timestamp": "2023-06-15T14:30:00"
},
{
"id": "emp_001",
"name": "John Doe",
"birth_date": "1982-05-15", # Conflict: Different year
"department": "Engineering",
"source": "public_dir",
"timestamp": "2022-12-01T09:00:00"
}
]
print(f"Loaded {len(entity_records)} records for Employee 001")In [ ]:
from semantica.conflicts import SourceTracker
source_tracker = SourceTracker()
print("Registering sources...")
for source_id, metadata in sources_metadata.items():
source_tracker.register_source(
source_id=source_id,
source_type=metadata["type"],
credibility_score=metadata["credibility"]
)
print(f" - Registered '{source_id}' with credibility {metadata['credibility']}")In [ ]:
from semantica.conflicts import ConflictDetector
# Initialize detector with our populated source tracker
detector = ConflictDetector(source_tracker=source_tracker)
conflicts = []
# 1. Check birth_date
dob_conflicts = detector.detect_value_conflicts(entity_records, "birth_date")
conflicts.extend(dob_conflicts)
# 2. Check department
dept_conflicts = detector.detect_value_conflicts(entity_records, "department")
conflicts.extend(dept_conflicts)
print(f"Detected {len(conflicts)} conflicts:")
for conflict in conflicts:
print(f"- {conflict.conflict_type.value}: {conflict.property_name} for {conflict.entity_id}")
print(f" Values: {conflict.conflicting_values}")
print(f" Severity: {conflict.severity}")
print("--- ")In [ ]:
from semantica.conflicts import ConflictAnalyzer
analyzer = ConflictAnalyzer()
analysis = analyzer.analyze_conflicts(conflicts)
print("Conflict Analysis Summary:")
print(f"Total Conflicts: {analysis['total_conflicts']}")
print(f"By Type: {analysis.get('by_type', {}).get('counts')}")
print(f"By Severity: {analysis.get('by_severity', {}).get('counts')}")In [ ]:
from semantica.conflicts import ConflictResolver
resolver = ConflictResolver()
# CRITICAL: Link the source tracker to the resolver.
# This allows the resolver to look up the credibility scores we registered in Step 2.
resolver.set_source_tracker(source_tracker)
print("--- Resolution: Voting ---")
voting_results = resolver.resolve_conflicts(conflicts, strategy="voting")
for res in voting_results:
print(f"Property: {res.metadata.get('property_name'):<15} | Resolved Value: {res.resolved_value}")
print("\n--- Resolution: Credibility Weighted ---")
# Notice how the HR DB's value is preferred due to higher credibility
credibility_results = resolver.resolve_conflicts(conflicts, strategy="credibility_weighted")
for res in credibility_results:
print(f"Property: {res.metadata.get('property_name'):<15} | Resolved Value: {res.resolved_value} (Confidence: {res.confidence:.2f})")In [ ]:
from semantica.conflicts import InvestigationGuideGenerator
guide_generator = InvestigationGuideGenerator()
# Generate a guide for the first conflict (birth_date)
guide = guide_generator.generate_guide(conflicts[0])
print(f"=== {guide.title} ===")
print(f"Summary: {guide.conflict_summary}\n")
print("Investigation Steps:")
for i, step in enumerate(guide.investigation_steps, 1):
print(f"{i}. {step.description}")
print(f" Action: {step.action}")
print("\nRecommended Actions:")
for action in guide.recommended_actions:
print(f"[ ] {action}")