Files
o2o-weather/backend/services/saju/strength.py
T

52 lines
2.2 KiB
Python

"""오행 세력과 신강·신약. 자리마다 점수를 주고, 지지 점수는 지장간 날 수대로 나눈다."""
from dataclasses import dataclass
from services.saju.ganji import ELEMENTS, HIDDEN_DAYS, HIDDEN_STEMS, stem_element
from services.saju.pillars import Chart
# 자리별 점수. 득령(월지) 30 · 득지(일지) 20 · 나머지 10. 일간을 뺀 일곱 자리 합이 100
POSITION_POINTS = {
("year", "stem"): 10, ("year", "branch"): 10, ("month", "stem"): 10, ("month", "branch"): 30,
("day", "stem"): 10, ("day", "branch"): 20, ("hour", "stem"): 10, ("hour", "branch"): 10,
}
STRONG_FROM = 0.5 # 일간을 뺀 점수 중 비겁·인성 몫이 이 이상이면 신강
@dataclass(frozen=True)
class Strength:
elements: dict[str, float] # 오행별 점수 (일간 포함), 木火土金水 순
top: str # 가장 센 오행
support: float # 일간을 뺀 점수 중 일간을 돕는 오행(같은 오행·생해 주는 오행) 몫 0~1
strong: bool
def char_points(chart: Chart) -> list[tuple[str, str, str, float]]:
"""(자리, stem|branch, 천간, 점수). 지지는 지장간 천간들로 나눈다."""
out = []
for pos, p in chart.pillars().items():
out.append((pos, "stem", p.stem, float(POSITION_POINTS[(pos, "stem")])))
points = POSITION_POINTS[(pos, "branch")]
days = HIDDEN_DAYS[p.branch]
for stem, d in zip(HIDDEN_STEMS[p.branch], days):
if stem and d:
out.append((pos, "branch", stem, points * d / sum(days)))
return out
def chart_strength(chart: Chart) -> Strength:
me = ELEMENTS.index(stem_element(chart.day_stem))
helpers = {ELEMENTS[me], ELEMENTS[(me - 1) % 5]}
elements = dict.fromkeys(ELEMENTS, 0.0)
support = others = 0.0
for pos, part, stem, points in char_points(chart):
el = stem_element(stem)
elements[el] += points
if (pos, part) == ("day", "stem"):
continue
others += points
support += points if el in helpers else 0.0
elements = {k: round(v, 1) for k, v in elements.items()}
ratio = round(support / others, 3)
return Strength(elements, max(elements, key=elements.get), ratio, ratio >= STRONG_FROM)