Files
umed/web/scripts/build-hospitals-zh-locale.py
2026-07-09 14:35:32 +08:00

183 lines
6.7 KiB
Python

"""从 Excel 原始数据生成医院/专家中文 locale 文件(与 hospitals-content.ts 的 id 对齐)。"""
from __future__ import annotations
import json
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
WEB = ROOT / "web"
EXCEL_JSON = WEB / "scripts" / "hospitals-excel-raw.json"
OUT_NAMES = WEB / "data" / "locale" / "zh" / "hospitals-names.zh.ts"
OUT_INTROS = WEB / "data" / "locale" / "zh" / "hospitals-intros.zh.ts"
OUT_DOCTORS = WEB / "data" / "locale" / "zh" / "doctors.zh.ts"
# 与 build-hospitals-data.py 保持一致:照片目录 id -> Excel 医院中文名
FOLDER_TO_HOSPITAL: dict[str, str] = {
"中山一院图": "中山大学附属第一医院(越秀院区、南沙院区)",
"广东省人民医院": "广东省人民医院",
"广东省妇幼保健院": "广东省妇幼保健院",
"孙逸仙纪念医院": "中山大学孙逸仙纪念医院",
"中山三院": "中山大学附属第三医院",
"中山大学中山眼科中心": "中山大学中山眼科中心",
"中山肿瘤": "中山大学肿瘤防治中心",
"中大口腔": "中山大学附属口腔医院",
"中山六院": "中山大学附属第六医院",
"南方医院": "南方医科大学南方医院",
"珠江医院": "南方医科大学珠江医院",
"暨大附一院": "暨南大学附属第一医院",
"广东省中医院": "广东省中医院",
"汕大附一院": "汕头大学医学院第一附属医院",
"广医附一院": "广州医科大学附属第一医院",
"广州市一院": "广州市第一人民医院",
"广妇儿中心": "广州医科大学附属妇女儿童医疗中心",
"深圳人医": "深圳市人民医院",
"北大深圳": "北京大学深圳医院",
"港大深圳": "香港大学深圳医院",
"南方深圳": "南方医科大学深圳医院",
"中山5院": "中山大学附属第五医院",
"珠海人民医院": "珠海市人民医院",
"祈福医院": "广东祈福医院",
"佛山复星": "佛山复星禅诚医院",
"湘雅二院": "中南大学湘雅二医院",
}
HOSPITAL_ZH_TO_ID = {v: k for k, v in FOLDER_TO_HOSPITAL.items()}
def extract_doctor_name(expert: str) -> str:
expert = expert.strip()
if not expert:
return expert
m = re.search(r"([\u4e00-\u9fff]{2,4})$", expert)
if m:
return m.group(1)
parts = expert.split()
return parts[-1] if parts else expert
def parse_excel() -> list[dict]:
with open(EXCEL_JSON, encoding="utf-8") as fh:
rows = json.load(fh)
hospitals: list[dict] = []
current: dict | None = None
for row in rows[1:]:
h, dept, expert, _hospital_en, _idx, expert_info, intro, url, cert = (row + [None] * 9)[:9]
if h:
if current:
hospitals.append(current)
current = {
"name": str(h).strip(),
"intro": (intro or "").strip() if intro else "",
"url": (url or "").strip() if url else "",
"cert": (cert or "").strip() if cert else "",
"doctors": [],
}
if current and expert:
expert_s = str(expert).strip()
dept_s = str(dept).strip() if dept else ""
current["doctors"].append(
{
"department": dept_s,
"expert": expert_s,
"name": extract_doctor_name(expert_s),
"expertInfo": str(expert_info).strip() if expert_info else None,
}
)
if current:
hospitals.append(current)
return hospitals
def format_intro_zh(intro: str, cert: str) -> str:
parts = [p for p in (intro, cert) if p]
return "\n\n".join(parts)
def ts_string(value: str) -> str:
return json.dumps(value, ensure_ascii=False)
def write_names(hospital_names: dict[str, str]) -> None:
lines = [
"/** 医院中文名称 — 来源:医院-科室-专家 Excel */",
"",
"export const hospitalNameZh: Record<string, string> = {",
]
for hid in sorted(hospital_names):
lines.append(f" {json.dumps(hid, ensure_ascii=False)}: {ts_string(hospital_names[hid])},")
lines.append("}")
lines.append("")
OUT_NAMES.write_text("\n".join(lines), encoding="utf-8")
def write_intros(hospital_intros: dict[str, str]) -> None:
lines = [
"/** 医院简介中文 — 来源:医院-科室-专家 Excel(含认证情况) */",
"",
"export const hospitalIntroZh: Record<string, string> = {",
]
for hid in sorted(hospital_intros):
lines.append(f" {json.dumps(hid, ensure_ascii=False)}: {ts_string(hospital_intros[hid])},")
lines.append("}")
lines.append("")
OUT_INTROS.write_text("\n".join(lines), encoding="utf-8")
def write_doctors(doctor_zh: dict[str, dict]) -> None:
lines = [
'import type { DoctorRecord } from \'~/data/hospitals-content\'',
"",
"export type DoctorZhFields = Pick<DoctorRecord, 'name' | 'department' | 'hospitalName' | 'expertLabel' | 'expertInfo'>",
"",
"/** 专家中文 — 来源:医院-科室-专家 Excel */",
"export const doctorZh: Record<string, DoctorZhFields> = " + json.dumps(doctor_zh, ensure_ascii=False, indent=2),
"",
]
OUT_DOCTORS.write_text("\n".join(lines), encoding="utf-8")
def main() -> None:
excel_hospitals = parse_excel()
name_to_excel = {h["name"]: h for h in excel_hospitals}
hospital_names: dict[str, str] = {}
hospital_intros: dict[str, str] = {}
doctor_zh: dict[str, dict] = {}
missing_hospitals: list[str] = []
for hid, hospital_name in FOLDER_TO_HOSPITAL.items():
excel = name_to_excel.get(hospital_name)
if not excel:
missing_hospitals.append(hospital_name)
continue
hospital_names[hid] = hospital_name
hospital_intros[hid] = format_intro_zh(excel["intro"], excel["cert"])
for doc in excel["doctors"]:
did = f"{hid}-{doc['name']}"
fields = {
"name": doc["name"],
"department": doc["department"],
"hospitalName": hospital_name,
"expertLabel": doc["expert"],
}
if doc.get("expertInfo"):
fields["expertInfo"] = doc["expertInfo"]
doctor_zh[did] = fields
if missing_hospitals:
raise SystemExit(f"Excel 中缺少医院: {missing_hospitals}")
write_names(hospital_names)
write_intros(hospital_intros)
write_doctors(doctor_zh)
print(f"OK: {len(hospital_names)} hospitals, {len(doctor_zh)} doctors")
print(f" -> {OUT_NAMES.name}")
print(f" -> {OUT_INTROS.name}")
print(f" -> {OUT_DOCTORS.name}")
if __name__ == "__main__":
main()