Files
Jyotisha/scripts/chart_view_points/karaka_rank.py
T
ClaudeandClaude Opus 5.5 578a361460 feat(chart): functional roles, 7/8 Chara Karaka and three new 特殊点 groups (#163 1-3)
/api/chart_points:
- karaka_scheme 7 (default) / 8 / both; shared ranking in
  chart_view_points/karaka_rank.py (unrounded in-sign degree, Rahu reversed
  in the 8 scheme, Ketu out). Bodies equal at full precision mark only the
  roles they span as tied; Goel uses the same ranking (BUG-1512).
- functional_roles: v2 BPHS ch.34 profile laid out per body, nodes pending,
  Yogakaraka / Badhaka / Kendradhipati (conditions not evaluated) as
  separate labels.
- sensitive_points (Bhrigu Bindu per chart node_mode, 64th Navamsa, 22nd
  Drekkana sign-only), sahams (PVR Table 74, nine first, all kept),
  goel_points (printed-text policy, worked example beside it).

盘面数据: 行星 gets Chara Karaka (AK · 自我, 七星/八星 switch), 自然属性,
管辖宫位, 功能角色, 补充身份; 特殊点 splits into 特殊上升 / 敏感点 / Saham /
Goel 特殊点 and the board's 特殊点 layer follows the open group.

jaimini.py, functional_benefics.py and varga.py are untouched (rectification
frozen identity).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y
2026-11-02 20:35:31 +08:00

114 lines
4.8 KiB
Python

"""Chara karaka ranking with explicit ties (issue #163).
One ranking for the chart page's karaka column and Goel's full Jaimini
strength. Bodies are ordered by their unrounded in-sign degree, highest first.
In the eight-karaka scheme Rahu is included, Ketu is not, and Rahu ranks by
``30 - degree in sign``, the same reversal as ``jaimini.calc_chara_karaka_8``.
Two bodies whose sort degrees are still equal at full precision (within
``TIE_TOLERANCE``) form a tie. The roles they span are reported as contested
with every candidate; nothing is ordered by input or array order. Roles outside
a tie keep their planet.
``jaimini.calc_chara_karaka_7`` / ``calc_chara_karaka_8`` are part of the
rectification production identity and are not changed here; on untied input
this ranking gives the same order (tests/test_chara_karaka_rank.py).
"""
from __future__ import annotations
from typing import Any, Mapping
try:
from jaimini import KARAKA_7, KARAKA_8
except ImportError: # package import (scripts.chart_view_points.karaka_rank)
from scripts.jaimini import KARAKA_7, KARAKA_8
SEVEN = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn")
EIGHT = SEVEN + ("Rahu",)
TIE_TOLERANCE = 1e-10
KARAKA_SHORT = {
"Atmakaraka": "AK", "Amatyakaraka": "AmK", "Bhratrikaraka": "BK", "Matrikaraka": "MK",
"Pitrukaraka": "PiK", "Putrakaraka": "PK", "Gnatikaraka": "GK", "Darakaraka": "DK",
}
SCHEMES = {7: (SEVEN, KARAKA_7), 8: (EIGHT, KARAKA_8)}
def sort_degree(planet: str, degree_in_sign: float, scheme: int) -> float:
"""The value a body is ranked by: in-sign degree, Rahu reversed in the 8 scheme."""
raw = float(degree_in_sign) % 30.0
if scheme == 8 and planet == "Rahu":
return (30.0 - raw) % 30.0
return raw
def rank_chara_karakas(degrees: Mapping[str, float], scheme: int = 7) -> dict[str, Any]:
"""Rank the scheme's bodies; ``degrees`` holds in-sign degrees or longitudes.
Returns ``status: blocked`` when a body is missing. Otherwise ``rows`` has
one entry per role in rank order. A tied role has ``planet: None``,
``tied: True`` and the tied bodies in ``candidates``; ``ties`` lists each
tie once with the roles it spans.
"""
if scheme not in SCHEMES:
raise ValueError("chara karaka scheme must be 7 or 8")
bodies, roles = SCHEMES[scheme]
missing = [planet for planet in bodies if not isinstance(degrees.get(planet), (int, float)) or isinstance(degrees.get(planet), bool)]
if missing:
return {"status": "blocked", "scheme": scheme, "reason": "planets_missing", "missing": missing}
raw = {planet: float(degrees[planet]) % 30.0 for planet in bodies}
keyed = {planet: sort_degree(planet, raw[planet], scheme) for planet in bodies}
ordered = sorted(bodies, key=lambda planet: keyed[planet], reverse=True)
# Group runs of equal sort degrees. A run of one is an ordinary rank.
groups: list[list[str]] = []
for planet in ordered:
if groups and abs(keyed[groups[-1][-1]] - keyed[planet]) <= TIE_TOLERANCE:
groups[-1].append(planet)
else:
groups.append([planet])
rows: list[dict[str, Any]] = []
ties: list[dict[str, Any]] = []
by_planet: dict[str, dict[str, Any]] = {}
rank = 1
for group in groups:
span = list(range(rank, rank + len(group)))
tied = len(group) > 1
candidates = [planet for planet in bodies if planet in group] # fixed body order, not input order
span_keys = [KARAKA_SHORT[roles[r]] for r in span]
if tied:
ties.append({"planets": candidates, "roles": span_keys, "sort_degree": keyed[group[0]]})
for offset, r in enumerate(span):
name = roles[r]
planet = None if tied else group[0]
rows.append({
"rank": r,
"key": KARAKA_SHORT[name],
"name": name,
"planet": planet,
"tied": tied,
"candidates": candidates if tied else [group[0]],
"degree_in_sign": raw[group[offset]],
"sort_degree": keyed[group[offset]],
"rahu_reverse_applied": scheme == 8 and not tied and group[0] == "Rahu",
})
for planet in group:
by_planet[planet] = {
"role": None if tied else span_keys[0],
"tied": tied,
"contested_roles": span_keys if tied else [],
"tied_with": [p for p in candidates if p != planet],
}
rank += len(group)
return {
"status": "computed",
"scheme": scheme,
"rows": rows,
"ties": ties,
"planets": by_planet,
"order": ordered if not ties else None,
"tie_tolerance": TIE_TOLERANCE,
"sort_basis": "unrounded_degree_in_sign" if scheme == 7 else "unrounded_degree_in_sign_rahu_30_minus",
}