knowledge: temporal restrictions layer (phase 2, RESEARCH-derived-answers Part 2)
The extractor's temporal vocabulary is RESTRICTIONS, never Allen labels:
typed Fact gains start/end/point (optional, ISO-ish) + granularity +
orders [{rel: before|after|concurrent, other: <sibling index>}] — the
text's connectives transcribed, not classified. The Allen algebra stays
a COMPUTED VIEW over endpoints.
- checks.py: every stated date must be CONTAINED IN THE QUOTE — an
invented date rejects the whole candidate (LLM proposes, code
verifies against the source surface).
- store.py: edges gain t_start/t_end (valid-time window, None end =
OPEN, honest to 'since 2018'); fact_order table (order assertions
between addressable facts — reification is free); _classify consumes
interval arithmetic: overlapping open windows -> alternatives
(coexistence provable, no statistics), disjoint windows ->
succession (never a conflict). Boundary law at year granularity:
[2014,2018) vs [2018,None) = succession — the mandate changed hands
in 2018. add_fact_order validates rel in {before, after, concurrent}.
- expert.py: _ingest_candidate returns (merged, eid) and passes the
windows; the prose path collects positional fact_ids and wires the
orders' sibling references to real edge ids (malformed refs skip
honestly).
Tests: temporal.py 6/6 (typed fields, containment rejection, open-
window overlap -> alternatives, touch-at-year -> succession, empty
stays empty, fact_order ingest) + 7/7 interval cases incl. the
open-past degenerate. Suite: all green.
LIVE (run 20261025-205018, cold KG, glm-5.3-flash): 29 edges carry
windows (Spain 2018 / PSOE 2017 / SI 2022 all open-ended), 25
fact_order rows (Madrid before Congress of Deputies...), 8 invented
end-dates REJECTED by the containment check (the footballer Pedro
articles — the check catching exactly what it should), 0 conflicts.
The run itself answered UNKNOWN on its Q1 ("what DATE did he take
office") — honest: the blind extractor did not transcribe the exact
date this run and the noisy re-probe view (four Pedros) is the known
branch weakness, not a phase-2 regression. Design note for phase 4:
the three presidencies live under DIFFERENT rels, so the alternatives
substrate must be queried trans-rel by the consumer (alternatives_of
is per-rel by construction).
This commit is contained in:
@@ -1076,8 +1076,26 @@ class KnowledgeExpert:
|
||||
charge_llm()
|
||||
raw = self.extract(entities=ent_str, heading=chunk.heading_path,
|
||||
text=chunk.text)
|
||||
# phase 2: ingest with order assertions — fact_ids collects the
|
||||
# edge id per 0-based position in THIS extraction's facts list,
|
||||
# so 'orders' sibling references resolve to real edges
|
||||
fact_ids: list[int | None] = []
|
||||
for cand in (raw.facts or [])[: self.cfg.max_facts_per_chunk]:
|
||||
extracted += self._ingest_candidate(cand, page, containment=page.text)
|
||||
merged, eid = self._ingest_candidate(cand, page, containment=page.text)
|
||||
extracted += merged
|
||||
fact_ids.append(eid)
|
||||
# second pass: the order assertions of all facts of this chunk
|
||||
for cand, fid in zip((raw.facts or [])[: self.cfg.max_facts_per_chunk], fact_ids):
|
||||
if fid is None:
|
||||
continue
|
||||
for o in (getattr(cand, "orders", None) or []):
|
||||
other = o.get("other")
|
||||
rel = o.get("rel")
|
||||
if (other is None or rel not in ("before", "after", "concurrent")
|
||||
or not 0 <= other < len(fact_ids)
|
||||
or fact_ids[other] is None):
|
||||
continue # malformed reference: skip honestly
|
||||
self.store.add_fact_order(fid, fact_ids[other], rel)
|
||||
# ---- table fragments (extract path) -----------------------------
|
||||
from .store import stem as _stem
|
||||
# containment target: prose + ALL table rows (the fragment's rows
|
||||
@@ -1103,31 +1121,40 @@ class KnowledgeExpert:
|
||||
heading = f"== Tables == (table {frag['table_idx']}, part {frag['part']}/{frag['of']})"
|
||||
raw = self.extract(entities=ent_str, heading=heading, text=text)
|
||||
for cand in (raw.facts or [])[: self.cfg.max_facts_per_chunk]:
|
||||
extracted += self._ingest_candidate(cand, page, containment=containment)
|
||||
merged, _eid = self._ingest_candidate(cand, page, containment=containment)
|
||||
extracted += merged
|
||||
return self._merged
|
||||
|
||||
def _ingest_candidate(self, cand, page: WikiPage, containment: str) -> int:
|
||||
def _ingest_candidate(self, cand, page: WikiPage, containment: str) -> tuple[int, int | None]:
|
||||
"""Containment-check one typed Fact, merge it, cache its vector.
|
||||
Returns 1 when merged, 0 otherwise. Shared by the prose and table-
|
||||
fragment extract paths (research/semantic-kg)."""
|
||||
|
||||
Returns (merged, eid): merged=1 when the candidate was accepted
|
||||
(a duplicate counts — corroboration was recorded), eid=None when
|
||||
no NEW edge was created (duplicate or reject). Phase 2: the CALLER
|
||||
wires order assertions through the returned eid."""
|
||||
self._checked += 1
|
||||
cand = {"subject": cand.subject, "relation": cand.relation,
|
||||
"object": cand.object, "quote": cand.quote}
|
||||
"object": cand.object, "quote": cand.quote,
|
||||
"start": getattr(cand, "start", None),
|
||||
"end": getattr(cand, "end", None),
|
||||
"point": getattr(cand, "point", None)}
|
||||
ok, reason = check_candidate(cand, containment)
|
||||
if not ok:
|
||||
self._log(f"candidate rejected ({reason}): {cand.get('subject', '')[:40]}")
|
||||
return 0
|
||||
return 0, None
|
||||
doc_id = self.store.add_document(
|
||||
url=page.url, title=page.title, fetched_at=page.fetched_at,
|
||||
sha256=hashlib.sha256(page.text.encode()).hexdigest())
|
||||
qid = self.store.add_quote(doc_id, cand["quote"], page.url, page.fetched_at)
|
||||
eid = self.store.add_edge(cand["subject"], cand["relation"],
|
||||
cand["object"], qid)
|
||||
cand["object"], qid,
|
||||
t_start=cand["start"] or None,
|
||||
t_end=cand["end"] or None)
|
||||
self._merged += 1
|
||||
if eid is not None:
|
||||
self._cache_edge_vector(eid, cand["subject"], cand["relation"],
|
||||
cand["object"])
|
||||
return 1
|
||||
return 1, eid
|
||||
|
||||
def _cache_edge_vector(self, eid: int, subject: str, relation: str,
|
||||
obj: str) -> None:
|
||||
|
||||
Reference in New Issue
Block a user