MCPcopy Create free account
hub / github.com/LeapLabTHU/Absolute-Zero-Reasoner / strip_string

Function strip_string

evaluation/math_eval/eval/parser.py:211–349  ·  view source on GitHub ↗
(string, skip_unit=False)

Source from the content-addressed store, hash-verified

209
210
211def strip_string(string, skip_unit=False):
212 string = str(string).strip()
213 # linebreaks
214 string = string.replace("\n", "")
215
216 # right "."
217 string = string.rstrip(".")
218
219 # remove inverse spaces
220 # replace \\ with \
221 string = string.replace("\\!", "")
222 # string = string.replace("\\ ", "")
223 # string = string.replace("\\\\", "\\")
224
225 # matrix
226 string = re.sub(r"\\begin\{array\}\{.*?\}", r"\\begin{pmatrix}", string)
227 string = re.sub(r"\\end\{array\}", r"\\end{pmatrix}", string)
228 string = string.replace("bmatrix", "pmatrix")
229
230 # replace tfrac and dfrac with frac
231 string = string.replace("tfrac", "frac")
232 string = string.replace("dfrac", "frac")
233 string = (
234 string.replace("\\neq", "\\ne")
235 .replace("\\leq", "\\le")
236 .replace("\\geq", "\\ge")
237 )
238
239 # remove \left and \right
240 string = string.replace("\\left", "")
241 string = string.replace("\\right", "")
242 string = string.replace("\\{", "{")
243 string = string.replace("\\}", "}")
244
245 # Remove unit: miles, dollars if after is not none
246 _string = re.sub(r"\\text{.*?}$", "", string).strip()
247 if _string != "" and _string != string:
248 # print("Warning: unit not removed: '{}' -> '{}'".format(string, _string))
249 string = _string
250
251 if not skip_unit:
252 # Remove unit: texts
253 for _ in range(2):
254 for unit_text in unit_texts:
255 # use regex, the prefix should be either the start of the string or a non-alphanumeric character
256 # the suffix should be either the end of the string or a non-alphanumeric character
257 _string = re.sub(r"(^|\W)" + unit_text + r"($|\W)", r"\1\2", string)
258 if _string != "":
259 string = _string
260
261 # Remove circ (degrees)
262 string = string.replace("^{\\circ}", "")
263 string = string.replace("^\\circ", "")
264
265 # remove dollar signs
266 string = string.replace("\\$", "")
267 string = string.replace("$", "")
268 string = string.replace("\\(", "").replace("\\)", "")

Callers 3

extract_answerFunction · 0.70
parse_ground_truthFunction · 0.70
run_executeFunction · 0.70

Calls 4

convert_word_numberFunction · 0.85
_fix_sqrtFunction · 0.70
_fix_fracsFunction · 0.70
_fix_a_slash_bFunction · 0.70

Tested by

no test coverage detected