MCPcopy Create free account
hub / github.com/Bairong-Xdynamics/MistakeNotebookLearning / grade_answer

Function grade_answer

examples/utils/grader.py:233–286  ·  view source on GitHub ↗

The answer will be considered correct if: (a) it normalizes to the same string as the ground truth answer OR (b) sympy can simplify the difference between the expressions to 0

(given_answer: str, ground_truth: str)

Source from the content-addressed store, hash-verified

231
232
233def grade_answer(given_answer: str, ground_truth: str) -> bool:
234 """
235 The answer will be considered correct if:
236 (a) it normalizes to the same string as the ground truth answer
237 OR
238 (b) sympy can simplify the difference between the expressions to 0
239 """
240 if given_answer is None:
241 return False
242
243 ground_truth_normalized_mathd = normalize_answer(ground_truth)
244 given_answer_normalized_mathd = normalize_answer(given_answer)
245
246 # be at least as lenient as mathd
247 if ground_truth_normalized_mathd == given_answer_normalized_mathd:
248 return True
249
250 ground_truth_normalized = _normalize(ground_truth)
251 given_normalized = _normalize(given_answer)
252
253 if ground_truth_normalized is None:
254 return False
255
256 if ground_truth_normalized == given_normalized:
257 return True
258
259 if len(given_normalized) == 0:
260 return False
261
262 ground_truth_elems = split_tuple(ground_truth_normalized)
263 given_elems = split_tuple(given_normalized)
264
265 if len(ground_truth_elems) > 1 and (
266 ground_truth_normalized[0] != given_normalized[0]
267 or ground_truth_normalized[-1] != given_normalized[-1]
268 ):
269 is_correct = False
270 elif len(ground_truth_elems) != len(given_elems):
271 is_correct = False
272 else:
273 for ground_truth_elem, given_elem in zip(ground_truth_elems, given_elems):
274 if _is_frac(ground_truth_elem) and _is_frac(given_elem):
275 # if fractions aren't reduced, then shouldn't be marked as correct
276 # so, we don't want to allow sympy.simplify in this case
277 is_correct = ground_truth_elem == given_elem
278 elif _str_is_int(ground_truth_elem) != _str_is_int(given_elem):
279 # if the ground truth answer is an integer, we require the given answer to be a strict match (no sympy.simplify)
280 is_correct = False
281 else:
282 is_correct = are_equal_under_sympy(ground_truth_elem, given_elem)
283 if not is_correct:
284 break
285
286 return is_correct

Callers 1

math_latex_reward_fnFunction · 0.90

Calls 6

normalize_answerFunction · 0.85
_normalizeFunction · 0.85
split_tupleFunction · 0.85
_is_fracFunction · 0.85
_str_is_intFunction · 0.85
are_equal_under_sympyFunction · 0.85

Tested by

no test coverage detected