"""Rebuilding a table from where its text sits. The model answers with regions -- this is everything that happens afterwards: grouping boxes into rows and columns, working out which cells span, deciding which rows are headers, and emitting the grid as HTML or as prose. All of it is arithmetic over dictionaries, so none of it needs the weights, and until now none of it was reached without them. It is ported code (RAGFlow's ``table_structure_recognizer.py``), which is the kind where a wrong comparison does not raise: the table still renders, with a column folded into its neighbour or a header row read as data. """ from __future__ import annotations import pytest from raven.knowledge.parser.deepdoc._tsr import TableStructureRecognizer as TSR def box(text: str, *, row: int, col: int, page: int = 1, **extra: object) -> dict: """One cell, placed on a regular grid. The geometry is what the grouping reads: 100 wide and 20 tall per cell, so neighbouring columns never overlap and consecutive rows sit clear of the 3pt slack the row break allows for. """ cell = { "text": text, "x0": col * 100.0, "x1": col * 100.0 + 90.0, "top": row * 20.0, "bottom": row * 20.0 + 15.0, "page_number": page, } cell.update(extra) # type: ignore[arg-type] return cell def grid(rows: list[list[str]], **extra: object) -> list[dict]: return [box(text, row=r, col=c, **extra) for r, line in enumerate(rows) for c, text in enumerate(line)] class TestIsCaption: """What counts as a caption, and so is lifted out of the grid entirely.""" @pytest.mark.parametrize( "text", [ "Table 1: results", "table 2 results", "Figure 3 the layout", "Fig. 4 the layout", "Fig 5 the layout", "\u8868 1\uff1a\u7ed3\u679c", "\u56fe 2: \u5e03\u5c40", ], ) def test_the_published_caption_spellings(self, text: str) -> None: assert TSR.is_caption({"text": text}) @pytest.mark.parametrize("text", ["Tables are useful", "1 Table", "figure it out", "revenue", ""]) def test_prose_that_merely_mentions_one_is_not_a_caption(self, text: str) -> None: """`Table 1` is a caption and `Tables are useful` is a sentence. The patterns anchor at the start for that reason.""" assert not TSR.is_caption({"text": text}) def test_the_layout_model_can_say_so_instead(self) -> None: """A caption the patterns do not match is still a caption when the layout pass labelled it one.""" assert TSR.is_caption({"text": "anything at all", "layout_type": "table caption"}) def test_surrounding_space_does_not_hide_one(self) -> None: assert TSR.is_caption({"text": " Table 6: results "}) class TestBlockType: def test_a_number_is_called_a_number(self) -> None: assert TSR.blockType({"text": " 1,234.5 "}) == "Nu" def test_a_word_is_not(self) -> None: assert TSR.blockType({"text": "revenue"}) != "Nu" class TestConstructTable: """The grid, rebuilt. Driven end to end because the span calculation and both renderers are private and only ever reached through here.""" def test_an_empty_input_answers_with_nothing(self) -> None: assert TSR.construct_table([]) == [] def test_a_table_that_is_only_a_caption_answers_with_nothing(self) -> None: """The caption is lifted out first, which can empty the input. Reaching the grouping below with no boxes would index boxes[0].""" assert TSR.construct_table([box("Table 1: results", row=0, col=0)]) == [] def test_rows_and_columns_come_back_as_a_grid(self) -> None: html = TSR.construct_table(grid([["a", "b"], ["c", "d"]])) assert html.startswith("