Spaces:
Running
Running
Download archive/integrate_batch_05_robust.py from sisithefox/astrology-ziwei-api: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/sisithefox/astrology-ziwei-api/resolve/main/archive/integrate_batch_05_robust.py
- Command line
-
hf download hf://spaces/sisithefox/astrology-ziwei-api/archive/integrate_batch_05_robust.py
-
curl -L -o integrate_batch_05_robust.py https://huggingface.co/spaces/sisithefox/astrology-ziwei-api/resolve/main/archive/integrate_batch_05_robust.py
4.19 kB
| #!/usr/bin/env python3 | |
| """ | |
| 整合新断诀 Batch 05 - 更强大的JSON清理 | |
| """ | |
| import json | |
| import re | |
| import sys | |
| print("=" * 80) | |
| print("🎉 紫微规则库扩展 Batch 05 (Robust)") | |
| print("=" * 80) | |
| # 1. 读取新断诀文件 | |
| print("\n📖 Step 1: 读取新断诀文件...") | |
| source_file = "/Volumes/sparksverse/agent-knowledge/mingli-knowledge/eastern/ziwei/interpretations/03-divination-rules700-1000.md" | |
| with open(source_file, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # 2. 提取JSON数组 | |
| print("📖 Step 2: 提取JSON数组...") | |
| start_idx = content.find('[') | |
| end_idx = content.rfind(']') | |
| if start_idx == -1 or end_idx == -1: | |
| print("❌ 无法找到JSON数组!") | |
| sys.exit(1) | |
| json_str = content[start_idx:end_idx+1] | |
| # 3. 强力清理JSON格式 | |
| print("🔧 Step 3: 强力清理JSON格式...") | |
| # 修复age_range | |
| json_str = re.sub(r'"age_range"\s*:\s*,', '"age_range": [20, 70],', json_str) | |
| json_str = re.sub(r'"age_range"\s*:\s*null\s*,', '"age_range": [20, 70],', json_str) | |
| json_str = re.sub(r'"age_range"\s*:\s*null\s*\}', '"age_range": [20, 70]\n }', json_str) | |
| # 修复fortune_timing | |
| json_str = re.sub(r'"fortune_timing"\s*:\s*null\s*,', '"fortune_timing": null,', json_str) | |
| # 修复空数组 | |
| json_str = re.sub(r'\[\s*\]', '[]', json_str) | |
| # 修复单词拼写错误 | |
| json_str = json_str.replace('"carEer"', '"career"') | |
| json_str = json_str.replace('",\n }', '"\n }') | |
| # 尝试解析 | |
| print("🔍 Step 4: 尝试解析JSON...") | |
| try: | |
| new_rules = json.loads(json_str) | |
| print(f"✅ 成功提取 {len(new_rules)} 条规则") | |
| except json.JSONDecodeError as e: | |
| print(f"❌ JSON解析失败: {e}") | |
| print(f" 位置: line {e.lineno}, column {e.colno}") | |
| print(f" 错误内容:") | |
| lines = json_str.split('\n') | |
| if e.lineno <= len(lines): | |
| for i in range(max(0, e.lineno-5), min(len(lines), e.lineno+5)): | |
| prefix = ">>> " if i == e.lineno-1 else " " | |
| print(f"{prefix}{i+1:4d}: {lines[i]}") | |
| sys.exit(1) | |
| # 5. 检查规则完整性 | |
| print("\n🔍 Step 5: 检查规则完整性...") | |
| valid_rules = [] | |
| for i, rule in enumerate(new_rules): | |
| if not rule.get('id'): | |
| print(f"⚠️ Rule {i} 缺少ID,跳过") | |
| continue | |
| if not rule.get('name'): | |
| print(f"⚠️ Rule {rule['id']} 缺少name,跳过") | |
| continue | |
| # 修复age_range | |
| if rule.get('age_range') is None or rule.get('age_range') == '': | |
| rule['age_range'] = [20, 70] | |
| # 确保必需字段存在 | |
| rule.setdefault('category', '运势走向') | |
| rule.setdefault('stars_required', []) | |
| rule.setdefault('palace_types', []) | |
| rule.setdefault('conditions', {}) | |
| rule.setdefault('interpretations', {}) | |
| rule.setdefault('effect_level', '中') | |
| rule.setdefault('compatibility_score', 50) | |
| rule.setdefault('source', '南北派实战断诀') | |
| valid_rules.append(rule) | |
| print(f"✅ 有效规则: {len(valid_rules)} 条") | |
| # 6. 重新编号(+203偏移) | |
| print("\n🔢 Step 6: 重新编号(SC700→SC903, +203偏移)...") | |
| id_offset = 203 | |
| renumbered = 0 | |
| for rule in valid_rules: | |
| old_id = rule['id'] | |
| match = re.match(r'SC(\d+)', old_id) | |
| if match: | |
| old_num = int(match.group(1)) | |
| new_num = old_num + id_offset | |
| new_id = f'SC{new_num}' | |
| rule['id'] = new_id | |
| renumbered += 1 | |
| if renumbered <= 5 or renumbered > len(valid_rules) - 5: | |
| print(f" {old_id} → {new_id}") | |
| elif renumbered == 6: | |
| print(f" ... ({len(valid_rules)-10} more) ...") | |
| else: | |
| print(f"⚠️ 无法解析ID: {old_id}") | |
| print(f"✅ 重新编号完成: {renumbered} 条") | |
| # 保存中间结果用于调试 | |
| debug_file = '/Volumes/sparksverse/huggingface-spaces/astrology-ziwei-api/batch_05_rules_debug.json' | |
| with open(debug_file, 'w', encoding='utf-8') as f: | |
| json.dump(valid_rules, f, ensure_ascii=False, indent=2) | |
| print(f"📝 调试文件已保存: {debug_file}") | |
| print("\n✅ 规则提取和重编号完成!") | |
| print(f" 有效规则: {len(valid_rules)} 条") | |
| print(f" ID范围: {valid_rules[0]['id']} - {valid_rules[-1]['id']}") | |
| print("\n📝 下一步: 运行合并脚本将这些规则添加到API") | |