from __future__ import annotations from typing import List, Dict import os import json from .config import settings SYNONYM_MAP = { # 症状同义 "胃脘胀": "胀满", "胃胀": "胀满", "脘闷": "胀满", "心窝胀": "胀满", "嗳腐": "嗳气", "烧心": "反酸", # 舌脉 "舌苔厚腻": "舌苔白腻", "苔白腻": "舌苔白腻", # 证型/病机关键词 "肝郁": "肝气郁结", "气机不畅": "气滞", "食滞": "饮食积滞", "痰湿": "痰湿中阻", "脾虚": "脾胃虚弱", "阳虚": "脾胃虚寒", } _external_map: Dict[str, str] | None = None def _load_external_synonyms() -> Dict[str, str]: global _external_map if _external_map is not None: return _external_map path = settings.synonyms_path mapping: Dict[str, str] = {} if os.path.isfile(path): try: text = open(path, "r", encoding="utf-8").read() # 允许 YAML 或 JSON;无 PyYAML 时做轻量级解析 if path.endswith(".json") or text.strip().startswith("{"): mapping = json.loads(text) else: # 简易 YAML: 支持 "a: b" 每行一条;忽略注释与空行 for line in text.splitlines(): line = line.strip() if (not line) or line.startswith("#"): continue if ":" in line: k, v = line.split(":", 1) k = k.strip().strip('"\'') v = v.strip().strip('"\'') if k and v: mapping[k] = v except Exception: mapping = {} _external_map = mapping return mapping def normalize_terms(terms: List[str]) -> List[str]: normalized: List[str] = [] external = _load_external_synonyms() for t in terms: if not t: continue t2 = t.strip() t2 = external.get(t2, SYNONYM_MAP.get(t2, t2)) if t2 not in normalized: normalized.append(t2) return normalized