Files
Jyotisha/scripts/report_render/yoga_effect_gate.py
T

142 lines
7.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""BUG-2300: one reading of the yoga engine's effect gates for every consumer.
The engine (`yoga_engine._apply_effect_and_method_gates`, upstream 58f37ee5b) moves a
gated rule's traditional effects to `traditional_effects` and empties `effects`. A
consumer that falls back to other prose (the rule's reader explanation, `interpretation`,
`summary`, an archived alias's effects) would put the traditional verdict back as a
personal conclusion. Every consumer asks here instead.
"""
from __future__ import annotations
from typing import Any
# Her five effect statuses (upstream 58f37ee5b `detect` / `professional_parity_closure`).
GATED_EFFECT_STATUSES = frozenset({
"source_neutralized", "incomplete", "historical_medical_reference",
"historical_conduct_reference", "historical_injury_reference",
})
# Row states under which the engine withholds the traditional effects.
WITHHELD_FORMATION_STATUSES = frozenset({
"method_requires_review", "combustion_requires_review", "incomplete_conditions",
"not_implemented", "evaluation_error", "blocked",
})
# Her page sentences (upstream professional_parity_closure.build_yoga_interpretation_pack).
_RESULT_ZH = {
"historical_injury_reference": "保留传统条件与历史解释,不据此判断战争、伤害或死亡事件。",
"historical_conduct_reference": "保留传统结构与出处,不能据此认定本人或伴侣不忠。",
"historical_medical_reference": "保留传统条件与历史解释,不据此判断疾病、伤残或治疗结果。",
"source_neutralized": "原文所述作用已有缓解条件,不直接用于判断个人性格或结果。",
"incomplete": "结构已经核对,但影响其表现的条件资料不全,暂不据此判断个人性格或结果。",
}
_RESULT_EN = {
"historical_injury_reference": "The traditional condition and its historical reading are kept; no war, injury or death event is inferred.",
"historical_conduct_reference": "The traditional structure and its source are kept; no infidelity or personal conduct is established.",
"historical_medical_reference": "The traditional condition and its historical reading are kept; no illness, disability or treatment outcome is inferred.",
"source_neutralized": "The source's stated effect has a mitigating condition; it is not used to judge character or outcome.",
"incomplete": "The structure was checked, but inputs that shape its expression are missing; no character or outcome is judged from it.",
}
# BUG-2304 (负责人 BUG-2280:不对读者写「待核」「待核实」「还没核实」): 原值「取法尚待核定,保留候选结构,不据此写传统作用。」 /
# "The method is still under review; …" / 新值:只说列出结构、不按传统说法下结论;method_review 状态只留在后台字段。
_METHOD_ZH = "这一条只列出结构,不按传统说法下结论。"
_METHOD_EN = "Only the structure is listed; no traditional result is drawn from it."
_OTHER_ZH = "部分条件缺少资料,这一条只列出结构,不按传统说法下结论。"
_OTHER_EN = "Some inputs are missing; only the structure is listed and no traditional result is drawn from it."
# BUG-2304: clauses of her source texts (description_zh / summary_zh and their English) that say a qualifier is still
# being decided. They stay in the backend data; reader and writer text drop them.
import re as _re
_PENDING_ZH = _re.compile(r"待核|还没核实|尚待|尚未核定|未核定|另须[^;;。,,]{0,12}核定|另行核定|另待核定|须分开核定|须分别核定")
_PENDING_EN = _re.compile(r"pending review|under review|\bunresolved\b|not yet adjudicated|requires? adjudication|"
r"awaiting (?:review|adjudication)", _re.IGNORECASE)
def reader_text(text: Any, language: str = "zh") -> str:
"""``text`` without the clauses that tell the reader a qualifier is still being decided (BUG-2304)."""
value = str(text or "")
pattern = _PENDING_ZH if language != "en" else _PENDING_EN
if not pattern.search(value):
return value
if language != "en":
parts = _re.split(r"([;;。])", value)
kept, out = [], ""
for index in range(0, len(parts), 2):
clause, sep = parts[index], parts[index + 1] if index + 1 < len(parts) else ""
sub = [piece for piece in _re.split(r"(?<=[,,])", clause) if not pattern.search(piece)]
clause = "".join(sub).rstrip(",,")
if clause.strip():
kept.append(clause + sep)
out = "".join(kept).strip()
return out.strip(";;,,")
sentences = _re.split(r"(?<=[.;])\s+", value)
return " ".join(sentence for sentence in sentences if not pattern.search(sentence)).strip().rstrip(";,")
def effect_status(row: Any) -> str | None:
"""The row's gated effect status (one of her five), or None."""
if not isinstance(row, dict):
return None
assessment = row.get("effect_assessment")
status = assessment.get("status") if isinstance(assessment, dict) else None
return status if status in GATED_EFFECT_STATUSES else None
def _formation_states(row: dict) -> set[str]:
states = {str(row.get(key) or "") for key in ("formation_status", "condition_status", "display_status")}
review = row.get("method_review")
if isinstance(review, dict) and review.get("status") == "pending":
states.add("method_requires_review")
return states
def effect_withheld(row: Any) -> bool:
"""True when the engine withheld this row's traditional effects."""
if not isinstance(row, dict):
return False
if effect_status(row) is not None:
return True
return bool(_formation_states(row) & WITHHELD_FORMATION_STATUSES)
def withheld_note(row: Any, language: str = "zh") -> str:
"""The sentence a reader sees instead of the traditional effect ('' when not withheld)."""
if not effect_withheld(row):
return ""
zh = language != "en"
status = effect_status(row)
if status is not None:
assessment = row.get("effect_assessment") or {}
summary = reader_text(assessment.get("summary_zh" if zh else "summary_en"), "zh" if zh else "en")
if summary.strip():
return summary.strip()
return (_RESULT_ZH if zh else _RESULT_EN)[status]
if "method_requires_review" in _formation_states(row):
return _METHOD_ZH if zh else _METHOD_EN
return _OTHER_ZH if zh else _OTHER_EN
def page_result_sentence(row: Any, language: str = "zh") -> str:
"""Her page `result` line for a gated effect status (not the summary), or ''."""
status = effect_status(row)
if status is None:
return ""
return (_RESULT_ZH if language != "en" else _RESULT_EN)[status]
# BUG-2304: rule grade keys the engine writes on gated or observation rows, in reader words.
_STRENGTH_WORDS = {
"conditional": ("视条件而定", "Conditional"),
"not_assessed": ("未评估", "Not assessed"),
"observation": ("结构观察", "Observation"),
}
def reader_strength(value: Any, language: str = "zh") -> Any:
"""A rule grade key as a reader word; any other value unchanged."""
words = _STRENGTH_WORDS.get(str(value or "").strip())
if not words:
return value
return words[1] if language == "en" else words[0]