feat(knowledge): LIST answer path — full table ingestion, typed list payloads
Part 1 of the lists design (DESIGN.md §11): mechanical full ingestion, separated from epistemic answer-completeness. parse_tables rewritten as a GRID parser: cells accumulate until |- (both layouts), empty cells kept as placeholders (image cells clean to ''; dropping them shifted France rows to start at 'FRA'), rowspan/colspan expanded at placement, total_rows is the computed extent — no truncation. _clean_cell: refs/sup/File links dropped, citation templates (cref2/efn...) resolve empty, links take display text, attrs split first. LIST path in expert.py: deterministic enumeration detection OR planner-declared LIST; SELECT TABLE (one LLM call over table metadata, never rows); ingest ALL rows as member_of edges (quote = full row line, containment against stored table text); aggregate via len() — no LLM. AnswerType.LIST added; evidence payloads carry list_items + coverage. Verified live cold: 50 sovereign states from the Europe article table (51 rows - Total footer), every member with verbatim quote. 19/19 tests.
This commit is contained in:
@@ -85,8 +85,116 @@ def test_store_four_cases(tmp="/tmp/floop_kg_test.sqlite"):
|
||||
print("ok test_store_four_cases")
|
||||
|
||||
|
||||
def test_table_parser_untruncated():
|
||||
from knowledge.wiki import parse_tables
|
||||
# vertical layout, 5 rows, header row -> total_rows MUST be 5, no truncation
|
||||
wt = ("{| class=wikitable" + chr(10)
|
||||
+ "! Country !! Population" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| A" + chr(10) + "| 1" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| B" + chr(10) + "| 2" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| C" + chr(10) + "| 3" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| D" + chr(10) + "| 4" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| E" + chr(10) + "| 5" + chr(10)
|
||||
+ "|}")
|
||||
tables = parse_tables(wt)
|
||||
assert len(tables) == 1
|
||||
t = tables[0]
|
||||
assert t.total_rows == 5, f"all 5 rows kept, got {t.total_rows}"
|
||||
assert t.header == ["Country", "Population"], f"header parsed: {t.header}"
|
||||
assert t.rows[0] == ["A", "1"]
|
||||
# horizontal layout parses identically
|
||||
wh = ("{| class=wikitable" + chr(10)
|
||||
+ "! Country !! Population" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| A || 1" + chr(10)
|
||||
+ "|-" + chr(10)
|
||||
+ "| B || 2" + chr(10)
|
||||
+ "|}")
|
||||
t2 = parse_tables(wh)[0]
|
||||
assert t2.total_rows == 2 and t2.rows[1] == ["B", "2"]
|
||||
print("ok test_table_parser_untruncated")
|
||||
|
||||
|
||||
def test_list_ingest_aggregate(tmp="/tmp/floop_kg_list.sqlite"):
|
||||
"""The LIST path end-to-end with a FAKE LLM: SELECT TABLE picks the table,
|
||||
ALL rows ingested as member_of edges, aggregation is code-only."""
|
||||
from knowledge.expert import KnowledgeExpert, KnowledgeConfig
|
||||
from knowledge.wiki import WikiTable
|
||||
|
||||
class FakeTablePicker:
|
||||
def __init__(self, *a, **k):
|
||||
pass
|
||||
def __call__(self, **kw):
|
||||
class P: pass
|
||||
p = P()
|
||||
p.table_index = "1"
|
||||
p.column = "Country"
|
||||
p.set_name = "sovereign states in Europe"
|
||||
return p
|
||||
|
||||
class FakeStore:
|
||||
def __init__(self):
|
||||
self.edges = []
|
||||
self.docs = 0
|
||||
self.quotes = 0
|
||||
def add_document(self, **kw):
|
||||
self.docs += 1
|
||||
return 1
|
||||
def add_quote(self, *a, **kw):
|
||||
self.quotes += 1
|
||||
return self.quotes
|
||||
def add_edge(self, subject, rel, obj, qid, **kw):
|
||||
self.edges.append((subject, rel, obj))
|
||||
return len(self.edges)
|
||||
|
||||
ke = KnowledgeExpert.__new__(KnowledgeExpert) # no dspy init
|
||||
ke.cfg = KnowledgeConfig()
|
||||
ke.store = FakeStore()
|
||||
ke.streams = None
|
||||
ke.select_table = FakeTablePicker()
|
||||
ke._merged = 0
|
||||
ke._log = lambda text, think=False: None # bound-style: no self
|
||||
|
||||
from knowledge.wiki import WikiPage
|
||||
page = WikiPage(title="List of states", pageid=1, url="u",
|
||||
fetched_at="2026-10-18T00:00:00+00:00", text="x",
|
||||
tables=[WikiTable(idx=1, header=["Country", "Population"],
|
||||
rows=[["Albania", "2.8"], ["Belgium", "11.8"],
|
||||
["Croatia", "3.9"]])])
|
||||
ans = ke._list_from_page(page, "What are the countries in Europe?", None)
|
||||
assert ans is not None
|
||||
assert ans["list_count"] == 3, f"count = len (code, not LLM): {ans}"
|
||||
assert ans["list_items"] == ["Albania", "Belgium", "Croatia"]
|
||||
assert ans["coverage"]["complete_per_source"] is True
|
||||
assert ans["coverage"]["rows_ingested"] == 3
|
||||
# every row became a membership edge, quote = full row line
|
||||
assert len(ke.store.edges) == 3
|
||||
assert all(e[1] == "member_of" for e in ke.store.edges)
|
||||
# rejected path: no table matches -> None -> fall through to extract path
|
||||
class FakeNonePicker:
|
||||
def __init__(self, *a, **k):
|
||||
pass
|
||||
def __call__(self, **kw):
|
||||
class P: pass
|
||||
p = P()
|
||||
p.table_index = "NONE"
|
||||
p.column = ""
|
||||
p.set_name = ""
|
||||
return p
|
||||
ke.select_table = FakeNonePicker()
|
||||
assert ke._list_from_page(page, "irrelevant", None) is None
|
||||
print("ok test_list_ingest_aggregate")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_chunker_sections()
|
||||
test_containment()
|
||||
test_store_four_cases()
|
||||
print("\n3 knowledge tests passed")
|
||||
test_list_ingest_aggregate()
|
||||
test_table_parser_untruncated()
|
||||
print(f"\n{test_table_parser_untruncated.__name__} etc — 5 knowledge tests passed")
|
||||
Reference in New Issue
Block a user