Spaces:
Sleeping
Sleeping
| """ | |
| 测试 GDL 和自然语言提取功能 | |
| """ | |
| import re | |
| import sys | |
| def extract_gdl_and_narrative(content): | |
| """提取 GDL 和自然语言部分(支持多种格式变体)""" | |
| # 定义多种可能的标记格式(按优先级排序) | |
| gdl_patterns = [ | |
| r"##\s*GDL\s*描述", # ## GDL描述 或 ## GDL 描述 | |
| r"##\s*GDL描述", # ##GDL描述 | |
| r"GDL\s*描述", # GDL描述 或 GDL 描述 | |
| r"##\s*GDL" # ## GDL | |
| ] | |
| narrative_patterns = [ | |
| r"##\s*自然语言规则说明", # ## 自然语言规则说明 | |
| r"##\s*自然语言规则", # ## 自然语言规则 | |
| r"自然语言规则说明", # 自然语言规则说明 | |
| r"自然语言规则" # 自然语言规则 | |
| ] | |
| # 尝试查找 GDL 部分 | |
| gdl_start = -1 | |
| for pattern in gdl_patterns: | |
| match = re.search(pattern, content) | |
| if match: | |
| gdl_start = match.start() | |
| break | |
| # 尝试查找自然语言部分 | |
| narrative_start = -1 | |
| for pattern in narrative_patterns: | |
| match = re.search(pattern, content) | |
| if match: | |
| narrative_start = match.start() | |
| break | |
| if gdl_start != -1 and narrative_start != -1: | |
| # 获取 GDL 部分(从标记开始到自然语言标记之前) | |
| gdl_end = narrative_start | |
| gdl_content = content[gdl_start:gdl_end].strip() | |
| # 获取自然语言部分(从标记开始到结尾) | |
| narrative_content = content[narrative_start:].strip() | |
| return gdl_content, narrative_content | |
| else: | |
| # 如果找不到标记,返回空字符串 | |
| print(f"⚠️ 提取警告: GDL标记={'找到' if gdl_start != -1 else '未找到'}, 自然语言标记={'找到' if narrative_start != -1 else '未找到'}") | |
| return "", "" | |
| def test_extraction(): | |
| """测试提取功能的各种格式""" | |
| print("=" * 60) | |
| print("测试 GDL 和自然语言提取功能") | |
| print("=" * 60) | |
| # 测试用例 1:标准格式(有空格) | |
| test1 = """ | |
| ## 游戏名称 | |
| 测试游戏 | |
| ## 游戏理念 | |
| 这是一个测试 | |
| ## GDL描述 | |
| (game "TestGame" | |
| (players 3) | |
| (deck "Standard54") | |
| ) | |
| ## 自然语言规则说明 | |
| 1. 这是规则说明 | |
| 2. 测试内容 | |
| """ | |
| # 测试用例 2:无空格格式 | |
| test2 = """ | |
| ## 游戏名称 | |
| 测试游戏 | |
| ##GDL描述 | |
| (game "TestGame" | |
| (players 3) | |
| ) | |
| ##自然语言规则说明 | |
| 1. 规则说明 | |
| """ | |
| # 测试用例 3:混合格式 | |
| test3 = """ | |
| ## 游戏名称 | |
| 测试游戏 | |
| ## GDL 描述 | |
| (game "TestGame" | |
| (players 3) | |
| ) | |
| ## 自然语言规则说明 | |
| 1. 规则说明 | |
| """ | |
| test_cases = [ | |
| ("标准格式(有空格)", test1), | |
| ("无空格格式", test2), | |
| ("带空格分隔的格式", test3) | |
| ] | |
| all_passed = True | |
| for i, (name, content) in enumerate(test_cases, 1): | |
| print(f"\n测试用例 {i}: {name}") | |
| print("-" * 60) | |
| gdl, narrative = extract_gdl_and_narrative(content) | |
| if gdl and narrative: | |
| print(f"✅ 提取成功") | |
| print(f" GDL 长度: {len(gdl)} 字符") | |
| print(f" GDL 前50字符: {gdl[:50]}...") | |
| print(f" 自然语言长度: {len(narrative)} 字符") | |
| print(f" 自然语言前50字符: {narrative[:50]}...") | |
| else: | |
| print(f"❌ 提取失败") | |
| if not gdl: | |
| print(" 未找到 GDL 部分") | |
| if not narrative: | |
| print(" 未找到自然语言部分") | |
| all_passed = False | |
| print("\n" + "=" * 60) | |
| if all_passed: | |
| print("✅ 所有测试通过!") | |
| else: | |
| print("⚠️ 部分测试失败,请检查提取逻辑") | |
| print("=" * 60) | |
| return all_passed | |
| if __name__ == "__main__": | |
| success = test_extraction() | |
| sys.exit(0 if success else 1) | |