feat: Enhance CSV cleaning service with new endpoints for CSV analysis and preview functionality. Introduce support for job management linked to extraction batches, and update the cleaning rules structure. Add new utility functions for handling CSV data and improve documentation for clarity on usage and available endpoints.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Extracción de documento y autorización desde notas de texto."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any, Optional
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from .nullish import _is_nullish_scalar
|
||||
|
||||
|
||||
def _normalize_nota_text(raw: Any) -> str:
|
||||
if _is_nullish_scalar(raw):
|
||||
return ""
|
||||
try:
|
||||
if pd.isna(raw):
|
||||
return ""
|
||||
except TypeError:
|
||||
pass
|
||||
s = str(raw).strip()
|
||||
if not s or s.lower() in ("nan", "none", "nat", "<na>"):
|
||||
return ""
|
||||
return re.sub(r"\s+", " ", s)
|
||||
|
||||
|
||||
def extract_paciente_documento_nota(raw: Any) -> Optional[str]:
|
||||
"""Número de documento tras ``Paciente:`` y antes del guión del nombre."""
|
||||
t = _normalize_nota_text(raw)
|
||||
if not t:
|
||||
return None
|
||||
m = _PACIENTE_DOC_NOTA_RE.search(t)
|
||||
return m.group(1) if m else None
|
||||
|
||||
|
||||
def extract_autorizacion_nota(raw: Any) -> Optional[str]:
|
||||
"""Número tras ``Autorización No.`` (opcional por fila)."""
|
||||
t = _normalize_nota_text(raw)
|
||||
if not t:
|
||||
return None
|
||||
m = _AUTORIZACION_NOTA_RE.search(t)
|
||||
return m.group(1) if m else None
|
||||
|
||||
|
||||
def _alloc_nota_extract_column_names(
|
||||
col_name: str, existing: set[str]
|
||||
) -> tuple[str, str]:
|
||||
"""Nombres únicos para documento paciente y autorización."""
|
||||
base = re.sub(r"[^\w]+", "_", str(col_name).strip(), flags=re.ASCII) or "col"
|
||||
for i in range(0, 200):
|
||||
suf = f"_{i}" if i else ""
|
||||
dcol, acol = f"{base}{suf}_nota_paciente_doc", f"{base}{suf}_nota_autorizacion"
|
||||
if dcol not in existing and acol not in existing:
|
||||
return dcol, acol
|
||||
raise ValueError("No se pudieron asignar nombres de columnas únicos.")
|
||||
|
||||
|
||||
def _apply_nota_extract_columns(result: pd.DataFrame, col: str, log: list[str]) -> None:
|
||||
"""Añade columnas con documento (Paciente:) y número de autorización si existe."""
|
||||
dcol, acol = _alloc_nota_extract_column_names(str(col), set(result.columns))
|
||||
docs: list[Any] = []
|
||||
auths: list[Any] = []
|
||||
for v in result[col].tolist():
|
||||
dd = extract_paciente_documento_nota(v)
|
||||
aa = extract_autorizacion_nota(v)
|
||||
docs.append(dd if dd is not None else pd.NA)
|
||||
auths.append(aa if aa is not None else pd.NA)
|
||||
result[dcol] = docs
|
||||
result[acol] = auths
|
||||
log.append(
|
||||
f"[OK] '{str(col)}' -> columnas '{dcol}' y '{acol}' <- Extraer datos de notas"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user