From 016a5ea9a5f4987d39a5e7eca832b2813aee924a Mon Sep 17 00:00:00 2001 From: anor Date: Wed, 18 Nov 2026 04:42:02 +1000 Subject: [PATCH] =?UTF-8?q?fix(=D0=A2=D0=A1=D0=9D):=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B1=D0=B0=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D1=83=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BB=D0=B8=D1=87=D0=B0=D0=B5=D0=BC=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BA=D1=83=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B0,=20=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=BE=20=D1=81=D0=BB=D0=BE=D0=B2=D1=83=20=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B4=D0=B5=D1=80=D0=B6=D0=B0=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Признак раздела включался со страницы содержания и перекрашивал весь справочник: 38 строк перебазировки и одна честно битая строка (в PDF «227,6S»). Теперь раздел узнаётся по заголовку с шапкой «№№ позиций»/«Сметная стоимость». Co-Authored-By: Claude Code --- tests/test_tsn_price_book.py | 8 +++++++- tsn_parse.py | 28 ++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/tests/test_tsn_price_book.py b/tests/test_tsn_price_book.py index 7370ce3..cf36d94 100644 --- a/tests/test_tsn_price_book.py +++ b/tests/test_tsn_price_book.py @@ -153,6 +153,12 @@ def test_name_keeps_numbers(tmp_path): def test_relocation_section_rows_get_honest_reason(): """Таблица «Стоимость перебазировки» — это не цена за маш.-ч: строка в карантин с причиной, объясняющей отказ, а не «цена не найдена».""" - assert tsn_parse._price_missing_reason("Раздел 2. Стоимость перебазировки машин") \ + heading = "Раздел 2. Стоимость перебазировки машин. №№ позиций" + assert tsn_parse._price_missing_reason(heading) \ == "раздел перебазировки: это стоимость перебазировки, не цена за маш.-ч" + # упоминание в содержании — не раздел + assert tsn_parse._price_missing_reason( + "Раздел 2. Стоимость перебазировки машин ........ 30") == "цена не найдена" assert tsn_parse._price_missing_reason("Сметные цены эксплуатации машин") == "цена не найдена" + # продолжение раздела на следующей странице без заголовка + assert "перебазиров" in tsn_parse._price_missing_reason("", in_relocation=True) diff --git a/tsn_parse.py b/tsn_parse.py index 50902cf..da00bd3 100644 --- a/tsn_parse.py +++ b/tsn_parse.py @@ -456,8 +456,11 @@ def parse_price_book(path: str, meta: dict | None = None) -> dict: with fitz.open(path) as doc: pages_total = doc.page_count pending: dict | None = None # строка, у которой цена ещё не встретилась + relocation = False for pno in range(pages_total): page_text = doc[pno].get_text("text") + if _is_relocation_section(page_text): + relocation = True for line in _lines(doc[pno].get_text("words")): if not line: continue @@ -467,7 +470,7 @@ def parse_price_book(path: str, meta: dict | None = None) -> dict: and len(line[1][2]) == code_len and line[1][2].isdigit()): if pending: quarantine.append(_pb_quarantine( - pending, _price_missing_reason(page_text))) + pending, _price_missing_reason(page_text, relocation))) rows_seen += 1 pending = {"pos": first[2], "code": line[1][2], "page": pno + 1, "name": _words_in(line, PB_NAME_MIN, name_max), @@ -491,7 +494,7 @@ def parse_price_book(path: str, meta: dict | None = None) -> dict: (quarantine.append(why) if item is None else items.append(item)) pending = None if pending: - quarantine.append(_pb_quarantine(pending, _price_missing_reason(page_text))) + quarantine.append(_pb_quarantine(pending, _price_missing_reason(page_text, relocation))) stats = { "pages_total": pages_total, "rows_seen": rows_seen, @@ -503,11 +506,24 @@ def parse_price_book(path: str, meta: dict | None = None) -> dict: "book": book, "file": meta.get("file")} -def _price_missing_reason(page_text: str) -> str: - """Почему у строки нет цены. В справочнике машин есть раздел «Стоимость +def _is_relocation_section(page_text: str) -> bool: + """Заголовок раздела перебазировки (а не упоминание в содержании): после него + на странице идёт шапка таблицы с «№№ позиций».""" + for m in re.finditer(r"раздел\s*\d+\.?\s*стоимость\s+перебазиров", + page_text or "", re.IGNORECASE): + tail = (page_text or "")[m.start():m.start() + 400] + if "№№" in tail or "Сметная стоимость" in tail: + return True + return False + + +def _price_missing_reason(page_text: str, in_relocation: bool = False) -> str: + """Почему у строки нет цены. В справочнике машин есть разделы «Стоимость перебазировки»: там рубли за перевозку машины, а не цена машино-часа, — - подставлять их в цену ресурса нельзя, и причина должна это говорить.""" - if "перебазиров" in (page_text or "").lower(): + подставлять их в цену ресурса нельзя, и причина должна это говорить. + Раздел перебазировки идёт в конце книги и продолжается на страницах без + повторного заголовка, поэтому признак переносится со страницы на страницу.""" + if in_relocation or _is_relocation_section(page_text): return "раздел перебазировки: это стоимость перебазировки, не цена за маш.-ч" return "цена не найдена"