Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions docling/backend/xml/uspto_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,14 @@ def __init__(self, input: str) -> None:
self.empty_text = ""
self._soup = BeautifulSoup(input, features="xml")

@staticmethod
def _parse_colwidth(colwidth: str | None) -> float:
"""Parse CALS colwidth magnitude from fixed ("36pt"), proportional ("2*"), or absent values."""
if not colwidth:
return 1.0
match = re.match(r"\s*([0-9]*\.?[0-9]+)", colwidth)
return float(match.group(1)) if match else 1.0

def _create_tg_range(self, tgs: list[ColInfo]) -> dict[int, ColInfoType]:
"""Create a unified range along the table groups.

Expand All @@ -1548,13 +1556,7 @@ def _create_tg_range(self, tgs: list[ColInfo]) -> dict[int, ColInfoType]:
}
offst = 0
for info in tg["colinfo"]:
cw = info["colwidth"]
cw = re.sub("pt", "", cw, flags=re.I)
cw = re.sub("mm", "", cw, flags=re.I)
try:
cw = int(cw)
except BaseException:
cw = float(cw)
cw = self._parse_colwidth(info["colwidth"])
colinfo[itg]["colwidth"].append(cw)
colinfo[itg]["offset"].append(offst)
offst += cw
Expand Down
23 changes: 23 additions & 0 deletions tests/test_backend_patent_uspto.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ def test_tables(tables):
assert len(file_table.table_cells) == 130


@pytest.mark.parametrize(
"colspecs",
[
'<colspec colname="1" colwidth="1*"/><colspec colname="2" colwidth="2*"/>',
'<colspec colname="1"/><colspec colname="2"/>',
'<colspec colname="1" colwidth="50pt"/><colspec colname="2" colwidth="60pt"/>',
],
)
def test_table_colwidth_variants(colspecs):
"""CALS colwidth may be proportional ("1*") or absent; both must parse, not drop the table."""
xml = (
f'<table><tgroup cols="2">{colspecs}'
"<tbody><row><entry>a</entry><entry>b</entry></row></tbody>"
"</tgroup></table>"
)
table = XmlTable(xml).parse()
assert table is not None
assert table.num_rows == 1
assert table.num_cols == 2
assert [cell.text for cell in table.table_cells] == ["a", "b"]
assert [cell.start_col_offset_idx for cell in table.table_cells] == [0, 1]


def test_table_out_of_range_namest_does_not_crash():
"""An entry whose numeric namest points past the declared columns must degrade, not crash."""
xml = (
Expand Down