11from datetime import datetime , date
22from pathlib import Path
33from typing import List , Tuple , Dict
4+ from collections import Counter
45
56from src .four_pillars .entities .schemas import FourPillar
7+ from src .four_pillars .entities .enums import FiveElements
68
79
810class FourPillarsCalculator :
@@ -14,6 +16,39 @@ class FourPillarsCalculator:
1416 def __init__ (self ):
1517 self ._init_kanshi_data ()
1618 self ._init_setsuiri_data ()
19+ self ._init_five_elements_mapping ()
20+
21+ def _init_five_elements_mapping (self ):
22+ """천간과 지지의 오행 매핑 초기화"""
23+ # 천간 오행 매핑
24+ self .jikkan_five_elements = {
25+ "甲" : FiveElements .WOOD , # 갑 - 목
26+ "乙" : FiveElements .WOOD , # 을 - 목
27+ "丙" : FiveElements .FIRE , # 병 - 화
28+ "丁" : FiveElements .FIRE , # 정 - 화
29+ "戊" : FiveElements .EARTH , # 무 - 토
30+ "己" : FiveElements .EARTH , # 기 - 토
31+ "庚" : FiveElements .METAL , # 경 - 금
32+ "辛" : FiveElements .METAL , # 신 - 금
33+ "壬" : FiveElements .WATER , # 임 - 수
34+ "癸" : FiveElements .WATER , # 계 - 수
35+ }
36+
37+ # 지지 오행 매핑
38+ self .jyunishi_five_elements = {
39+ "寅" : FiveElements .WOOD , # 인 - 목
40+ "卯" : FiveElements .WOOD , # 묘 - 목
41+ "巳" : FiveElements .FIRE , # 사 - 화
42+ "午" : FiveElements .FIRE , # 오 - 화
43+ "辰" : FiveElements .EARTH , # 진 - 토
44+ "戌" : FiveElements .EARTH , # 술 - 토
45+ "丑" : FiveElements .EARTH , # 축 - 토
46+ "未" : FiveElements .EARTH , # 미 - 토
47+ "申" : FiveElements .METAL , # 신 - 금
48+ "酉" : FiveElements .METAL , # 유 - 금
49+ "亥" : FiveElements .WATER , # 해 - 수
50+ "子" : FiveElements .WATER , # 자 - 수
51+ }
1752
1853 def _init_kanshi_data (self ):
1954 """60간지 배열과 해시 초기화"""
@@ -115,6 +150,40 @@ def _calculate_kanshi(
115150
116151 return [year_pillar , month_pillar , day_pillar , time_pillar ]
117152
153+ def _analyze_five_elements (self , pillars : List [str ]) -> Tuple [List [FiveElements ], List [FiveElements ]]:
154+ """사주팔자의 오행 분석"""
155+ element_counts = Counter ()
156+
157+ # 모든 오행을 0으로 초기화
158+ all_elements = [FiveElements .WOOD , FiveElements .FIRE , FiveElements .EARTH , FiveElements .METAL , FiveElements .WATER ]
159+ for element in all_elements :
160+ element_counts [element ] = 0
161+
162+ for pillar in pillars :
163+ if pillar is None :
164+ continue
165+
166+ # 천간과 지지 분리
167+ jikkan = pillar [0 ] # 첫 번째 글자는 천간
168+ jyunishi = pillar [1 ] # 두 번째 글자는 지지
169+
170+ # 천간 오행 추가
171+ if jikkan in self .jikkan_five_elements :
172+ element_counts [self .jikkan_five_elements [jikkan ]] += 1
173+
174+ # 지지 오행 추가
175+ if jyunishi in self .jyunishi_five_elements :
176+ element_counts [self .jyunishi_five_elements [jyunishi ]] += 1
177+
178+ # 가장 많은 오행과 가장 적은 오행 찾기
179+ max_count = max (element_counts .values ())
180+ min_count = min (element_counts .values ())
181+
182+ strong_elements = [element for element , count in element_counts .items () if count == max_count ]
183+ weak_elements = [element for element , count in element_counts .items () if count == min_count ]
184+
185+ return strong_elements , weak_elements
186+
118187 def calculate_four_pillars (self , birth_date : datetime ) -> FourPillar :
119188 """사주 계산 메인 함수"""
120189 year = birth_date .year
@@ -129,13 +198,21 @@ def calculate_four_pillars(self, birth_date: datetime) -> FourPillar:
129198 minute = None
130199
131200 pillars = self ._calculate_kanshi (year , month , day , hour , minute )
201+
202+ # 오행 분석
203+ strong_elements , weak_elements = self ._analyze_five_elements (pillars )
204+
205+ # FiveElements enum을 문자열로 변환
206+ strong_elements_str = [element .value for element in strong_elements ] if strong_elements else None
207+ weak_elements_str = [element .value for element in weak_elements ] if weak_elements else None
208+
132209 result : FourPillar = {
133210 "year_pillar" : pillars [0 ], # 년주
134211 "month_pillar" : pillars [1 ], # 월주
135212 "day_pillar" : pillars [2 ], # 일주
213+ "time_pillar" : pillars [3 ], # 시주
214+ "strong_elements" : strong_elements_str ,
215+ "weak_elements" : weak_elements_str
136216 }
137217
138- if pillars [3 ] is not None :
139- result ["time_pillar" ] = pillars [3 ]
140-
141218 return result
0 commit comments