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

Function _normalize

examples/utils/grader.py:104–174  ·  view source on GitHub ↗

Normalize answer expressions.

(expr: str)

Source from the content-addressed store, hash-verified

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

Callers 1

grade_answerFunction · 0.85

Calls 6

_is_floatFunction · 0.85
_is_intFunction · 0.85
_parse_latexFunction · 0.85
_str_is_intFunction · 0.85
_str_to_intFunction · 0.85

Tested by

no test coverage detected