feat: Sheet 性质判定(table/free_text/mixed)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
|
||||
class SheetNature(Enum):
|
||||
TABLE = "table"
|
||||
FREE_TEXT = "free_text"
|
||||
MIXED = "mixed"
|
||||
|
||||
|
||||
def _non_empty(row: list[Any]) -> list[Any]:
|
||||
return [c for c in row if c is not None and str(c).strip() != ""]
|
||||
|
||||
|
||||
def find_header_row(matrix: list[list[Any]]) -> int:
|
||||
for i, row in enumerate(matrix):
|
||||
if len(_non_empty(row)) >= 2:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def _free_text_like(matrix: list[list[Any]]) -> bool:
|
||||
if not matrix:
|
||||
return True
|
||||
max_cols = max((len(row) for row in matrix), default=0)
|
||||
if max_cols <= 1:
|
||||
return True
|
||||
non_empty_rows = [r for r in matrix if _non_empty(r)]
|
||||
if len(non_empty_rows) / len(matrix) < 0.7:
|
||||
return True
|
||||
return find_header_row(matrix) == -1
|
||||
|
||||
|
||||
def classify_sheet(matrix: list[list[Any]]) -> SheetNature:
|
||||
if _free_text_like(matrix):
|
||||
return SheetNature.FREE_TEXT
|
||||
header_row = find_header_row(matrix)
|
||||
if header_row >= 0:
|
||||
for row in matrix[header_row + 1:]:
|
||||
if any(str(c).strip().startswith(("・", "■")) for c in _non_empty(row)):
|
||||
return SheetNature.MIXED
|
||||
return SheetNature.TABLE
|
||||
Reference in New Issue
Block a user