Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
147 lines
6.8 KiB
Python
147 lines
6.8 KiB
Python
"""Calculation and interpretation axes shared with the consultation audit."""
|
||
|
||
import re
|
||
|
||
CALCULATION_LABELS = {
|
||
"executed": "已执行", "partial": "部分执行", "blocked": "阻塞", "not_applicable": "不适用",
|
||
}
|
||
INTERPRETATION_LABELS = {
|
||
"primary": "主要依据", "supporting": "辅助依据", "conditional": "有条件",
|
||
"observation_only": "仅观察", "blocked": "不作判断", "not_used": "不参与",
|
||
}
|
||
CALCULATION_LABELS_EN = {
|
||
"executed": "Calculation completed",
|
||
"partial": "Calculation partially completed",
|
||
"blocked": "Calculation blocked",
|
||
"not_applicable": "Calculation not applicable",
|
||
"not_run": "Calculation not completed",
|
||
}
|
||
INTERPRETATION_LABELS_EN = {
|
||
"primary": "primary interpretive basis",
|
||
"supporting": "supporting interpretive evidence",
|
||
"conditional": "conditional interpretation",
|
||
"observation_only": "observation only",
|
||
"blocked": "no interpretation",
|
||
"not_used": "excluded from interpretation",
|
||
}
|
||
FUNCTIONAL_PROFILE_LABELS = {"pvr_table30_v1": "PVR 表 30", "bphs_exception_profile": "BPHS 例外表"}
|
||
CONDITIONAL_EVIDENCE = {
|
||
"parameter_sensitive", "profile_sensitive", "single_system_inference",
|
||
"unclosed_divisional_chart", "user_history_verification_required",
|
||
}
|
||
_CANDIDATE = re.compile(
|
||
r"^(?:used|executed|已执行)?\s*[((]\s*(?:候选|candidate)\s*[))]$|^(?:候选|candidate|single_system_candidate)$",
|
||
re.I,
|
||
)
|
||
_CALCULATION_ALIASES = {
|
||
"partial": ("partial", "部分执行", "部分", "不足", "insufficient"),
|
||
"executed": ("已执行", "已匹配", "used", "executed", "complete", "official_verified", "pass", "ready", "ok", "matched"),
|
||
"not_applicable": ("不适用", "not_applicable", "n/a", "na", "not applicable"),
|
||
}
|
||
_INTERPRETATION_ALIASES = {
|
||
"primary": ("primary", "主要依据", "主要"),
|
||
"supporting": ("supporting", "辅助依据", "辅助"),
|
||
"conditional": ("conditional", "有条件", "条件性", "候选", "candidate"),
|
||
"observation_only": ("observation_only", "observation only", "仅观察", "观察"),
|
||
"blocked": ("blocked", "不作判断", "阻塞"),
|
||
}
|
||
|
||
|
||
def _key(value):
|
||
return str(value or "").strip().lower().replace(chr(96), "").replace("*", "")
|
||
|
||
|
||
def parse_technique_calculation_status(value, has_result=True):
|
||
normalized = _key(value)
|
||
if normalized in CONDITIONAL_EVIDENCE:
|
||
return "executed" if has_result else "partial"
|
||
if _CANDIDATE.fullmatch(str(value or "").strip()):
|
||
return "executed"
|
||
return next((key for key, labels in _CALCULATION_ALIASES.items() if normalized in labels), "blocked")
|
||
|
||
|
||
def parse_technique_audit_status(value, has_result=True):
|
||
calculation = parse_technique_calculation_status(value, has_result)
|
||
if _key(value) in CONDITIONAL_EVIDENCE or _CANDIDATE.fullmatch(str(value or "").strip()):
|
||
interpretation = "conditional"
|
||
else:
|
||
interpretation = {
|
||
"executed": "primary", "not_applicable": "not_used", "partial": "conditional", "blocked": "blocked",
|
||
}[calculation]
|
||
return {"calculation_status": calculation, "interpretation_status": interpretation}
|
||
|
||
|
||
def _has_result(packet, row):
|
||
if row.get("result"):
|
||
return True
|
||
worksheets = packet.get("worksheets") or {}
|
||
timing = worksheets.get("timing_and_predictive_systems") or {}
|
||
advanced = worksheets.get("advanced_systems") or {}
|
||
strengths = worksheets.get("strengths_and_scores") or {}
|
||
technique = row.get("technique")
|
||
if technique in ("Narayana Dasha", "Narayana / Patyayini / Tajika Parity"):
|
||
narayana = timing.get("narayana_dasha") or {}
|
||
return bool(narayana.get("current_dasha") or narayana.get("branches") or narayana.get("mahadasha"))
|
||
paths = {
|
||
"Vimshottari Dasha": (timing, "dasha"),
|
||
"KP Lord/Sub": (advanced, "kp"),
|
||
"Annual Tajika": (timing, "annual_tajika_pack"),
|
||
"Functional Benefic/Malefic": (strengths, "functional_benefic_malefic"),
|
||
}
|
||
source, key = paths.get(technique, ({}, None))
|
||
result = source.get(key)
|
||
return isinstance(result, dict) and bool(result) and not result.get("error") and result.get("status") != "blocked"
|
||
|
||
|
||
def technique_audit_axes(packet, row):
|
||
status = _key(row.get("status"))
|
||
if isinstance(row.get("calculation_status"), str):
|
||
calculation = parse_technique_calculation_status(row["calculation_status"])
|
||
raw_interpretation = _key(row.get("interpretation_status"))
|
||
interpretation = next((key for key, labels in _INTERPRETATION_ALIASES.items()
|
||
if raw_interpretation in labels), "not_used")
|
||
axes = {"calculation_status": calculation, "interpretation_status": interpretation}
|
||
else:
|
||
axes = parse_technique_audit_status(row.get("status"), _has_result(packet, row))
|
||
if status in CONDITIONAL_EVIDENCE:
|
||
axes["interpretation_evidence"] = status
|
||
if axes["calculation_status"] == "partial":
|
||
axes["calculation_gap"] = "calculation_result_missing"
|
||
elif status and axes["calculation_status"] == "blocked" and status not in ("blocked", "not_run", "not run", "未完成"):
|
||
axes["calculation_gap"] = status
|
||
return axes
|
||
|
||
|
||
def technique_audit_text(row, axes, language):
|
||
calculation = axes["calculation_status"]
|
||
interpretation = axes["interpretation_status"]
|
||
evidence = axes.get("interpretation_evidence")
|
||
if evidence:
|
||
# BUG-2095: the condition in words (was the code, e.g. `parameter_sensitive`).
|
||
from .reader_keys import reader_key
|
||
|
||
evidence = reader_key(evidence, language)
|
||
unfinished = _key(row.get("status")) in ("not_run", "not run", "未完成") or _key(row.get("calculation_status")) in ("not_run", "未完成")
|
||
if language == "en":
|
||
calc = CALCULATION_LABELS_EN["not_run" if unfinished else calculation]
|
||
suffix = f" ({evidence})" if evidence else ""
|
||
return f"{calc}, {INTERPRETATION_LABELS_EN[interpretation]}{suffix}"
|
||
calc = "未完成" if unfinished else CALCULATION_LABELS[calculation]
|
||
suffix = f"({evidence})" if evidence else ""
|
||
return f"计算{calc},解读{INTERPRETATION_LABELS[interpretation]}{suffix}"
|
||
|
||
|
||
def functional_profile_text(value, language="zh"):
|
||
if not value:
|
||
return "profile not specified" if language == "en" else "口径未注明"
|
||
labels = {"pvr_table30_v1": "PVR Table 30", "bphs_exception_profile": "BPHS exception table"} if language == "en" else FUNCTIONAL_PROFILE_LABELS
|
||
label = labels.get(value)
|
||
if label is None:
|
||
from .reader_keys import reader_key
|
||
|
||
return str(reader_key(str(value), language))
|
||
# 原值:「PVR 表 30(pvr_table30_v1)」/ "PVR Table 30 (pvr_table30_v1)"。
|
||
# 新值:只写口径名;版本只在功能吉凶表的口径一行(functional_layer_table)写一次。
|
||
# 原因:BUG-2095,读者正文不出现内部版本代码。
|
||
return label
|