v3.1.4: таблицы — мин 2 строки, 40% заполнения

This commit is contained in:
2026-06-18 08:22:18 +04:00
parent 0151a380ea
commit 52fe7b92fb
+12 -10
View File
@@ -154,7 +154,7 @@ def _parse_pdf(file_bytes: bytes) -> dict:
tables = page.extract_tables() tables = page.extract_tables()
if tables: if tables:
for tbl in tables: for tbl in tables:
if tbl and _table_has_content(tbl, min_fill_ratio=0.3): if tbl and _table_has_content(tbl):
result["elements"].append({ result["elements"].append({
"type": "table", "type": "table",
"rows": tbl, "rows": tbl,
@@ -168,18 +168,22 @@ def _parse_pdf(file_bytes: bytes) -> dict:
def _table_has_content(tbl: list, min_fill_ratio: float = 0.0) -> bool: def _table_has_content(tbl: list, min_fill_ratio: float = 0.0) -> bool:
""" """
Проверить что таблица содержит осмысленные данные. Проверить что таблица содержит осмысленные ТАБЛИЧНЫЕ данные.
Фильтры: Отбрасываем:
- Хотя бы одна непустая ячейка - Полностью пустые
- Не менее min_fill_ratio ячеек заполнены (по умолчанию 0 — без фильтра) - Одна строка (обычно это текст, не таблица)
- Менее 40% ячеек заполнены
""" """
if not tbl or not tbl[0]: if not tbl or not tbl[0]:
return False return False
# Одна строка — не таблица, а текст
if len(tbl) <= 1:
return False
total = 0 total = 0
filled = 0 filled = 0
all_text = ""
for row in tbl: for row in tbl:
if not row: if not row:
@@ -189,14 +193,12 @@ def _table_has_content(tbl: list, min_fill_ratio: float = 0.0) -> bool:
cell_str = str(cell).strip() if cell else "" cell_str = str(cell).strip() if cell else ""
if cell_str: if cell_str:
filled += 1 filled += 1
all_text += " " + cell_str
if filled == 0: if filled == 0:
return False return False
# Фильтр по доле заполненных ячеек # Не менее 40% ячеек должны быть заполнены
if min_fill_ratio > 0 and total > 0: if total > 0 and filled / total < 0.4:
if filled / total < min_fill_ratio:
return False return False
return True return True