Parse reasoning text into structured format.
(reasoning_text)
| 83 | |
| 84 | |
| 85 | def parse_reasoning(reasoning_text): |
| 86 | """ |
| 87 | Parse reasoning text into structured format. |
| 88 | """ |
| 89 | parsed_data = {} |
| 90 | |
| 91 | # Extract QUESTION |
| 92 | question_match = re.search(r"QUESTION:\s*(.*)", reasoning_text) |
| 93 | parsed_data["QUESTION"] = question_match.group(1) if question_match else "" |
| 94 | |
| 95 | # Extract OPTIONS |
| 96 | options_match = re.findall(r"([A-D])\.\s*(.*)", reasoning_text) |
| 97 | parsed_data["OPTIONS"] = {opt: text for opt, text in options_match} |
| 98 | |
| 99 | # Extract ANSWER |
| 100 | answer_match = re.search(r"ANSWER:\s*([A-D])", reasoning_text) |
| 101 | parsed_data["ANSWER"] = answer_match.group(1) if answer_match else "" |
| 102 | |
| 103 | # Extract REASONS |
| 104 | reasons = {} |
| 105 | if "##### From [" in reasoning_text: |
| 106 | reason_blocks = re.split(r"##### From \[.*?\]", reasoning_text)[1:] |
| 107 | reason_blocks_2 = re.split(r"##### From ", reasoning_text)[1:] |
| 108 | else: |
| 109 | reason_blocks = re.split(r"##### From .*?\n", reasoning_text)[1:] |
| 110 | reason_blocks_2 = re.split(r"##### From ", reasoning_text)[1:] |
| 111 | |
| 112 | for i, block in enumerate(reason_blocks): |
| 113 | if block and block[0] == ":": |
| 114 | block = block[1:] |
| 115 | step_reasons = [line.strip('- ') for line in block.strip().split('\n') if line.startswith('- ')] |
| 116 | try: |
| 117 | # 尝试提取时间戳,支持两种格式: |
| 118 | # 1. ##### From [0 to 10]: |
| 119 | # 2. ##### From 0 to 10: |
| 120 | timestamp_raw = reason_blocks_2[i].split(block)[0].strip() |
| 121 | if "[" in timestamp_raw and "]" in timestamp_raw: |
| 122 | # 格式1: 有方括号 |
| 123 | timestamp = timestamp_raw.split("[")[1].split("]")[0] |
| 124 | else: |
| 125 | # 格式2: 没有方括号,直接提取冒号前的内容 |
| 126 | timestamp = timestamp_raw.rstrip(":").strip() |
| 127 | except: |
| 128 | timestamp = "" |
| 129 | reasons[f"Step {i + 1}"] = {"timestamp": timestamp, "reasons": step_reasons} |
| 130 | |
| 131 | parsed_data["REASONS"] = reasons |
| 132 | |
| 133 | return parsed_data |
| 134 | |
| 135 | |
| 136 | def _remove_captions(text): |