knowledge: PR-1 skim surfaces — read articles in passing (KNOWLEDGE §7)

The Italy run (Run G) failed with the data in hand: Q1's LIST ingest
early-exited on the length table and discarded the same article's 27-row
discharge table; the Adige/Tiber discharges live in infobox templates,
invisible to both the plain extract and parse_tables; and {{cvt|50|m3/s}}
cleaned to "50" — units silently dropped everywhere.

PR-1 builds the skim's surfaces and signals (deterministic, zero LLM):

- knowledge/skim.py: skim_article() builds a per-article SkimSheet —
  section headings with term hits, table identity (caption/headers/row
  count), infobox parameter keys with resolved quantity values, link
  candidates from goal-relevant regions — from material fetch_rich
  already downloads.
- quantity-unit patterns (find_quantities): "50 m3/s" is a
  vocabulary-independent signal; the Italy discharge section's heading
  says "above 50 m3/s", never "discharge" — this finds what keyword
  search cannot.
- wiki.resolve_convert: {{convert|N|unit}} resolves to "N unit" inside
  _clean_cell BEFORE the generic template pass (which kept only the first
  argument and dropped the unit). Captions and cells keep units.
- WikiPage.wikitext: fetch_rich stores the raw source (infobox surface).
- _process_page: gated chunks are RANKED by skim signals (question + goal
  terms + quantity patterns) instead of first-N in document order.

Live smoke (real Wikipedia, no LLM): the Italy sheet shows table 2
"Rivers of Italy above 50 m3/s of average flow at the mouth", 27 rows,
quantities 50/350/239/235/187/170 m3/s; the Adige sheet shows infobox
discharge1_avg = 235 m3/s.

Tests: 6 new (knowledge/tests/skim.py), suite 35/35 green.
PR-2 (opportunistic harvest + seen-index) and PR-3 (notices to the
machine) follow.
This commit is contained in:
2026-10-20 14:52:38 +02:00
parent b85f196cbe
commit 73fe5cc8e1
4 changed files with 395 additions and 9 deletions
+32 -2
View File
@@ -157,12 +157,37 @@ def _tpl_arg(content: str) -> str:
return ""
CONVERT_RE = re.compile(r"\{\{(?:convert|cvt)\|([^{}]+)\}\}", re.I)
_UNIT_CANON = {"cuft/s": "cu ft/s", "ft3/s": "cu ft/s", "m³/s": "m3/s",
"km²": "km2", "mi²": "mi2"}
def resolve_convert(text: str) -> str:
"""Resolve {{convert|N|unit|...}} / {{cvt|N|unit|...}} to 'N unit'.
The primary unit is the fact; display-side secondary units are dropped
(PR-1 unit-loss fix: a caption saying 'above {{cvt|50|m3/s}}' cleaned
to 'above 50' — the unit is data, quantities carry values AND units)."""
def _one(m: re.Match) -> str:
parts = [p.strip() for p in m.group(1).split("|")]
if len(parts) < 2 or not parts[0]:
return m.group(0)
num = parts[0].rstrip(".,")
unit = parts[1].replace("\u00a0", " ").strip()
unit = unit.lower()
unit = _UNIT_CANON.get(unit, unit)
return f"{num} {unit}"
return CONVERT_RE.sub(_one, text or "")
def _clean_cell(cell: str) -> str:
"""Resolve links/templates to display text; drop refs, File links, tags.
Order matters: <br> variants become ' / ' separators FIRST (multi-value
name cells like 'Albania<br>Republic of Albania<br>ALB' must not
concatenate), then refs, File links, links, templates."""
concatenate), then refs, File links, links, templates. convert/cvt
templates resolve to 'N unit' BEFORE the generic template pass — the
generic pass keeps only the first argument and would drop the unit."""
cell = re.sub(r"<br\s*/?>", " / ", cell, flags=re.I)
cell = re.sub(r"<ref[^>]*>.*?</ref>", "", cell, flags=re.S | re.I)
cell = re.sub(r"<ref[^>]*/?>", "", cell, flags=re.I)
@@ -170,6 +195,7 @@ def _clean_cell(cell: str) -> str:
cell = re.sub(r"<sup[^>]*/?>", "", cell, flags=re.I)
cell = re.sub(r"\[\[(?:File|Image):.*?\]\]", "", cell, flags=re.I | re.S)
cell = re.sub(r"\[\[(?:[^|\]]*\|)*([^\]]+)\]\]", r"\1", cell)
cell = resolve_convert(cell)
while "{{" in cell:
new = re.sub(r"\{\{([^{}]*)\}\}", lambda mm: _tpl_arg(mm.group(1)), cell)
if new == cell:
@@ -206,6 +232,7 @@ class WikiPage:
fetched_at: str # ISO timestamp
rev_id: int | None = None
tables: list[WikiTable] = field(default_factory=list)
wikitext: str = "" # raw source (infobox params live here; PR-1)
@property
def sections(self) -> list[str]:
@@ -299,10 +326,13 @@ class WikipediaClient:
"""Extract + typed tables: wikitext tables are parsed UNTRUNCATED into
WikiTable objects (total_rows is a computed fact). Prose enrichment
(== Tables == section) kept for the chunk path; the LIST path reads
page.tables directly."""
page.tables directly. The raw wikitext stays on the page (skim's
infobox surface) and the '== Tables ==' prose block keeps units:
{{convert|N|unit}} resolves to 'N unit' (PR-1 unit-loss fix)."""
page = self.fetch(title)
wikitext = self.fetch_wikitext(title)
page.tables = parse_tables(wikitext)
page.wikitext = wikitext
extra = tables_as_text(wikitext)
if extra:
page.text = page.text + "\n\n== Tables ==\n" + extra