Preserve raw HTML alongside stripped text when archiving documents
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
"""Download every clinical document (病历文书) for each patient/visit as plain text.
|
||||
"""Download every clinical document (病历文书) for each patient/visit, saving
|
||||
both the raw HTML and a tag-stripped plain-text copy.
|
||||
|
||||
Unlike download_documents.py (which only extracts 手术记录/出院记录 into a
|
||||
summary CSV), this walks the full Medical/Index document list per visit and
|
||||
saves each document's complete text locally, plus a per-patient manifest.
|
||||
saves each document's complete content locally, plus a per-patient manifest.
|
||||
The HTML is kept alongside the .txt because stripping tags flattens any
|
||||
tables (lab results, checkbox exam findings) into a single line of
|
||||
space-separated text, which loses structure that might matter later.
|
||||
|
||||
Usage:
|
||||
python download_all_documents.py --template patient_id.csv --output-dir docs_full
|
||||
@@ -25,9 +29,10 @@ from download_documents import (
|
||||
DEFAULT_BASE_URL,
|
||||
DEFAULT_DELAY_SEC,
|
||||
DEFAULT_HOSPITAL_NO,
|
||||
get_document_text,
|
||||
get_document_html,
|
||||
get_medical_index,
|
||||
get_patient_info,
|
||||
html_to_text,
|
||||
)
|
||||
|
||||
|
||||
@@ -49,22 +54,23 @@ def process_patient(base_url: str, hospital_no: str, patient_id: str, patient_di
|
||||
seen_names: dict[str, int] = {}
|
||||
|
||||
for rec in records:
|
||||
text = get_document_text(session, base_url, rec["doc_id"])
|
||||
html = get_document_html(session, base_url, rec["doc_id"])
|
||||
safe_name = sanitize_filename(rec["name"])
|
||||
seen_names[safe_name] = seen_names.get(safe_name, 0) + 1
|
||||
suffix = f"_{seen_names[safe_name]}" if seen_names[safe_name] > 1 else ""
|
||||
safe_date = rec["date"].replace("/", "-").replace(" ", "_").replace(":", "")
|
||||
filename = f"{safe_date}_{safe_name}{suffix}.txt"
|
||||
base_name = f"{safe_date}_{safe_name}{suffix}"
|
||||
|
||||
if text:
|
||||
if html:
|
||||
visit_dir.mkdir(parents=True, exist_ok=True)
|
||||
(visit_dir / filename).write_text(text, encoding="utf-8")
|
||||
(visit_dir / f"{base_name}.html").write_text(html, encoding="utf-8")
|
||||
(visit_dir / f"{base_name}.txt").write_text(html_to_text(html), encoding="utf-8")
|
||||
|
||||
visit_manifest["documents"].append({
|
||||
"doc_id": rec["doc_id"],
|
||||
"date": rec["date"],
|
||||
"name": rec["name"],
|
||||
"saved_as": filename if text else None,
|
||||
"saved_as": base_name if html else None,
|
||||
})
|
||||
|
||||
manifest["visits"].append(visit_manifest)
|
||||
|
||||
+11
-2
@@ -106,16 +106,25 @@ def get_medical_index(session: requests.Session, base_url: str, hospital_no: str
|
||||
return [{"doc_id": doc_id, "date": d.strip(), "name": n.strip()} for doc_id, d, n in docs]
|
||||
|
||||
|
||||
def get_document_text(session: requests.Session, base_url: str, doc_id: str) -> str | None:
|
||||
def get_document_html(session: requests.Session, base_url: str, doc_id: str) -> str | None:
|
||||
resp = session.get(f"{base_url}/Medical/MedicalRecord", params={"fileId": doc_id}, timeout=30)
|
||||
resp.encoding = "utf-8"
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
text = re.sub(r"<[^>]+>", " ", resp.text)
|
||||
return resp.text
|
||||
|
||||
|
||||
def html_to_text(html: str) -> str:
|
||||
text = re.sub(r"<[^>]+>", " ", html)
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
return text.replace(" ", " ")
|
||||
|
||||
|
||||
def get_document_text(session: requests.Session, base_url: str, doc_id: str) -> str | None:
|
||||
html = get_document_html(session, base_url, doc_id)
|
||||
return html_to_text(html) if html is not None else None
|
||||
|
||||
|
||||
def first_match(patterns: list[str], text: str, max_len: int = 500) -> str:
|
||||
for pattern in patterns:
|
||||
match = re.search(pattern, text, re.DOTALL)
|
||||
|
||||
Reference in New Issue
Block a user