fix: 사주 조후를 월령 기준으로 보정 — 여름 흙은 마른 흙, 未 0.9, 오행 세력·신강신약 추가, 계산식 문서
This commit is contained in:
1 parent
08a2e9f907
commit
63db0b725b
6 files changed
+202
-13
No files matched your search
@@ -4,6 +4,7 @@ from dataclasses import dataclass
|
||||
from typing import Iterable
|
||||
|
||||
from services.saju.pillars import Chart
|
||||
from services.saju.strength import POSITION_POINTS, Strength, chart_strength
|
||||
|
||||
# 글자별 (한난, 조습) 초안 — 주인 확인 필요. 한난: 차가움 −1 … 따뜻함 +1, 조습: 마름 −1 … 습함 +1
|
||||
# 丙丁巳午는 불이라 따뜻하고 말리며, 壬癸亥子는 물이라 차고 젖는다. 庚辛申酉는 가을 쇠라 서늘하고 마르고,
|
||||
@@ -18,17 +19,20 @@ CHAR_CLIMATE: dict[str, tuple[float, float]] = {
|
||||
"寅": (-0.3, 0.0), "卯": (-0.1, 0.2),
|
||||
"辰": (0.0, 0.6), # 봄 젖은 흙 (물 창고)
|
||||
"巳": (0.8, -0.3), "午": (1.0, -0.5),
|
||||
"未": (0.6, -0.7), # 여름 끝 마른 흙
|
||||
"未": (0.9, -0.7), # 대서가 든 한여름 마른 흙 (지장간 丁火)
|
||||
"申": (-0.3, -0.3), "酉": (-0.5, -0.5),
|
||||
"戌": (0.2, -0.8), # 가을 마른 흙 (불 창고)
|
||||
"亥": (-0.7, 0.8),
|
||||
}
|
||||
# 자리별 비중. 태어난 달(월령)이 계절을 정하므로 월지를 가장 크게
|
||||
CHART_WEIGHTS = {
|
||||
("year", "stem"): 1.0, ("year", "branch"): 1.0, ("month", "stem"): 1.0, ("month", "branch"): 3.0,
|
||||
("day", "stem"): 1.0, ("day", "branch"): 1.5, ("hour", "stem"): 1.0, ("hour", "branch"): 1.0,
|
||||
}
|
||||
SEASONS = {"寅卯辰": "봄", "巳午未": "여름", "申酉戌": "가을", "亥子丑": "겨울"}
|
||||
# 월령: 태어난 달이 다른 글자를 데우거나 식힌다 (『궁통보감』 조후). 달별 한난, 계절별 조습
|
||||
MONTH_TEMP = {"寅": -0.4, "卯": -0.1, "辰": 0.2, "巳": 0.6, "午": 0.9, "未": 1.0, "申": 0.4, "酉": -0.1, "戌": -0.3, "亥": -0.6, "子": -0.9, "丑": -1.0}
|
||||
SEASON_MOIST = {"봄": 0.3, "여름": -1.0, "가을": -0.5, "겨울": 1.0}
|
||||
# 월령을 받는 세기. 흙(戊己辰戌丑未)은 계절 따라 마른 흙·젖은 흙이 되므로 더 크게 받는다 (三夏己土 取癸為要, 六月土燥)
|
||||
SEASON_PULL = {"earth": {"temp": 0.5, "moist": 0.8}, "other": {"temp": 0.3, "moist": 0.2}}
|
||||
EARTH = set("戊己辰戌丑未")
|
||||
# 축이 균형(±balanced 안)일 때 이름(따뜻/서늘, 촉촉/마름)을 정할 기울기: 가장 센 오행. 토와 목은 월령을 따른다
|
||||
ELEMENT_LEAN = {"火": (1.0, -0.5), "金": (-0.5, -0.5), "水": (-0.7, 0.9)}
|
||||
|
||||
# 날씨 → 같은 두 축. 한난: 체감 15°C 가 0, 15°C 차이마다 ±1. 조습: 습도 55% 가 0, 35%p 차이마다 ±1, 비가 오면 최소 +0.8
|
||||
WEATHER_SCALE = {"neutral_c": 15.0, "span_c": 15.0, "neutral_humidity": 55.0, "span_humidity": 35.0, "rain_moist": 0.8}
|
||||
@@ -60,8 +64,9 @@ class ChartClimate:
|
||||
moist: float
|
||||
label: str # 따뜻·마름 / 따뜻·촉촉 / 서늘·마름 / 서늘·촉촉
|
||||
season: str # 월지 계절
|
||||
strength: Strength
|
||||
need: dict[str, str] # {"temp": warm|cool|mild, "moist": dry|moist|mild}
|
||||
parts: tuple[dict, ...] # 글자별 기여 {"position", "part", "char", "temp", "moist", "weight"}
|
||||
parts: tuple[dict, ...] # 글자별 기여 {"position", "part", "char", "temp", "moist", "weight"} (월령 보정 뒤 값)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -85,12 +90,30 @@ def _clamp(x: float, lo: float = -1.0, hi: float = 1.0) -> float:
|
||||
return max(lo, min(hi, x))
|
||||
|
||||
|
||||
def season_of(branch: str) -> str:
|
||||
return next(name for group, name in SEASONS.items() if branch in group)
|
||||
|
||||
|
||||
def seasoned(char: str, month_branch: str) -> tuple[float, float]:
|
||||
"""월령을 받은 글자의 (한난, 조습). 월지 자신은 계절 그 자체라 그대로 둔다."""
|
||||
t, m = CHAR_CLIMATE[char]
|
||||
pull = SEASON_PULL["earth" if char in EARTH else "other"]
|
||||
t += pull["temp"] * MONTH_TEMP[month_branch]
|
||||
m += pull["moist"] * SEASON_MOIST[season_of(month_branch)]
|
||||
return round(_clamp(t), 3), round(_clamp(m), 3)
|
||||
|
||||
|
||||
def _lean(strength: Strength, month_branch: str) -> tuple[float, float]:
|
||||
return ELEMENT_LEAN.get(strength.top) or CHAR_CLIMATE[month_branch]
|
||||
|
||||
|
||||
def chart_climate(chart: Chart) -> ChartClimate:
|
||||
mb = chart.month.branch
|
||||
parts = []
|
||||
for pos, p in chart.pillars().items():
|
||||
for part, char in (("stem", p.stem), ("branch", p.branch)):
|
||||
t, m = CHAR_CLIMATE[char]
|
||||
parts.append({"position": pos, "part": part, "char": char, "temp": t, "moist": m, "weight": CHART_WEIGHTS[(pos, part)]})
|
||||
t, m = CHAR_CLIMATE[char] if (pos, part) == ("month", "branch") else seasoned(char, mb)
|
||||
parts.append({"position": pos, "part": part, "char": char, "temp": t, "moist": m, "weight": POSITION_POINTS[(pos, part)]})
|
||||
total = sum(x["weight"] for x in parts)
|
||||
temp = round(sum(x["temp"] * x["weight"] for x in parts) / total, 3)
|
||||
moist = round(sum(x["moist"] * x["weight"] for x in parts) / total, 3)
|
||||
@@ -99,9 +122,12 @@ def chart_climate(chart: Chart) -> ChartClimate:
|
||||
"temp": "warm" if temp < -b else "cool" if temp > b else "mild",
|
||||
"moist": "dry" if moist > b else "moist" if moist < -b else "mild",
|
||||
}
|
||||
label = f"{'따뜻' if temp >= 0 else '서늘'}·{'촉촉' if moist >= 0 else '마름'}"
|
||||
season = next(name for group, name in SEASONS.items() if chart.month.branch in group)
|
||||
return ChartClimate(temp, moist, label, season, need, tuple(parts))
|
||||
strength = chart_strength(chart)
|
||||
lean_t, lean_m = _lean(strength, mb)
|
||||
warm = temp > 0 if abs(temp) > b else lean_t >= 0
|
||||
wet = moist > 0 if abs(moist) > b else lean_m >= 0
|
||||
label = f"{'따뜻' if warm else '서늘'}·{'촉촉' if wet else '마름'}"
|
||||
return ChartClimate(temp, moist, label, season_of(mb), strength, need, tuple(parts))
|
||||
|
||||
|
||||
def weather_vector(w: HourWeather) -> dict[str, float]:
|
||||
|
||||
@@ -25,6 +25,11 @@ HIDDEN_STEMS: dict[str, tuple[str, Optional[str], str]] = {
|
||||
"戌": ("辛", "丁", "戊"),
|
||||
"亥": ("戊", "甲", "壬"),
|
||||
}
|
||||
# 지장간이 맡는 날 수 (여기, 중기, 정기), 한 달 30일. 子卯酉는 중기 없이 10·20, 午는 丙10·己9·丁11
|
||||
HIDDEN_DAYS: dict[str, tuple[int, int, int]] = {
|
||||
"子": (10, 0, 20), "丑": (9, 3, 18), "寅": (7, 7, 16), "卯": (10, 0, 20), "辰": (9, 3, 18), "巳": (7, 7, 16),
|
||||
"午": (10, 9, 11), "未": (9, 3, 18), "申": (7, 7, 16), "酉": (10, 0, 20), "戌": (9, 3, 18), "亥": (7, 7, 16),
|
||||
}
|
||||
|
||||
|
||||
def stem_element(stem: str) -> str:
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""오행 세력과 신강·신약. 자리마다 점수를 주고, 지지 점수는 지장간 날 수대로 나눈다."""
|
||||
|
||||
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)
|
||||
@@ -4,13 +4,14 @@ import pytest
|
||||
|
||||
from common.utils.kma_time import KST
|
||||
from services.saju.birth import to_solar_time
|
||||
from services.saju.climate import HourWeather, axis_effect, chart_climate, climate_match
|
||||
from services.saju.climate import HourWeather, axis_effect, chart_climate, climate_match, seasoned
|
||||
from services.saju.daily import SCORE_WEIGHTS, daily_facts
|
||||
from services.saju.pillars import day_pillar, make_chart
|
||||
from services.saju.rules import (
|
||||
branch_relations, branch_ten_god, chart_ten_gods, dohwa_of, hongyeom_of, stem_relations, ten_god, triple_relations,
|
||||
)
|
||||
from services.saju.solar_terms import JEOL, jeol_of_year, nutation_longitude, sun_apparent_longitude
|
||||
from services.saju.strength import chart_strength
|
||||
|
||||
EXAMPLE = make_chart(date(1995, 3, 14))
|
||||
|
||||
@@ -254,6 +255,24 @@ def test_example_chart_climate_and_best_window():
|
||||
assert moist == {"axis": "moist", "need": "dry", "effect": "dries", "helps": True, "points": moist["points"], "weather": "체감 13°, 습도 40%"}
|
||||
|
||||
|
||||
|
||||
def test_example_strength_counts_hidden_stems_and_month_branch():
|
||||
s = chart_strength(EXAMPLE) # 乙亥 己卯 甲辰
|
||||
assert s.elements == {"木": 58.3, "火": 0.0, "土": 24.3, "金": 0.0, "水": 7.3}
|
||||
assert s.top == "木" and s.strong and s.support == 0.696
|
||||
|
||||
|
||||
def test_summer_earth_chart_reads_hot_and_dry_not_rainy():
|
||||
chart = make_chart(date(1959, 7, 16)) # 己亥 辛未 己亥: 未월 토가 많고 물이 둘
|
||||
climate = chart_climate(chart)
|
||||
assert climate.season == "여름" and climate.label == "따뜻·마름" and climate.need["temp"] == "cool"
|
||||
assert climate.strength.top == "土" and climate.strength.strong
|
||||
|
||||
|
||||
def test_month_dries_summer_earth_and_soaks_winter_earth():
|
||||
assert seasoned("己", "未")[1] < 0 < seasoned("己", "子")[1]
|
||||
assert seasoned("壬", "未")[0] > seasoned("壬", "子")[0]
|
||||
|
||||
def test_balanced_axis_prefers_mild_weather_and_rain_moistens():
|
||||
assert axis_effect(0.0, 0.0) > axis_effect(0.0, 0.6) and axis_effect(0.0, 0.0) > axis_effect(0.0, -0.6)
|
||||
assert axis_effect(-0.4, 0.8) > axis_effect(-0.4, 0.0) > axis_effect(-0.4, -0.8)
|
||||
|
||||
Reference in new issue
Block a user