| 112 | |
| 113 | |
| 114 | class Extractor: |
| 115 | |
| 116 | def extract_matching_bracket(cls, target_str: str): |
| 117 | if not target_str: |
| 118 | return target_str |
| 119 | current_nest_level = 1 |
| 120 | for i, ch in enumerate(target_str): |
| 121 | if ch == '{': |
| 122 | current_nest_level += 1 |
| 123 | elif ch == '}': |
| 124 | current_nest_level -= 1 |
| 125 | if current_nest_level == 0: |
| 126 | break |
| 127 | return target_str[:i] |
| 128 | |
| 129 | def clean(cls, target_str: str): |
| 130 | opt = target_str.strip().replace('{{', '{').replace('}}', '}') |
| 131 | if not opt: |
| 132 | return opt |
| 133 | if opt[-1] == '.' or opt[-1] == '。': |
| 134 | return opt[:-1] |
| 135 | return opt |
| 136 | |
| 137 | def extract_answer(cls, pred: str, extract_last_num=False): |
| 138 | if pred.find('The final answer is ') >= 0: |
| 139 | x = pred[pred.find('The final answer is ') + |
| 140 | len('The final answer is '):] |
| 141 | x = x[1:x.find('$.')] |
| 142 | # print(x) |
| 143 | return cls.clean(x) |
| 144 | if pred.find('\n\nQuestion:') >= 0: |
| 145 | pred = pred.split('\n\nQuestion:')[0] |
| 146 | if pred.find('The answer is'): |
| 147 | pred = pred[pred.find('The answer is') + len('The answer is'):] |
| 148 | return cls.clean(pred) |
| 149 | if pred.find('# Answer') >= 0: |
| 150 | return cls.clean(pred[pred.find('# Answer') + len('# Answer'):]) |
| 151 | if pred.find('The answer is:') >= 0: |
| 152 | return cls.clean(pred[pred.find('The answer is:') + |
| 153 | len('The answer is:'):]) |
| 154 | if pred.find('####') >= 0: |
| 155 | return cls.clean(pred[pred.find('####') + 4:]) |
| 156 | left = '\\boxed{' |
| 157 | if pred.find(left) >= 0: |
| 158 | pred = pred[pred.find(left) + len(left):] |
| 159 | return cls.clean(cls.extract_matching_bracket(pred)) |
| 160 | |
| 161 | if extract_last_num: |
| 162 | nums = [] |
| 163 | opt = '' |
| 164 | |
| 165 | def contain_digit(opt): |
| 166 | for ch in opt: |
| 167 | if ch.isdigit(): |
| 168 | return True |
| 169 | return False |
| 170 | |
| 171 | for ch in pred: |
no outgoing calls
no test coverage detected