Extract a single answer letter (A/B/C/D) from the response.
(response: str)
| 26 | |
| 27 | |
| 28 | def _parse_single_answer(response: str) -> Optional[str]: |
| 29 | """Extract a single answer letter (A/B/C/D) from the response.""" |
| 30 | valid = {"A", "B", "C", "D"} |
| 31 | patterns = [ |
| 32 | r"answer is \((.)\)", |
| 33 | r"Answer: \((.)\)", |
| 34 | r"answer: \((.)\)", |
| 35 | r"answer is ([A-D])\b", |
| 36 | r"Answer: ([A-D])\b", |
| 37 | ] |
| 38 | for pattern in patterns: |
| 39 | match = re.search(pattern, response) |
| 40 | if match and match.group(1) in valid: |
| 41 | return match.group(1) |
| 42 | |
| 43 | # Fallback: last standalone (X) pattern |
| 44 | matches = re.findall(r"\(([A-D])\)", response) |
| 45 | if matches: |
| 46 | return matches[-1] |
| 47 | |
| 48 | return None |
| 49 | |
| 50 | |
| 51 | def _parse_multi_answer(response: str) -> Optional[str]: |