Improve patient list OCR cleanup

This commit is contained in:
2026-06-11 20:31:11 +08:00
parent fde8e11cd1
commit 939c34be19
3 changed files with 155 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ import argparse
import csv
import json
import os
import re
import subprocess
import tempfile
from pathlib import Path
@@ -22,6 +23,7 @@ DEFAULT_RESULT_JSON = Path("数据处理结果区/合并_患者列表_结构化.
DEFAULT_SCHEMA_SQL = Path("数据处理工作区/06_PostgreSQL建表结构.sql")
TABLE_NAME = '"Patient_Lists"'
DATE_TIME_RE = re.compile(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$")
PATIENT_FIELDS = [
"batch_name",
@@ -75,6 +77,16 @@ def review_note_items(review: dict[str, Any]) -> list[str]:
return note_items
def admission_after_discharge(admission_time: Any, discharge_time: Any) -> bool:
admission = scalar(admission_time).strip()
discharge = scalar(discharge_time).strip()
if not admission or not discharge:
return False
if not DATE_TIME_RE.match(admission) or not DATE_TIME_RE.match(discharge):
return False
return admission > discharge
def psql_quote(path: Path) -> str:
return "'" + str(path.resolve()).replace("'", "''") + "'"
@@ -87,6 +99,17 @@ def flatten_patient_record(record: dict[str, Any]) -> dict[str, Any]:
hospital_days, hospital_days_note = clean_integer(patient["住院天数"])
if hospital_days_note:
note_items.append(hospital_days_note)
review_status = review["状态"]
patient_name = scalar(patient["姓名"]).strip()
if not patient_name:
patient_name = "缺少姓名"
review_status = "需人工复核"
if "缺少姓名" not in note_items:
note_items.append("缺少姓名")
if admission_after_discharge(patient["入院时间"], patient["出院时间"]):
review_status = "需人工复核"
if "入院时间晚于出院时间" not in note_items:
note_items.append("入院时间晚于出院时间")
review_notes = "".join(note_items)
return {
"batch_name": record["处理批次"],
@@ -96,7 +119,7 @@ def flatten_patient_record(record: dict[str, Any]) -> dict[str, Any]:
"image_path": image["图片路径"],
"image_name": image["图片名"],
"image_row_no": image["图片内行号"],
"patient_name": patient["姓名"],
"patient_name": patient_name,
"gender": patient["性别"],
"age": patient["年龄"],
"inpatient_no": patient["住院号"],
@@ -106,7 +129,7 @@ def flatten_patient_record(record: dict[str, Any]) -> dict[str, Any]:
"hospital_days": hospital_days,
"discharge_time": patient["出院时间"],
"postoperative_days": patient["手术后天数"],
"review_status": review["状态"],
"review_status": review_status,
"review_notes": review_notes,
"manual_corrected": bool(review.get("人工修正")),
}