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

Function _normalize

data_preprocessing/math_util/__init__.py:109–181  ·  view source on GitHub ↗

Normalize answer expressions.

(expr: str)

Source from the content-addressed store, hash-verified

107
108
109def _normalize(expr: str) -> str:
110 """Normalize answer expressions."""
111 if expr is None:
112 return None
113
114 # Remove enclosing `\text{}`.
115 m = re.search("^\\\\text\{(?P<text>.+?)\}$", expr)
116 if m is not None:
117 expr = m.group("text")
118
119 expr = expr.replace("\\%", "%")
120 expr = expr.replace("\\$", "$")
121 expr = expr.replace("$", "")
122 expr = expr.replace("%", "")
123 expr = expr.replace(" or ", " , ")
124 expr = expr.replace(" and ", " , ")
125
126 expr = expr.replace("million", "*10^6")
127 expr = expr.replace("billion", "*10^9")
128 expr = expr.replace("trillion", "*10^12")
129
130 for unit in [
131 "degree",
132 "cm",
133 "centimeter",
134 "meter",
135 "mile",
136 "second",
137 "minute",
138 "hour",
139 "day",
140 "week",
141 "month",
142 "year",
143 "foot",
144 "feet",
145 "inch",
146 "yard",
147 "liter",
148 ]:
149 expr = re.sub(f"{unit}(es)?(s)? *(\^[0-9]+)?", "", expr)
150 expr = re.sub(f"\^ *\\\\circ", "", expr)
151 # expr = re.sub(f"\^*\\\\circ", "", expr)
152
153 if len(expr) > 0 and expr[0] == "{" and expr[-1] == "}":
154 expr = expr[1:-1]
155
156 expr = re.sub(",\\\\! *", "", expr)
157 if _is_float(expr) and _is_int(float(expr)):
158 expr = str(int(round(float(expr))))
159 if "\\" in expr:
160 try:
161 expr = _parse_latex(expr)
162 except:
163 pass
164
165 # edge case with mixed numbers and negative signs
166 expr = re.sub("- *", "-", expr)

Callers 1

grade_answerFunction · 0.70

Calls 7

_is_floatFunction · 0.70
_is_intFunction · 0.70
_parse_latexFunction · 0.70
_str_is_intFunction · 0.70
_str_to_intFunction · 0.70
groupMethod · 0.45

Tested by

no test coverage detected