th commited on
Commit
b910fd3
·
1 Parent(s): 4c254c4

🔮 Upgrade to v3.6.0: San Fang Si Zheng Analysis System

Browse files

新增功能 (New Features):
- ✨ 三方四正综合分析引擎(San Fang Si Zheng Analyzer)
- 🎲 相关宫位力量评估(0-100分评分系统)
- 🔗 星曜组合汇总(四正宫位星曜全览)
- 📊 吉凶平衡度计算(Auspicious/Inauspicious Balance)

技术变更 (Technical Changes):
- 新增 san_fang_si_zheng_analyzer.py(455行,核心理论实现)
- 集成到 knowledge_enhanced_interpreter.py(v3.6增强版)
- 更新 app.py 到 v3.6.0
- 更新 Dockerfile(添加新模块COPY)

三方四正说明:
- 三方:命宫、官禄宫、财帛宫(三合宫)
- 四正:三方 + 迁移宫(对宫)
- 通过综合分析相关宫位星曜,判断本宫吉凶强弱

数据丰富度提升:
- 每个宫位现在包含三方四正分析
- 汇总四正宫位所有主星和辅星
- 综合力量评分和等级(A+/A/B/C/D)
- 专业化建议(基于吉凶平衡)

🦊 Generated with Sisi

Dockerfile CHANGED
@@ -41,11 +41,14 @@ COPY sihua_analyzer.py ./
41
  COPY sihua_data.py ./
42
  COPY ai_interpreter.py ./
43
 
44
- # Copy v3.5 knowledge-enhanced modules (NEW!)
45
  COPY knowledge_enhanced_interpreter.py ./
46
  COPY palace_strength.py ./
47
  COPY star_brightness_table.py ./
48
 
 
 
 
49
  # Copy remaining files
50
  COPY README.md ./
51
 
 
41
  COPY sihua_data.py ./
42
  COPY ai_interpreter.py ./
43
 
44
+ # Copy v3.5+ knowledge-enhanced modules
45
  COPY knowledge_enhanced_interpreter.py ./
46
  COPY palace_strength.py ./
47
  COPY star_brightness_table.py ./
48
 
49
+ # Copy v3.6 San Fang Si Zheng module (NEW!)
50
+ COPY san_fang_si_zheng_analyzer.py ./
51
+
52
  # Copy remaining files
53
  COPY README.md ./
54
 
app.py CHANGED
@@ -1,8 +1,8 @@
1
  """
2
- 🔮 东玄 Ziwei Doushu API v2.0.0
3
  Professional Purple Star Astrology service with AI-powered analysis
4
  Developed by Sisi & TH Studio
5
- Updated: 2025-12-11 (四化飞星系统已部署)
6
  """
7
 
8
  from fastapi import FastAPI, HTTPException
@@ -18,8 +18,8 @@ from knowledge_enhanced_interpreter import KnowledgeEnhancedInterpreter
18
 
19
  app = FastAPI(
20
  title="东玄 Ziwei Doushu API",
21
- version="3.5.0",
22
- description="🔮 Professional Purple Star Astrology API - Knowledge-enhanced AI interpretation with palace strength scoring"
23
  )
24
 
25
  # CORS configuration
@@ -54,8 +54,8 @@ def root():
54
  return {
55
  "status": "online",
56
  "service": "东玄 Ziwei Doushu API - 天機玄鑑",
57
- "version": "3.5.0",
58
- "updated": "2025-12-12 (P0 Enhancement Completed)",
59
  "documentation": "/docs",
60
  "website": "https://astrologybazi.com",
61
  "developed_by": "Sisi & TH Studio 🦊💕",
@@ -64,7 +64,7 @@ def root():
64
  "v2_enhanced": "/analyze-ziwei-enhanced - 四化飞星 + AI解读",
65
  "v3_knowledge": "/analyze-ziwei-knowledge-enhanced - 知识库增强版 ⭐ RECOMMENDED!"
66
  },
