Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y
181 lines
8.5 KiB
Python
181 lines
8.5 KiB
Python
"""Shared Ashtottari applicability rule-family helpers.
|
||
|
||
This module does not declare a single verified truth. It only centralizes the
|
||
repo's current competing rule families so runtime surfaces can expose them
|
||
consistently and audits can compare them without hidden drift.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
RULE_FAMILY_MOON_NAKSHATRA_PLUS_PAKSHA = "moon_nakshatra_plus_paksha"
|
||
RULE_FAMILY_RAHU_NOT_IN_KENDRA = "rahu_not_in_kendra"
|
||
RULE_FAMILY_RAHU_FROM_LAGNA_LORD = "rahu_from_lagna_lord_kendra_trikona_plus_paksha_daynight"
|
||
|
||
SHUKLA_PAKSHA_NAKSHATRAS = {0, 4, 6, 13, 20, 22}
|
||
KRISHNA_PAKSHA_NAKSHATRAS = {3, 5, 7, 8, 9, 26}
|
||
ALLOWED_RAHU_HOUSES_FROM_LAGNA_LORD = {4, 5, 7, 9, 10}
|
||
KENDRA_HOUSES = {1, 4, 7, 10}
|
||
|
||
|
||
def evaluate_moon_nakshatra_plus_paksha_rule(
|
||
moon_nakshatra_index: int | None,
|
||
is_shukla_paksha: bool | None,
|
||
) -> tuple[bool, str]:
|
||
if moon_nakshatra_index is None or is_shukla_paksha is None:
|
||
return False, "Moon-nakshatra/Paksha rule needs moon_nakshatra_index and is_shukla_paksha."
|
||
allowed = SHUKLA_PAKSHA_NAKSHATRAS if is_shukla_paksha else KRISHNA_PAKSHA_NAKSHATRAS
|
||
if moon_nakshatra_index in allowed:
|
||
return True, "Moon-nakshatra/Paksha rule matched the current whitelist."
|
||
return False, "Moon-nakshatra/Paksha rule did not match the current whitelist."
|
||
|
||
|
||
def evaluate_rahu_not_in_kendra_rule(rahu_house: int | None) -> tuple[bool, str]:
|
||
if rahu_house is None:
|
||
return False, "Rahu-not-in-Kendra rule needs rahu_house."
|
||
if rahu_house not in KENDRA_HOUSES:
|
||
return True, f"Rahu-not-in-Kendra rule matched: Rahu house {rahu_house} is outside 1/4/7/10."
|
||
return False, f"Rahu-not-in-Kendra rule blocked: Rahu house {rahu_house} is in 1/4/7/10."
|
||
|
||
|
||
def evaluate_rahu_from_lagna_lord_rule(birth_info: dict[str, Any]) -> tuple[bool, str]:
|
||
birth_is_daytime = birth_info.get("birth_is_daytime")
|
||
is_shukla = birth_info.get("is_shukla_paksha")
|
||
rahu_sign_index = birth_info.get("rahu_sign_index")
|
||
lagna_lord_sign_index = birth_info.get("lagna_lord_sign_index")
|
||
missing = [
|
||
name for name, value in (
|
||
("birth_is_daytime", birth_is_daytime),
|
||
("is_shukla_paksha", is_shukla),
|
||
("rahu_sign_index", rahu_sign_index),
|
||
("lagna_lord_sign_index", lagna_lord_sign_index),
|
||
)
|
||
if value is None
|
||
]
|
||
if missing:
|
||
return False, f"Candidate Ashtottari rule needs {', '.join(missing)}."
|
||
|
||
if birth_is_daytime and not is_shukla:
|
||
pass
|
||
elif (not birth_is_daytime) and is_shukla:
|
||
pass
|
||
else:
|
||
return False, (
|
||
"Candidate Ashtottari gate expects day birth in Krishna Paksha or "
|
||
"night birth in Shukla Paksha."
|
||
)
|
||
|
||
house_from_lagna_lord = ((int(rahu_sign_index) - int(lagna_lord_sign_index)) % 12) + 1
|
||
if house_from_lagna_lord not in ALLOWED_RAHU_HOUSES_FROM_LAGNA_LORD:
|
||
return False, (
|
||
"Rahu is not in Kendra/Trikona from the Lagna lord under the frozen candidate rule."
|
||
)
|
||
return True, (
|
||
f"Candidate rule matched: {('day' if birth_is_daytime else 'night')} birth, "
|
||
f"{'Krishna' if not is_shukla else 'Shukla'} Paksha, Rahu {house_from_lagna_lord}th from Lagna lord."
|
||
)
|
||
|
||
|
||
def evaluate_rule_family(rule_family: str, payload: dict[str, Any]) -> tuple[bool, str]:
|
||
if rule_family == RULE_FAMILY_MOON_NAKSHATRA_PLUS_PAKSHA:
|
||
return evaluate_moon_nakshatra_plus_paksha_rule(
|
||
payload.get("moon_nakshatra_index"),
|
||
payload.get("is_shukla_paksha"),
|
||
)
|
||
if rule_family == RULE_FAMILY_RAHU_NOT_IN_KENDRA:
|
||
return evaluate_rahu_not_in_kendra_rule(payload.get("rahu_house"))
|
||
if rule_family == RULE_FAMILY_RAHU_FROM_LAGNA_LORD:
|
||
return evaluate_rahu_from_lagna_lord_rule(payload)
|
||
return False, f"Unknown Ashtottari rule family: {rule_family}"
|
||
|
||
|
||
# Astrologer rulings 2026-11-07 (BUG-1676). Second reply: the production gate
|
||
# is BPHS's geometric condition only: Rahu is not in the natal Lagna and sits
|
||
# 4/5/7/9/10 from the Lagna lord (Rahu with the Lagna lord, the 1st, does not
|
||
# count). The Shukla-night / Krishna-day rule belongs to later or other
|
||
# lineages; it is computed and shown as a recorded variant and never decides.
|
||
# The Moon-nakshatra whitelist is not a gate either; the Moon's nakshatra still
|
||
# sets the starting lord and the balance.
|
||
RULE_FAMILY_STRICT = "bphs_rahu_from_lagna_lord_excluding_lagna_v1"
|
||
STRICT_RULE_SOURCE = "BPHS, Ashtottari condition: Rahu not in the Lagna, in a kendra / trikona from the Lagna lord"
|
||
|
||
|
||
def evaluate_strict_ashtottari(payload: dict[str, Any]) -> dict[str, Any]:
|
||
"""The Rahu gate decides; the Paksha / day-night gate is recorded only.
|
||
|
||
status: applicable / not_applicable / blocked (Rahu inputs missing).
|
||
"""
|
||
rahu = payload.get("rahu_sign_index")
|
||
lagna = payload.get("ascendant_sign_index", payload.get("lagna_rashi_index"))
|
||
lagna_lord = payload.get("lagna_lord_sign_index")
|
||
daytime = payload.get("birth_is_daytime")
|
||
shukla = payload.get("is_shukla_paksha")
|
||
|
||
rahu_gate: dict[str, Any] = {"rule": "rahu_from_lagna_lord", "role": "decides", "source": "BPHS"}
|
||
if rahu is None or lagna is None or lagna_lord is None:
|
||
rahu_gate.update(status="missing", missing=[
|
||
name for name, value in (("rahu_sign_index", rahu), ("ascendant_sign_index", lagna),
|
||
("lagna_lord_sign_index", lagna_lord)) if value is None])
|
||
else:
|
||
position = ((int(rahu) - int(lagna_lord)) % 12) + 1
|
||
in_lagna = int(rahu) % 12 == int(lagna) % 12
|
||
rahu_gate.update(
|
||
position_from_lagna_lord=position,
|
||
rahu_in_lagna=in_lagna,
|
||
status="pass" if not in_lagna and position in ALLOWED_RAHU_HOUSES_FROM_LAGNA_LORD else "fail",
|
||
)
|
||
|
||
paksha_gate: dict[str, Any] = {"rule": "paksha_day_night", "role": "recorded_only", "source": "later / other lineages"}
|
||
if daytime is None or shukla is None:
|
||
paksha_gate.update(status="missing", missing=[
|
||
name for name, value in (("birth_is_daytime", daytime), ("is_shukla_paksha", shukla)) if value is None])
|
||
else:
|
||
paksha_gate.update(
|
||
birth_is_daytime=bool(daytime),
|
||
is_shukla_paksha=bool(shukla),
|
||
status="pass" if bool(shukla) != bool(daytime) else "fail",
|
||
)
|
||
|
||
status = {"pass": "applicable", "fail": "not_applicable"}.get(rahu_gate["status"], "blocked")
|
||
return {
|
||
"rule_family": RULE_FAMILY_STRICT,
|
||
"source": STRICT_RULE_SOURCE,
|
||
"status": status,
|
||
"gates": {"rahu_from_lagna_lord": rahu_gate, "paksha_day_night": paksha_gate},
|
||
}
|
||
|
||
|
||
def strict_reason_en(result: dict[str, Any]) -> str:
|
||
gates = result.get("gates") or {}
|
||
rahu = gates.get("rahu_from_lagna_lord") or {}
|
||
paksha = gates.get("paksha_day_night") or {}
|
||
parts = []
|
||
if rahu.get("status") == "missing":
|
||
parts.append("missing inputs: " + ", ".join(rahu.get("missing") or []))
|
||
if rahu.get("status") in {"pass", "fail"}:
|
||
where = "in the Lagna" if rahu.get("rahu_in_lagna") else f"{rahu.get('position_from_lagna_lord')} from the Lagna lord"
|
||
parts.append(f"Rahu {where}, {'meets' if rahu['status'] == 'pass' else 'does not meet'} the 4/5/7/9/10 condition")
|
||
if paksha.get("status") in {"pass", "fail"}:
|
||
side = "Shukla" if paksha.get("is_shukla_paksha") else "Krishna"
|
||
when = "day" if paksha.get("birth_is_daytime") else "night"
|
||
parts.append(f"{side} Paksha and a {when} birth (day-night variant, recorded only: {'met' if paksha['status'] == 'pass' else 'not met'})")
|
||
return "; ".join(parts)
|
||
|
||
|
||
def strict_reason_zh(result: dict[str, Any]) -> str:
|
||
gates = result.get("gates") or {}
|
||
rahu = gates.get("rahu_from_lagna_lord") or {}
|
||
paksha = gates.get("paksha_day_night") or {}
|
||
parts = []
|
||
if rahu.get("status") == "missing":
|
||
parts.append("缺少罗睺或上升主星位置,无法判断")
|
||
if rahu.get("status") in {"pass", "fail"}:
|
||
where = "罗睺在上升星座" if rahu.get("rahu_in_lagna") else f"罗睺在上升主星起算第 {rahu.get('position_from_lagna_lord')} 位"
|
||
parts.append(f"{where},{'符合' if rahu['status'] == 'pass' else '不符合'}第 4、5、7、9、10 位的条件")
|
||
if paksha.get("status") in {"pass", "fail"}:
|
||
side = "白半月" if paksha.get("is_shukla_paksha") else "黑半月"
|
||
when = "白天" if paksha.get("birth_is_daytime") else "夜间"
|
||
parts.append(f"{side}{when}出生(昼夜与月相条件只作来源差异记录,{'符合' if paksha['status'] == 'pass' else '不符合'},不参与判定)")
|
||
return ";".join(parts)
|