File size: 8,848 Bytes
3e363e2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | """SEC Company Facts loader for structured financial observations."""
from __future__ import annotations
import json
import re
from collections.abc import Mapping
from pathlib import Path
from graph.graph import EdgeRecord, GraphStore
from graph.real.corpus import RealGraph, SourceMetadata, relation_phrase
def _slug(value: str, *, limit: int = 120) -> str:
return re.sub(r"[^A-Za-z0-9]+", "_", value.strip()).strip("_")[:limit] or "unknown"
def load_sec_companyfacts(
path: str | Path,
source: SourceMetadata,
*,
max_observations: int | None = None,
) -> RealGraph:
"""Load one JSON object per company from the SEC Company Facts API."""
nodes: dict[str, str] = {}
labels: dict[str, str] = {}
attributes: dict[str, dict[str, str]] = {}
edges: list[EdgeRecord] = []
observations = 0
with Path(path).open(encoding="utf-8") as handle:
for line in handle:
if not line.strip():
continue
payload = json.loads(line)
cik = str(payload.get("cik") or payload.get("entity", {}).get("cik", ""))
entity_name = str(
payload.get("entityName")
or payload.get("entity", {}).get("name", "")
or cik
)
if not cik:
continue
company_id = f"sec:company:{cik.zfill(10)}"
nodes[company_id] = "company"
labels[company_id] = entity_name
facts = payload.get("facts", {})
if not isinstance(facts, Mapping):
continue
for taxonomy, concepts in facts.items():
if not isinstance(concepts, Mapping):
continue
for concept, definition in concepts.items():
concept_id = f"sec:concept:{taxonomy}:{concept}"
nodes[concept_id] = "financial_concept"
labels[concept_id] = str(
definition.get("label", concept)
if isinstance(definition, Mapping)
else concept
)
units = (
definition.get("units", {})
if isinstance(definition, Mapping)
else {}
)
if not isinstance(units, Mapping):
continue
for unit, rows in units.items():
if not isinstance(rows, list):
continue
for row in rows:
if not isinstance(row, Mapping):
continue
observation_key = ":".join(
str(row.get(field, ""))
for field in (
"fy",
"fp",
"form",
"filed",
"end",
"start",
"val",
)
)
observation_id = (
f"sec:observation:{cik}:{taxonomy}:{concept}:"
f"{_slug(unit)}:{_slug(observation_key)}"
)
nodes[observation_id] = "financial_observation"
attributes[observation_id] = {
"value": str(row.get("val", "")),
"unit": str(unit),
"period_start": str(row.get("start", "")),
"period_end": str(row.get("end", "")),
"fiscal_year": str(row.get("fy", "")),
"form": str(row.get("form", "")),
"filed": str(row.get("filed", "")),
"evidence_kind": "regulatory_filing_observation",
}
edges.extend(
(
EdgeRecord(
company_id,
"reports_fact",
observation_id,
"company",
"financial_observation",
relation_phrase("reports_fact"),
{
"evidence_kind": "regulatory_filing_observation"
},
),
EdgeRecord(
observation_id,
"instance_of",
concept_id,
"financial_observation",
"financial_concept",
relation_phrase("instance_of"),
{
"evidence_kind": "regulatory_filing_observation"
},
),
)
)
period = str(row.get("end", ""))
if period:
period_id = f"sec:period:{period}"
nodes[period_id] = "reporting_period"
labels[period_id] = period
edges.append(
EdgeRecord(
observation_id,
"in_period",
period_id,
"financial_observation",
"reporting_period",
relation_phrase("in_period"),
{
"evidence_kind": "regulatory_filing_observation"
},
)
)
observations += 1
if (
max_observations is not None
and observations >= max_observations
):
return RealGraph(
graph=GraphStore(
nodes,
edges,
graph_version=f"{source.source_id}@{source.version}",
node_attributes=attributes,
relation_attributes={
relation: {"functional": "false"}
for relation in (
"reports_fact",
"instance_of",
"in_period",
)
},
),
node_labels=labels,
relation_phrases={
relation: relation_phrase(relation)
for relation in (
"reports_fact",
"instance_of",
"in_period",
)
},
source=source,
)
return RealGraph(
graph=GraphStore(
nodes,
edges,
graph_version=f"{source.source_id}@{source.version}",
node_attributes=attributes,
relation_attributes={
relation: {"functional": "false"}
for relation in ("reports_fact", "instance_of", "in_period")
},
),
node_labels=labels,
relation_phrases={
relation: relation_phrase(relation)
for relation in ("reports_fact", "instance_of", "in_period")
},
source=source,
)
|