claims.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  15. def _id_type_resource(identifier_type: str) -> str:
  16. if identifier_type == "mid":
  17. return "atlas:Mid"
  18. if identifier_type == "qid":
  19. return "atlas:WikidataQID"
  20. return f"atlas:{identifier_type}"
  21. def _claim_to_dict(claim: AtlasClaim) -> dict[str, Any]:
  22. obj: dict[str, Any] = {
  23. "kind": claim.object.kind,
  24. "value": claim.object.value,
  25. }
  26. if claim.object.id_type:
  27. obj["id_type"] = _id_type_resource(claim.object.id_type)
  28. return {
  29. "claim_id": claim.claim_id,
  30. "layer": claim.layer,
  31. "status": claim.status,
  32. "subject": claim.subject,
  33. "predicate": claim.predicate,
  34. "object": obj,
  35. "provenance": _prov_to_dict(claim.provenance),
  36. }
  37. def build_claim_sets(entity: AtlasEntity) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
  38. raw_claims = [_claim_to_dict(c) for c in entity.claims if c.layer == "raw"]
  39. derived_claims = [_claim_to_dict(c) for c in entity.claims if c.layer == "derived"]
  40. return raw_claims, derived_claims