test_placeholder.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. import re
  3. def _looks_like_placeholder_text(text: str) -> bool:
  4. normalized = (text or "").strip()
  5. if not normalized:
  6. return False
  7. placeholder_patterns = [
  8. r"^题目内容$",
  9. r"^解析内容$",
  10. r"^参考措施$",
  11. r"^答题要点$",
  12. r"^具体选项内容$",
  13. r"^选项[ABCD]$",
  14. r"^.+工程相关(?:单选题|多选题|判断题|简答题)\d+$",
  15. r"^.+相关(?:单选题|多选题|判断题|简答题)\d+$",
  16. r"^第?\d+题$",
  17. r"^题目\d+$",
  18. ]
  19. return any(re.search(pattern, normalized) for pattern in placeholder_patterns)
  20. def _exam_payload_has_placeholders(payload) -> bool:
  21. if isinstance(payload, dict):
  22. return any(_exam_payload_has_placeholders(value) for value in payload.values())
  23. if isinstance(payload, list):
  24. return any(_exam_payload_has_placeholders(item) for item in payload)
  25. if isinstance(payload, str):
  26. if _looks_like_placeholder_text(payload):
  27. print(f"Match found for: {payload}")
  28. return True
  29. return False
  30. return False
  31. # we can simulate the generated JSON to see which field triggers the placeholder
  32. mock_payload = {
  33. "title": "隧道工程施工技术考核",
  34. "totalScore": 100,
  35. "totalQuestions": 10,
  36. "singleChoice": {"scorePerQuestion": 2, "totalScore": 20, "count": 10, "questions": [{"text": "这是一个单选题题干", "options": [{"key": "A", "text": "选项A具体内容"}, {"key": "B", "text": "选项B具体内容"}], "answer": "A", "analysis": "解析内容"}]},
  37. }
  38. print("Has placeholder:", _exam_payload_has_placeholders(mock_payload))