MCPcopy Create free account
hub / github.com/MuLabPKU/TransArch / strip_string

Function strip_string

TransMLA_NeurIPS_2025/lighteval/math_utils.py:209–347  ·  view source on GitHub ↗
(string, skip_unit=False)

Source from the content-addressed store, hash-verified

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

Callers 1

extract_answerFunction · 0.85

Calls 4

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

Tested by

no test coverage detected