feat: 지수 축 점수를 만점 구간 밖에서 완만한 곡선으로, 기온·습도는 최적값 근처 100점

This commit is contained in:
skkim committed 2026-10-07 15:31:38 +09:00
1 parent 9b3c17fcb2
commit 3c8e8984af
5 files changed
+56 -29

No files matched your search

+33 -13
View File
@@ -1,7 +1,7 @@
from collections import Counter
from dataclasses import dataclass, field
from datetime import date
from math import cos, pi
from math import cos, exp, pi
from statistics import mean
from typing import Optional
@@ -14,6 +14,10 @@ _WEEKDAY_KO = ("월", "화", "수", "목", "금", "토", "일")
_UNKNOWN = 70
_EDGE = 85
_WORST_PULL = 0.25
_FALL_FLOOR = exp(-0.5)
# 편한 범위 안에서 최적값 ±이만큼은 100점 (기온 °, 습도 %p)
_TEMP_PLATEAU = 2
_HUMIDITY_PLATEAU = 5
@dataclass(frozen=True)
@@ -70,17 +74,33 @@ def _clamp(v: float) -> int:
return round(max(0.0, min(100.0, v)))
def _fall(t: float) -> float:
"""만점 구간 끝(t=0)에서 0점 지점(t=1)까지 1→0. 정규분포 곡선(σ = 그 거리) 모양이라 만점 근처는 천천히 내려간다."""
if t <= 0:
return 1.0
if t >= 1:
return 0.0
return (exp(-t * t / 2) - _FALL_FLOOR) / (1 - _FALL_FLOOR)
def _linear(v: float, zero: float, full: float) -> int:
return _clamp((v - zero) / (full - zero) * 100)
"""full 쪽은 100, zero 에서 0. 사이는 _fall 곡선."""
return _clamp(100 * _fall((v - full) / (zero - full)))
def _peak(v: float, lo: float, hi: float, best: Optional[float], per_out: float) -> int:
"""최적값에서만 100. 편한 범위 끝은 _EDGE, 범위 밖은 한 단위마다 per_out 씩 깎는다."""
best = (lo + hi) / 2 if best is None else best
def _range(v: float, lo: float, hi: float, best: Optional[float], plateau: float, per_out: float) -> int:
"""편한 범위 안: 최적값 ±plateau 는 100, 범위 끝으로 갈수록 _EDGE 까지 _fall 곡선으로 내려간다.
100점 구간은 최적값~범위 끝 거리의 1/3 을 넘지 않아 범위가 좁아도 끝에서 뚝 떨어지지 않는다.
범위 밖: _EDGE 에서 예전 직선(한 단위마다 per_out)이 0이 되던 거리까지 _fall 곡선으로 0."""
if v < lo or v > hi:
return _clamp(_EDGE - per_out * min(abs(v - lo), abs(v - hi)))
side = (best - lo) if v < best else (hi - best)
return _clamp(100 - (100 - _EDGE) * (abs(v - best) / side if side else 0))
return _clamp(_EDGE * _fall(min(abs(v - lo), abs(v - hi)) * per_out / _EDGE))
best = (lo + hi) / 2 if best is None else best
side = abs((lo if v < best else hi) - best)
flat = min(plateau, side / 3)
off = abs(v - best) - flat
if off <= 0:
return 100
return _clamp(_EDGE + (100 - _EDGE) * _fall(off / (side - flat)))
def temp_score(rule: dict, feels: float) -> tuple[int, str]:
@@ -88,7 +108,7 @@ def temp_score(rule: dict, feels: float) -> tuple[int, str]:
shown = f"산 정상 기준 {round(t)}°" if rule.get("offset") else f"체감 {round(t)}°"
if rule["type"] == "range":
lo, hi = rule["lo"], rule["hi"]
return _peak(t, lo, hi, rule.get("best"), 8), f"{shown}, 편한 범위 {lo}~{hi}° {'안' if lo <= t <= hi else '밖'}"
return _range(t, lo, hi, rule.get("best"), _TEMP_PLATEAU, 8), f"{shown}, 편한 범위 {lo}~{hi}° {'안' if lo <= t <= hi else '밖'}"
if rule["type"] == "away":
lo, hi = rule["lo"], rule["hi"]
dist = 0 if lo <= t <= hi else min(abs(t - lo), abs(t - hi))
@@ -99,8 +119,8 @@ def temp_score(rule: dict, feels: float) -> tuple[int, str]:
def rain_score(rule: dict, max_prob: int, raining: bool, until: str = "") -> tuple[int, str]:
prob = f"{until} 비 확률 {max_prob}%" if until else f"비 확률 {max_prob}%"
if rule["dir"] == "wet":
return (100 if raining else _clamp(max_prob)), (f"비 소식, {prob}" if raining else f"{prob}, 비 오면 올라가요")
return (0 if raining else _clamp(100 - max_prob)), (f"비 소식, {prob}" if raining else prob)
return (100 if raining else _linear(max_prob, 0, 100)), (f"비 소식, {prob}" if raining else f"{prob}, 비 오면 올라가요")
return (0 if raining else _linear(max_prob, 100, 0)), (f"비 소식, {prob}" if raining else prob)
def wind_score(rule: dict, wind: Optional[float]) -> tuple[int, str]:
@@ -115,7 +135,7 @@ def wind_score(rule: dict, wind: Optional[float]) -> tuple[int, str]:
return 100, note
if wind <= ok:
return _clamp(100 - (100 - _EDGE) * (wind - ok / 2) / (ok / 2)), note
return _clamp(_EDGE * (rule["limit"] - wind) / (rule["limit"] - ok)), note
return _clamp(_EDGE * _fall((wind - ok) / (rule["limit"] - ok))), note
if rule["type"] == "moderate":
if 2 <= wind <= 5:
return 100, note
@@ -126,7 +146,7 @@ def wind_score(rule: dict, wind: Optional[float]) -> tuple[int, str]:
def humidity_score(rule: dict, humidity: float) -> tuple[int, str]:
note = f"습도 {round(humidity)}%"
if rule["type"] == "range":
return _peak(humidity, rule["lo"], rule["hi"], rule.get("best"), 2), note
return _range(humidity, rule["lo"], rule["hi"], rule.get("best"), _HUMIDITY_PLATEAU, 2), note
if rule["type"] == "higher":
return _linear(humidity, 40, 90), note
if rule["type"] == "lower":