From 34ef28be4594f8d60e29ed5ff307e506c38d5c17 Mon Sep 17 00:00:00 2001 From: lhl Date: Sun, 9 Aug 2026 03:59:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=AE=B5=E8=90=BD=E5=88=86=E5=89=B2=20?= =?UTF-8?q?split=5Fparagraphs=EF=BC=88=E9=80=9A=E7=94=A8=E7=A9=BA=E8=A1=8C?= =?UTF-8?q?=E5=88=86=E8=AF=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/genesis/parsers/paragraph_splitter.py | 24 ++++++++++++++++++++++ tests/test_paragraph_splitter.py | 25 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/genesis/parsers/paragraph_splitter.py create mode 100644 tests/test_paragraph_splitter.py diff --git a/src/genesis/parsers/paragraph_splitter.py b/src/genesis/parsers/paragraph_splitter.py new file mode 100644 index 0000000..edf3174 --- /dev/null +++ b/src/genesis/parsers/paragraph_splitter.py @@ -0,0 +1,24 @@ +from __future__ import annotations + +from typing import Any + + +def _is_blank_row(row: list[Any]) -> bool: + return all(c is None or str(c).strip() == "" for c in row) + + +def split_paragraphs(matrix: list[list[Any]]) -> list[tuple[int, int]]: + """以全空行为界的通用段落分割;返回 (start_row, end_row)(含端,0-based)。""" + paragraphs: list[tuple[int, int]] = [] + start: int | None = None + for i, row in enumerate(matrix): + if not _is_blank_row(row): + if start is None: + start = i + else: + if start is not None: + paragraphs.append((start, i - 1)) + start = None + if start is not None: + paragraphs.append((start, len(matrix) - 1)) + return paragraphs \ No newline at end of file diff --git a/tests/test_paragraph_splitter.py b/tests/test_paragraph_splitter.py new file mode 100644 index 0000000..f27d307 --- /dev/null +++ b/tests/test_paragraph_splitter.py @@ -0,0 +1,25 @@ +from genesis.parsers.paragraph_splitter import split_paragraphs + + +def test_empty_matrix(): + assert split_paragraphs([]) == [] + + +def test_single_paragraph_no_empty_rows(): + m = [["a", "b"], ["c", "d"]] + assert split_paragraphs(m) == [(0, 1)] + + +def test_split_on_middle_empty_row(): + m = [["a"], [], ["b"], ["c"], []] + assert split_paragraphs(m) == [(0, 0), (2, 3)] + + +def test_trailing_empty_rows_no_extra_paragraph(): + m = [["a"], [], [], []] + assert split_paragraphs(m) == [(0, 0)] + + +def test_leading_empty_rows_start_at_first_nonempty(): + m = [[], ["a"], [], ["b"]] + assert split_paragraphs(m) == [(1, 1), (3, 3)] \ No newline at end of file