feat: 段落分割 split_paragraphs(通用空行分词)
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user