MCPcopy Create free account
hub / github.com/PRIME-RL/PRIME / grade_answer

Function grade_answer

data_preprocessing/math_util/__init__.py:240–293  ·  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

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

Callers 2

evaluate_mathFunction · 0.70
analyze_solutionsMethod · 0.50

Calls 5

_normalizeFunction · 0.70
split_tupleFunction · 0.70
_is_fracFunction · 0.70
_str_is_intFunction · 0.70
are_equal_under_sympyFunction · 0.70

Tested by

no test coverage detected