Coverage for src\genesis\parsers\free_text_extractor.py: 100%
22 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-26 14:20 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-26 14:20 +0800
1from __future__ import annotations
3from typing import Any
5from genesis.data_models import CellValue, ExcelTable, ExtractionMethod, Provenance, SheetType
8def extract_text_blocks(matrix: list[list[Any]]) -> list[str]:
9 """按全空行分段;行内非空单元格以「 」连接。"""
10 blocks: list[str] = []
11 current: list[str] = []
12 for row in matrix:
13 cells = [str(c).strip() for c in row if c is not None and str(c).strip() != ""]
14 if not cells:
15 if current:
16 blocks.append(" ".join(current))
17 current = []
18 continue
19 current.append(" ".join(cells))
20 if current:
21 blocks.append(" ".join(current))
22 return blocks
25def build_free_text_table(
26 sheet_name: str,
27 blocks: list[str],
28 file_name: str,
29 detected_type: SheetType = SheetType.GENERIC,
30) -> ExcelTable:
31 rows = []
32 for i, text in enumerate(blocks, start=1):
33 rows.append({
34 "text": CellValue(
35 value=text,
36 provenance=Provenance(
37 file_name=file_name,
38 sheet_name=sheet_name,
39 row=i,
40 column="A",
41 column_header="text",
42 ),
43 ),
44 })
45 return ExcelTable(
46 name=sheet_name,
47 detected_type=detected_type,
48 extraction_method=ExtractionMethod.LLM_FROM_FREE_TEXT.value,
49 headers=["text"],
50 rows=rows,
51 )