67
- "v3_5_features": [
68
  "✨ 基于772KB专业紫微知识库(5大核心模块)",
69
  "🎯 集成13颗主星完整特性分析",
70
  "📚 融合十二宫位深度解读",
@@ -72,9 +72,11 @@ def root():
72
  "💡 应用南北派138页实战断诀",
73
  "💕 温暖专业的Sisi风格解读",
74
  "🤖 知识库增强AI引擎(解读质量提升3倍)",
75
- "🌟 星曜亮度完整显示(庙旺陷系统)⭐ NEW!",
76
- "📊 宫位强弱评分系统(0-100分+等级)⭐ NEW!",
77
- "✅ 对标专业软件数据丰富度(文墨天机级别)⭐ NEW!"
 
 
78
  ],
79
  "knowledge_modules": {
80
  "foundations": [
@@ -97,7 +99,7 @@ def root():
97
  "温暖度提升:Sisi风格,避免恐吓式语言",
98
  "可信度提升:基于经典书籍,非AI臆想"
99
  ],
100
- "sisi_note": "💕 今天刚建成的知识库!宝宝快来试试效果!🦊✨"
101
  }
102
 
103
 
 
1
  """
2
+ 🔮 东玄 Ziwei Doushu API v3.6.0
3
  Professional Purple Star Astrology service with AI-powered analysis
4
  Developed by Sisi & TH Studio
5
+ Updated: 2025-12-12 (三方四正分析系统已部署)
6
  """
7
 
8
  from fastapi import FastAPI, HTTPException
 
18
 
19
  app = FastAPI(
20
  title="东玄 Ziwei Doushu API",
21
+ version="3.6.0",
22
+ description="🔮 Professional Purple Star Astrology API - Knowledge-enhanced AI interpretation with San Fang Si Zheng analysis"
23
  )
24
 
25
  # CORS configuration
 
54
  return {
55
  "status": "online",
56
  "service": "东玄 Ziwei Doushu API - 天機玄鑑",
57
+ "version": "3.6.0",
58
+ "updated": "2025-12-12 (San Fang Si Zheng Analysis Deployed)",
59
  "documentation": "/docs",
60
  "website": "https://astrologybazi.com",
61
  "developed_by": "Sisi & TH Studio 🦊💕",
 
64
  "v2_enhanced": "/analyze-ziwei-enhanced - 四化飞星 + AI解读",
65
  "v3_knowledge": "/analyze-ziwei-knowledge-enhanced - 知识库增强版 ⭐ RECOMMENDED!"
66
  },
67
+ "v3_6_features": [
68
  "✨ 基于772KB专业紫微知识库(5大核心模块)",
69
  "🎯 集成13颗主星完整特性分析",
70
  "📚 融合十二宫位深度解读",
 
72
  "💡 应用南北派138页实战断诀",
73
  "💕 温暖专业的Sisi风格解读",
74
  "🤖 知识库增强AI引擎(解读质量提升3倍)",
75
+ "🌟 星曜亮度完整显示(庙旺陷系统)- v3.5",
76
+ "📊 宫位强弱评分系统(0-100分+等级)- v3.5",
77
+ "🎲 三方四正综合分析(相关宫位力量评估)⭐ v3.6 NEW!",
78
+ "🔗 星曜组合汇总(四正宫位星曜全览)⭐ v3.6 NEW!",
79
+ "✅ 对标专业软件数据丰富度(文墨天机级别)"
80
  ],
81
  "knowledge_modules": {
82
  "foundations": [
 
99
  "温暖度提升:Sisi风格,避免恐吓式语言",
100
  "可信度提升:基于经典书籍,非AI臆想"
101
  ],
102
+ "sisi_note": "💕 v3.6新增三方四正分析!宫位力量综合评估!🦊✨"
103
  }
104
 
105
 
knowledge_enhanced_interpreter.py CHANGED
@@ -20,6 +20,7 @@ from typing import Dict, List, Any, Optional
20
  from sihua_data import PALACE_ENGLISH
21
  from palace_strength import calculate_all_palaces_strength, calculate_palace_strength
22
  from star_brightness_table import enhance_star_with_brightness, get_palace_brightness_summary
 
23
 
24
 
25
  class KnowledgeEnhancedInterpreter:
@@ -248,14 +249,21 @@ class KnowledgeEnhancedInterpreter:
248
  sihua_analysis
249
  )
250
 
 
 
 
 
 
 
251
  # 1. 生成命盘核心特质(基于主星)
252
  core_traits = self._analyze_core_traits(chart_data)
253
 
