Spaces:
Sleeping
Sleeping
File size: 2,132 Bytes
dbb49bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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
|