test_extract.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import re
  2. import requests
  3. # 获取common模板
  4. resp = requests.get("http://localhost:8000/api/templates/x11_wash_common")
  5. common_body = resp.json()["template_body"]
  6. print("=== Common模板前200字符 ===")
  7. print(common_body[:200])
  8. # 测试宏提取 - 使用DOTALL和MULTILINE
  9. text_sample = '''{% macro count_round(value) %}
  10. {% set value = value | int %}
  11. {{ (value / 10) | round(0) | int * 10 }}
  12. {% endmacro %}
  13. {% macro approx_fraction(p) -%}
  14. {%- set p = 99 if p |int >= 100 else p -%}
  15. {%- endmacro %}
  16. '''
  17. macro_pattern = r'{%-?\s*macro\s+\w+\s*\([^)]*\)\s*-%?%}[\s\S]*?{%-?\s*endmacro\s*-%?%}'
  18. print("\n=== 测试文本样本 ===")
  19. test_matches = list(re.finditer(macro_pattern, text_sample))
  20. print(f"测试样本宏匹配数量: {len(test_matches)}")
  21. for i, m in enumerate(test_matches):
  22. print(f"匹配 {i+1}: {m.group(0)[:80]}...")
  23. print("\n=== 实际common模板 ===")
  24. matches = list(re.finditer(macro_pattern, common_body))
  25. print(f"宏匹配数量: {len(matches)}")
  26. for i, m in enumerate(matches):
  27. print(f"匹配 {i+1}: {m.group(0)[:80]}...")
  28. # 测试列表变量提取
  29. list_set_pattern = r'{%\s*set\s+(greeting|assistant1|assistant2|user1|user2|period_list|city_list)\s*=\s*\[[^\]]*\]\s*%}'
  30. list_matches = list(re.finditer(list_set_pattern, common_body, re.IGNORECASE))
  31. print(f"\n列表变量匹配数量: {len(list_matches)}")
  32. for m in list_matches:
  33. print(f"变量: {m.group(0)[:60]}...")