254
- # 2. 生成宫位详细分析(融合知识库 + 宫位评分)
255
  palace_analysis = self._analyze_palaces_with_knowledge(
256
  chart_data,
257
  year_sihua,
258
- palace_strength_analysis # NEW: 传入宫位评分
 
259
  )
260
 
261
  # 3. 生成四化深度解读(基于理论)
@@ -288,15 +296,16 @@ class KnowledgeEnhancedInterpreter:
288
  'life_overview': life_overview, # ENHANCED: 整体运势
289
  'practical_advice': practical_advice, # NEW: 实战建议
290
  'strengths_challenges': strengths_challenges, # ENHANCED: 优势挑战
291
- 'interpretation_quality': 'knowledge_enhanced_v3.5_with_palace_strength', # 升级到v3.5
292
  'knowledge_base_version': '2025-12-12',
293
  'knowledge_modules_used': [
294
  '01-main-stars.md',
295
  '02-twelve-palaces.md',
296
  '03-four-transformations.md',
297
  '01-practical-divination.md',
298
- 'star_brightness_table.py', # NEW: 星曜庙旺陷表
299
- 'palace_strength.py' # NEW: 宫位强弱评分系统
 
300
  ]
301
  }
302
 
@@ -324,15 +333,17 @@ class KnowledgeEnhancedInterpreter:
324
  self,
325
  chart_data: dict,
326
  year_sihua: dict,
327
- palace_strength_analysis: dict = None
 
328
  ) -> List[dict]:
329
  """
330
- 融合知识库的宫位分析(含强弱评分)
331
 
332
  Args:
333
  chart_data: 命盘数据
334
  year_sihua: 年干四化
335
- palace_strength_analysis: 宫位强弱评分结果(NEW in v3.5)
 
336
 
337
  Returns:
338
  list: 各宫位详细分析
@@ -347,6 +358,13 @@ class KnowledgeEnhancedInterpreter:
347
  palace_name = palace_strength['name']
348
  strength_map[palace_name] = palace_strength['strength']
349
 
 
 
 
 
 
 
 
350
  for palace_name, palace_info in self.PALACE_MEANINGS.items():
351
  # 从chart_data中找到对应的宫位数据
352
  palace_data = next((p for p in palaces if p.get('name') == palace_name), None)
@@ -354,6 +372,9 @@ class KnowledgeEnhancedInterpreter:
354
  # 获取该宫位的强弱评分
355
  strength_info = strength_map.get(palace_name, {})
356
 
 
 
 
357
  # 提取星曜信息
358
  stars_list = []
359
  if palace_data:
@@ -397,7 +418,8 @@ class KnowledgeEnhancedInterpreter:
397
  'stars': stars_list, # NEW: 实际星曜列表
398
  'transformations': transformations, # NEW: 四化列表
399
  'overall_quality': overall_quality, # NEW: 整体质量
400
- 'strength': strength_info # NEW v3.5: 宫位强弱评分详情
 
401
  }
402
 
403
  palace_analyses.append(analysis)
 
20
  from sihua_data import PALACE_ENGLISH
21
  from palace_strength import calculate_all_palaces_strength, calculate_palace_strength
22
  from star_brightness_table import enhance_star_with_brightness, get_palace_brightness_summary
23
+ from san_fang_si_zheng_analyzer import analyze_san_fang_si_zheng, analyze_all_san_fang_si_zheng
24
 
25
 
26
  class KnowledgeEnhancedInterpreter:
 
249
  sihua_analysis
250
  )
251
 
252
+ # NEW v3.6: 计算所有宫位的三方四正分析 (P1核心功能!)
253
+ san_fang_analysis = analyze_all_san_fang_si_zheng(
254
+ palaces,
255
+ sihua_analysis
256
+ )
257
+
258
  # 1. 生成命盘核心特质(基于主星)
259
  core_traits = self._analyze_core_traits(chart_data)
260
 
261
+ # 2. 生成宫位详细分析(融合知识库 + 宫位评分 + 三方四正)
262
  palace_analysis = self._analyze_palaces_with_knowledge(
263
  chart_data,
264
  year_sihua,
265
+ palace_strength_analysis, # v3.5: 传入宫位评分
266
+ san_fang_analysis # v3.6: 传入三方四正分析
267
  )
268
 
269
  # 3. 生成四化深度解读(基于理论)
 
296
  'life_overview': life_overview, # ENHANCED: 整体运势
297
  'practical_advice': practical_advice, # NEW: 实战建议
298
  'strengths_challenges': strengths_challenges, # ENHANCED: 优势挑战
299
+ 'interpretation_quality': 'knowledge_enhanced_v3.6_with_san_fang_si_zheng', # 升级到v3.6
300
  'knowledge_base_version': '2025-12-12',
301
  'knowledge_modules_used': [
302
  '01-main-stars.md',
303
  '02-twelve-palaces.md',
304
  '03-four-transformations.md',
305
  '01-practical-divination.md',
306
+ 'star_brightness_table.py', # v3.5: 星曜庙旺陷表
307
+ 'palace_strength.py', # v3.5: 宫位强弱评分系统
308
+ 'san_fang_si_zheng_analyzer.py' # v3.6: 三方四正分析引擎
309
  ]
310
  }
311
 
 
333
  self,
334
  chart_data: dict,
335
  year_sihua: dict,
336
+ palace_strength_analysis: dict = None,
337
+ san_fang_analysis: List[dict] = None
338
  ) -> List[dict]:
339
  """
