Spaces:
Sleeping
Sleeping
File size: 3,992 Bytes
ee7234a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | """
测试 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)
|