diff --git a/tests/test_tsn_parse.py b/tests/test_tsn_parse.py index e791a27..266caf1 100644 --- a/tests/test_tsn_parse.py +++ b/tests/test_tsn_parse.py @@ -170,3 +170,16 @@ def test_index_value_must_be_decimal(): + + +def test_unit_state_belongs_to_its_table(): + """Единица «Измеритель» действует до подписи следующей таблицы (находка зайки): + таблица без строки «Измеритель» не наследует чужую единицу, её работы уйдут + в карантин «нет единицы», а не получат единицу соседа.""" + u = tsn_parse._track_unit + assert u("", "Измеритель: 100 м3") == "100 м3" + assert u("100 м3", "Таблица 1-2. Замена автомата") == "" + assert u("", "Таблица 1-1 (продолжение)") == "" + assert u("100 м3", "Таблица 1-1 (продолжение)") == "100 м3" + assert u("100 м3", "Прямые затраты") == "100 м3" + assert u("", "Измеритель:") == "" diff --git a/tsn_parse.py b/tsn_parse.py index 0b7777f..c6ccdb5 100644 --- a/tsn_parse.py +++ b/tsn_parse.py @@ -141,12 +141,16 @@ def _assign(values: list[tuple[float, float, str]], cols: list[tuple[str, float] return out -def _unit_of(lines: list[list[tuple[float, float, str]]]) -> str: - for line in lines: - text = " ".join(w for _, _, w in line) - if text.lower().startswith("измеритель"): - return text.split(":", 1)[-1].strip() or text - return "" +def _track_unit(current: str, text: str) -> str: + """Единица измерения своей таблицы. «Измеритель: …» задаёт её, подпись новой + таблицы сбрасывает: таблица без строки «Измеритель» не получает единицу соседней + (находка зайки 18.11.2026 — молчаливое наследование чужой единицы).""" + low = text.lower() + if CAPTION_RE.match(text) and "продолжение" not in low: + return "" + if low.startswith("измеритель"): + return text.split(":", 1)[-1].strip() + return current def parse_rate_book(path: str, meta: dict | None = None) -> dict: @@ -184,10 +188,8 @@ def parse_rate_book(path: str, meta: dict | None = None) -> dict: if found: current_table = found[-1] lines = _lines(page.get_text("words")) - unit = _unit_of(lines) - if unit: - current_unit = unit for line in lines: + current_unit = _track_unit(current_unit, " ".join(w for _, _, w in line)) cols = _columns(line) if cols: current_cols = cols