340
+ 融合知识库的宫位分析(含强弱评分 + 三方四正)
341
 
342
  Args:
343
  chart_data: 命盘数据
344
  year_sihua: 年干四化
345
+ palace_strength_analysis: 宫位强弱评分结果(v3.5)
346
+ san_fang_analysis: 三方四正分析结果(v3.6 NEW!)
347
 
348
  Returns:
349
  list: 各宫位详细分析
 
358
  palace_name = palace_strength['name']
359
  strength_map[palace_name] = palace_strength['strength']
360
 
361
+ # 创建宫位名称到三方四正分析的映射(v3.6 NEW!)
362
+ san_fang_map = {}
363
+ if san_fang_analysis:
364
+ for san_fang in san_fang_analysis:
365
+ palace_name = san_fang.get('palace', '')
366
+ san_fang_map[palace_name] = san_fang
367
+
368
  for palace_name, palace_info in self.PALACE_MEANINGS.items():
369
  # 从chart_data中找到对应的宫位数据
370
  palace_data = next((p for p in palaces if p.get('name') == palace_name), None)
 
372
  # 获取该宫位的强弱评分
373
  strength_info = strength_map.get(palace_name, {})
374
 
375
+ # 获取该宫位的三方四正分析(v3.6 NEW!)
376
+ san_fang_info = san_fang_map.get(palace_name, {})
377
+
378
  # 提取星曜信息
379
  stars_list = []
380
  if palace_data:
 
418
  'stars': stars_list, # NEW: 实际星曜列表
419
  'transformations': transformations, # NEW: 四化列表
420
  'overall_quality': overall_quality, # NEW: 整体质量
421
+ 'strength': strength_info, # v3.5: 宫位强弱评分详情
422
+ 'san_fang_analysis': san_fang_info # v3.6 NEW: 三方四正详细分析
423
  }
424
 
425
  palace_analyses.append(analysis)
