claims.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Claim extraction helpers for Atlas layered outputs."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from app.models import AtlasClaim, AtlasEntity, AtlasProvenance
  5. def _prov_to_dict(p: AtlasProvenance | None) -> dict[str, Any] | None:
  6. if p is None:
  7. return None
  8. return {
  9. "source": p.source,
  10. "method": p.retrieval_method,
  11. "confidence": p.confidence,
  12. "retrieved_at": p.retrieved_at,
  13. "evidence_property": p.evidence_property,
  14. "provider": p.provider,
  15. "model": p.model,
  16. }
  17. def _id_type_resource(identifier_type: str) -> str:
  18. if identifier_type == "mid":
  19. return "atlas:Mid"
  20. if identifier_type == "qid":
  21. return "atlas:WikidataQID"
  22. return f"atlas:{identifier_type}"
  23. def _claim_to_dict(claim: AtlasClaim) -> dict[str, Any]:
  24. obj: dict[str, Any] = {
  25. "kind": claim.object.kind,
  26. "value": claim.object.value,
  27. }
  28. if claim.object.id_type:
  29. obj["id_type"] = _id_type_resource(claim.object.id_type)
  30. return {
  31. "claim_id": claim.claim_id,
  32. "layer": claim.layer,
  33. "status": claim.status,
  34. "subject": claim.subject,
  35. "predicate": claim.predicate,
  36. "object": obj,
  37. "provenance": _prov_to_dict(claim.provenance),
  38. }
  39. def build_claim_sets(entity: AtlasEntity) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
  40. raw_claims = [_claim_to_dict(c) for c in entity.claims if c.layer == "raw"]
  41. derived_claims = [_claim_to_dict(c) for c in entity.claims if c.layer == "derived"]
  42. return raw_claims, derived_claims