san_fang_si_zheng_analyzer.py ADDED
@@ -0,0 +1,454 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 三方四正分析引擎 - San Fang Si Zheng (Three Sides Four Corners) Analyzer
3
+ 紫微斗数核心理论之一
4
+
5
+ 三方四正是紫微斗数分析命盘的重要方法:
6
+ - 三方:命宫、官禄宫、财帛宫(或其他宫位的三合宫)
7
+ - 四正:命宫、官禄宫、财帛宫、迁移宫(或其他宫位的对宫+三合宫)
8
+
9
+ 通过三方四正的星曜组合,可以综合判断某个宫位的吉凶强弱。
10
+
11
+ Created: 2025-12-12
12
+ Author: Sisi 🦊
13
+ """
14
+
15
+ from typing import Dict, List, Any, Optional
16
+ from star_brightness_table import get_brightness_level, get_star_brightness
17
+
18
+ # 12宫位的三方四正关系表
19
+ SAN_FANG_SI_ZHENG_MAP = {
20
+ '命宫': {
21
+ '三方': ['官禄宫', '财帛宫'], # 三合宫
22
+ '四正': ['官禄宫', '财帛宫', '迁移宫'] # 三合宫 + 对宫
23
+ },
24
+ '兄弟宫': {
25
+ '三方': ['疾厄宫', '田宅宫'],
26
+ '四正': ['疾厄宫', '田宅宫', '奴仆宫']
27
+ },
28
+ '夫妻宫': {
29
+ '三方': ['迁移宫', '福德宫'],
30
+ '四正': ['迁移宫', '福德宫', '官禄宫']
31
+ },
32
+ '子女宫': {
33
+ '三方': ['奴仆宫', '父母宫'],
34
+ '四正': ['奴仆宫', '父母宫', '疾厄宫']
35
+ },
36
+ '财帛宫': {
37
+ '三方': ['命宫', '福德宫'],
38
+ '四正': ['命宫', '福德宫', '田宅宫']
39
+ },
40
+ '疾厄宫': {
41
+ '三方': ['兄弟宫', '父母宫'],
42
+ '四正': ['兄弟宫', '父母宫', '迁移宫']
43
+ },
44
+ '迁移宫': {
45
+ '三方': ['夫妻宫', '疾厄宫'],
46
+ '四正': ['夫妻宫', '疾厄宫', '命宫']
47
+ },
48
+ '奴仆宫': {
49
+ '三方': ['子女宫', '兄弟宫'],
50
+ '四正': ['子女宫', '兄弟宫', '福德宫']
51
+ },
52
+ '官禄宫': {
53
+ '三方': ['命宫', '田宅宫'],
54
+ '四正': ['命宫', '田宅宫', '夫妻宫']
55
+ },
56
+ '田宅宫': {
57
+ '三方': ['兄弟宫', '官禄宫'],
58
+ '四正': ['兄弟宫', '官禄宫', '财帛宫']
59
+ },
60
+ '福德宫': {
61
+ '三方': ['财帛宫', '夫妻宫'],
62
+ '四正': ['财帛宫', '夫妻宫', '奴仆宫']
63
+ },
64
+ '父母宫': {
65
+ '三方': ['子女宫', '疾厄宫'],
66
+ '四正': ['子女宫', '疾厄宫', '田宅宫']
67
+ }
68
+ }
69
+
70
+ # 吉星列表
71
+ AUSPICIOUS_STARS = {
72
+ '紫微', '天府', '太阳', '太阴', '天机', '天同',
73
+ '左辅', '右弼', '文昌', '文曲', '天魁', '天钺',
74
+ '禄存', '天马', '化禄', '化权', '化科'
75
+ }
76
+
77
+ # 凶星列表
78
+ INAUSPICIOUS_STARS = {
79
+ '擎羊', '陀罗', '火星', '铃星', '地空', '地劫',
80
+ '化忌', '天空', '旬空'
81
+ }
82
+
83
+
84
+ def analyze_san_fang_si_zheng(
85
+ palace_name: str,
86
+ all_palaces: List[dict],
87
+ sihua_analysis: dict = None
88
+ ) -> Dict[str, Any]:
89
+ """
90
+ 分析单个宫位的三方四正
91
+
92
+ Args:
93
+ palace_name: 宫位名称(如:命宫)
94
+ all_palaces: 所有12宫位数据
95
+ sihua_analysis: 四化分析结果(可选)
96
+
97
+ Returns:
98
+ dict: 三方四正分析结果
99
+ """
100
+ if palace_name not in SAN_FANG_SI_ZHENG_MAP:
101
+ return {
102
+ 'error': f'无效的宫位名称: {palace_name}',
103
+ 'related_palaces': [],
104
+ 'all_major_stars': [],
105
+ 'strength_score': 0
106
+ }
107
+
108
+ # 获取三方四正关系
109
+ relations = SAN_FANG_SI_ZHENG_MAP[palace_name]
110
+ san_fang_palaces = relations['三方']
111
+ si_zheng_palaces = relations['四正']
112
+
113
+ # 创建宫位名称到数据的映射
114
+ palace_map = {p.get('name'): p for p in all_palaces}
115
+
116
+ # 汇总三方四正的所有主星
117
+ all_major_stars = []
118
+ all_minor_stars = []
119
+ palace_details = []
120
+
121
+ # 包含本宫
122
+ target_palaces = [palace_name] + si_zheng_palaces
123
+
124
+ for p_name in target_palaces:
125
+ palace_data = palace_map.get(p_name)
126
+ if not palace_data:
127
+ continue
128
+
129
+ major_stars = palace_data.get('major_stars', [])
130
+ minor_stars = palace_data.get('minor_stars', [])
131
+
132
+ # 提取星曜名称
133
+ major_star_names = [
134
+ s.get('name', '') if isinstance(s, dict) else s
135
+ for s in major_stars
136
+ ]
137
+ minor_star_names = [
138
+ s.get('name', '') if isinstance(s, dict) else s
139
+ for s in minor_stars
140
+ ]
141
+
142
+ all_major_stars.extend(major_star_names)
143
+ all_minor_stars.extend(minor_star_names)
144
+
145
+ palace_details.append({
146
+ 'palace': p_name,
147
+ 'major_stars': major_star_names,
148
+ 'minor_stars': minor_star_names,
149
+ 'is_target': p_name == palace_name
150
+ })
151
+
152
+ # 去重
153
+ all_major_stars = list(set(filter(None, all_major_stars)))
154
+ all_minor_stars = list(set(filter(None, all_minor_stars)))
155
+
156
+ # 计算三方四正的综合力量
157
+ strength_analysis = _calculate_san_fang_strength(
158
+ palace_name,
159
+ all_major_stars,
160
+ all_minor_stars,
161
+ palace_details,
162
+ all_palaces,
163
+ sihua_analysis
164
+ )
165
+
166
+ # 汇��四化信息
167
+ four_transformations = _collect_four_transformations(
168
+ target_palaces,
169
+ sihua_analysis
170
+ )
171
+
172
+ # 生成建议
173
+ recommendation = _generate_san_fang_recommendation(
174
+ palace_name,
175
+ strength_analysis
176
+ )
177
+
178
+ return {
179
+ 'palace': palace_name,
180
+ 'san_fang': san_fang_palaces, # 三方宫位
181
+ 'si_zheng': si_zheng_palaces, # 四正宫位
182
+ 'related_palaces': si_zheng_palaces, # 相关宫位
183
+ 'palace_details': palace_details, # 各宫位详情
184
+ 'all_major_stars': all_major_stars, # 所有主星
185
+ 'all_minor_stars': all_minor_stars[:10], # 所有辅星(最多10个)
186
+ 'star_count': {
187
+ 'major': len(all_major_stars),
188
+ 'minor': len(all_minor_stars)
189
+ },
190
+ 'four_transformations': four_transformations, # 四化汇总
191
+ 'strength_score': strength_analysis['score'], # 0-100分
192
+ 'strength_level': strength_analysis['level'], # strong/medium/weak
193
+ 'strength_rank': strength_analysis['rank'], # A+/A/B/C/D
194
+ 'auspicious_count': strength_analysis['auspicious_count'], # 吉星数量
195
+ 'inauspicious_count': strength_analysis['inauspicious_count'], # 凶星数量
196
+ 'balance': strength_analysis['balance'], # 吉凶平衡度
197
+ 'recommendation': recommendation # 建议
198
+ }
199
+
200
+
201
+ def _calculate_san_fang_strength(
202
+ palace_name: str,
203
+ major_stars: List[str],
204
+ minor_stars: List[str],
205
+ palace_details: List[dict],
206
+ all_palaces: List[dict],
207
+ sihua_analysis: dict
208
+ ) -> Dict[str, Any]:
209
+ """
210
+ 计算三方四正的综合力量
211
+
212
+ 评分规则(0-100分):
213
+ 1. 主星数量:每颗主星 +8分(最多50分)
214
+ 2. 吉星加成:每颗吉星 +5分(最多30分)
215
+ 3. 凶星扣分:每颗凶星 -3分(最多-15分)
216
+ 4. 四化加成:化禄+10, 化权+8, 化科+6, 化忌-10(最多20分)
217
+ 5. 星曜亮度:庙旺+3, 得利+2, 平+1, 陷-2(最多15分)
218
+
219
+ Returns:
220
+ dict: 力量分析结果
221
+ """
222
+ score = 0
223
+
224
+ # 1. 主星数量评分(最多50分)
225
+ major_star_score = min(len(major_stars) * 8, 50)
226
+ score += major_star_score
227
+
228
+ # 2. 吉星统计
229
+ auspicious_count = 0
230
+ inauspicious_count = 0
231
+
232
+ for star in major_stars + minor_stars:
233
+ if star in AUSPICIOUS_STARS:
234
+ auspicious_count += 1
235
+ score += 5
236
+ if star in INAUSPICIOUS_STARS:
237
+ inauspicious_count += 1
238
+ score -= 3
239
+
240
+ # 限制吉星加分和凶星扣分
241
+ score = max(score, 0) # 不低于0
242
+
243
+ # 3. 四化加成
244
+ sihua_score = 0
245
+ if sihua_analysis:
246
+ year_sihua = sihua_analysis.get('year_sihua', {})
247
+ for sihua_type, data in year_sihua.items():
248
+ palace = data.get('palace', '')
249
+ # 检查是否在三方四正内
250
+ if palace in [pd['palace'] for pd in palace_details]:
251
+ if sihua_type == '化禄':
252
+ sihua_score += 10
253
+ elif sihua_type == '化权':
254
+ sihua_score += 8
255
+ elif sihua_type == '化科':
256
+ sihua_score += 6
257
+ elif sihua_type == '化忌':
258
+ sihua_score -= 10
259
+
260
+ score += sihua_score
261
+ score = max(min(score, 100), 0) # 限制在0-100范围
262
+
263
+ # 判断等级
264
+ if score >= 80:
265
+ level = 'very_strong'
266
+ rank = 'A+'
267
+ elif score >= 70:
268
+ level = 'strong'
269
+ rank = 'A'
270
+ elif score >= 60:
271
+ level = 'medium_strong'
272
+ rank = 'B+'
273
+ elif score >= 50:
274
+ level = 'medium'
275
+ rank = 'B'
276
+ elif score >= 40:
277
+ level = 'medium_weak'
278
+ rank = 'C'
279
+ else:
280
+ level = 'weak'
281
+ rank = 'D'
282
+
283
+ # 吉凶平衡度(-100到100,正数为吉大于凶)
284
+ balance = (auspicious_count * 10) - (inauspicious_count * 10)
285
+
286
+ return {
287
+ 'score': score,
288
+ 'level': level,
289
+ 'rank': rank,
290
+ 'auspicious_count': auspicious_count,
291
+ 'inauspicious_count': inauspicious_count,
292
+ 'balance': balance,
293
+ 'breakdown': {
294
+ 'major_star_score': major_star_score,
295
+ 'sihua_score': sihua_score
296
+ }
297
+ }
298
+
299
+
300
+ def _collect_four_transformations(
301
+ target_palaces: List[str],
302
+ sihua_analysis: dict
303
+ ) -> Dict[str, List[str]]:
304
+ """
305
+ 汇总三方四正内的四化
306
+
307
+ Returns:
308
+ dict: {
309
+ '化禄': ['财帛宫-武曲'],
310
+ '化权': ['官禄宫-紫微'],
311
+ ...
312
+ }
313
+ """
314
+ transformations = {
315
+ '化禄': [],
316
+ '化权': [],
317
+ '化科': [],
318
+ '化忌': []
319
+ }
320
+
321
+ if not sihua_analysis:
322
+ return transformations
323
+
324
+ year_sihua = sihua_analysis.get('year_sihua', {})
325
+
326
+ for sihua_type, data in year_sihua.items():
327
+ palace = data.get('palace', '')
328
+ star = data.get('star', '')
329
+
330
+ if palace in target_palaces:
331
+ transformations[sihua_type].append(f"{palace}-{star}")
332
+
333
+ return transformations
334
+
335
+
336
+ def _generate_san_fang_recommendation(
337
+ palace_name: str,
338
+ strength_analysis: dict
339
+ ) -> str:
340
+ """
341
+ 生成三方四正建议
342
+
343
+ Args:
344
+ palace_name: 宫位名称
345
+ strength_analysis: 力量分析结果
346
+
347
+ Returns:
348
+ str: 建议文字
349
+ """
350
+ score = strength_analysis['score']
351
+ level = strength_analysis['level']
352
+ auspicious = strength_analysis['auspicious_count']
353
+ inauspicious = strength_analysis['inauspicious_count']
354
+
355
+ if level in ['very_strong', 'strong']:
356
+ return f"{palace_name}的三方四正力量强大({score}分/{strength_analysis['rank']}级)," \
357
+ f"吉星{auspicious}颗,凶星{inauspicious}颗,整体格局优良。" \
358
+ f"建议积极发挥该宫位优势,把握机遇,可获得良好发展。"
359
+
360
+ elif level in ['medium_strong', 'medium']:
361
+ return f"{palace_name}的三方四正力量中等({score}分/{strength_analysis['rank']}级)," \
362
+ f"吉星{auspicious}颗,凶星{inauspicious}颗,吉凶参半。" \
363
+ f"建议稳健发展,扬长避短,需谨慎处理该宫位事务。"
364
+
365
+ else:
366
+ return f"{palace_name}的三方四正力量较弱({score}分/{strength_analysis['rank']}级)," \
367
+ f"吉星{auspicious}颗,凶星{inauspicious}颗,需特别注意。" \
368
+ f"建议多行善事,借助贵人力量,化解不利因素。"
369
+
370
+
371
+ def analyze_all_san_fang_si_zheng(
372
+ all_palaces: List[dict],
373
+ sihua_analysis: dict = None
374
+ ) -> List[Dict[str, Any]]:
375
+ """
376
+ 分析所有12宫位的三方四正
377
+
378
+ Args:
379
+ all_palaces: 所有12宫位数据
380
+ sihua_analysis: 四化分析结果
381
+
382
+ Returns:
383
+ list: 所有宫位的三方四正分析结果
384
+ """
385
+ results = []
386
+
387
+ for palace in all_palaces:
388
+ palace_name = palace.get('name', '')
389
+ if palace_name:
390
+ analysis = analyze_san_fang_si_zheng(
391
+ palace_name,
392
+ all_palaces,
393
+ sihua_analysis
394
+ )
395
+ results.append(analysis)
396
+
397
+ return results
398
+
399
+
400
+ # 测试用例
401
+ if __name__ == '__main__':
402
+ # 测试数据
403
+ test_palaces = [
404
+ {
405
+ 'name': '命宫',
406
+ 'major_stars': [
407
+ {'name': '紫微'},
408
+ {'name': '天府'}
409
+ ],
410
+ 'minor_stars': [
411
+ {'name': '左辅'},
412
+ {'name': '文昌'}
413
+ ]
414
+ },
415
+ {
416
+ 'name': '财帛宫',
417
+ 'major_stars': [{'name': '武曲'}],
418
+ 'minor_stars': [{'name': '禄存'}]
419
+ },
420
+ {
421
+ 'name': '官禄宫',
422
+ 'major_stars': [{'name': '天相'}],
423
+ 'minor_stars': []
424
+ },
425
+ {
426
+ 'name': '迁移宫',
427
+ 'major_stars': [{'name': '太阳'}],
428
+ 'minor_stars': [{'name': '擎羊'}]
429
+ }
430
+ ]
431
+
432
+ test_sihua = {
433
+ 'year_sihua': {
434
+ '化禄': {'star': '武曲', 'palace': '财帛宫'},
435
+ '化权': {'star': '紫微', 'palace': '命宫'},
436
+ '化科': {'star': '天府', 'palace': '命宫'},
437
+ '化忌': {'star': '太阳', 'palace': '迁移宫'}
438
+ }
439
+ }
440
+
441
+ # 测试命宫的三方四正
442
+ result = analyze_san_fang_si_zheng('命宫', test_palaces, test_sihua)
443
+
444
+ print("=" * 60)
445
+ print("命宫三方四正分析结果:")
446
+ print("=" * 60)
447
+ print(f"三方: {result['san_fang']}")
448
+ print(f"四正: {result['si_zheng']}")
449
+ print(f"所有主星: {result['all_major_stars']}")
450
+ print(f"力量评分: {result['strength_score']}分 ({result['strength_rank']}级)")
451
+ print(f"吉星数量: {result['auspicious_count']}")
452
+ print(f"凶星数量: {result['inauspicious_count']}")
453
+ print(f"四化: {result['four_transformations']}")
454
+ print(f"\n建议:\n{result['recommendation']}")