zeju-0727 commited on
Commit
8028aae
·
verified ·
1 Parent(s): f08f879

Upload dyve_tts/eval/math/modeling/math_equivalence.py with huggingface_hub

Browse files
dyve_tts/eval/math/modeling/math_equivalence.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def _fix_fracs(string):
2
+ substrs = string.split("\\frac")
3
+ new_str = substrs[0]
4
+ if len(substrs) > 1:
5
+ substrs = substrs[1:]
6
+ for substr in substrs:
7
+ new_str += "\\frac"
8
+ if substr[0] == "{":
9
+ new_str += substr
10
+ else:
11
+ try:
12
+ assert len(substr) >= 2
13
+ except:
14
+ return string
15
+ a = substr[0]
16
+ b = substr[1]
17
+ if b != "{":
18
+ if len(substr) > 2:
19
+ post_substr = substr[2:]
20
+ new_str += "{" + a + "}{" + b + "}" + post_substr
21
+ else:
22
+ new_str += "{" + a + "}{" + b + "}"
23
+ else:
24
+ if len(substr) > 2:
25
+ post_substr = substr[2:]
26
+ new_str += "{" + a + "}" + b + post_substr
27
+ else:
28
+ new_str += "{" + a + "}" + b
29
+ string = new_str
30
+ return string
31
+
32
+ def _fix_a_slash_b(string):
33
+ if len(string.split("/")) != 2:
34
+ return string
35
+ a = string.split("/")[0]
36
+ b = string.split("/")[1]
37
+ try:
38
+ a = int(a)
39
+ b = int(b)
40
+ assert string == "{}/{}".format(a, b)
41
+ new_string = "\\frac{" + str(a) + "}{" + str(b) + "}"
42
+ return new_string
43
+ except:
44
+ return string
45
+
46
+ def _remove_right_units(string):
47
+ # "\\text{ " only ever occurs (at least in the val set) when describing units
48
+ if "\\text{ " in string:
49
+ splits = string.split("\\text{ ")
50
+ assert len(splits) == 2
51
+ return splits[0]
52
+ else:
53
+ return string
54
+
55
+ def _fix_sqrt(string):
56
+ if "\\sqrt" not in string:
57
+ return string
58
+ splits = string.split("\\sqrt")
59
+ new_string = splits[0]
60
+ for split in splits[1:]:
61
+ if split[0] != "{":
62
+ a = split[0]
63
+ new_substr = "\\sqrt{" + a + "}" + split[1:]
64
+ else:
65
+ new_substr = "\\sqrt" + split
66
+ new_string += new_substr
67
+ return new_string
68
+
69
+ def _strip_string(string):
70
+ # linebreaks
71
+ string = string.replace("\n", "")
72
+ #print(string)
73
+
74
+ # remove inverse spaces
75
+ string = string.replace("\\!", "")
76
+ #print(string)
77
+
78
+ # replace \\ with \
79
+ string = string.replace("\\\\", "\\")
80
+ #print(string)
81
+
82
+ # replace tfrac and dfrac with frac
83
+ string = string.replace("tfrac", "frac")
84
+ string = string.replace("dfrac", "frac")
85
+ #print(string)
86
+
87
+ # remove \left and \right
88
+ string = string.replace("\\left", "")
89
+ string = string.replace("\\right", "")
90
+ #print(string)
91
+
92
+ # Remove circ (degrees)
93
+ string = string.replace("^{\\circ}", "")
94
+ string = string.replace("^\\circ", "")
95
+
96
+ # remove dollar signs
97
+ string = string.replace("\\$", "")
98
+
99
+ # remove units (on the right)
100
+ string = _remove_right_units(string)
101
+
102
+ # remove percentage
103
+ string = string.replace("\\%", "")
104
+ string = string.replace("\%", "")
105
+
106
+ # " 0." equivalent to " ." and "{0." equivalent to "{." Alternatively, add "0" if "." is the start of the string
107
+ string = string.replace(" .", " 0.")
108
+ string = string.replace("{.", "{0.")
109
+ # if empty, return empty string
110
+ if len(string) == 0:
111
+ return string
112
+ if string[0] == ".":
113
+ string = "0" + string
114
+
115
+ # to consider: get rid of e.g. "k = " or "q = " at beginning
116
+ if len(string.split("=")) == 2:
117
+ if len(string.split("=")[0]) <= 2:
118
+ string = string.split("=")[1]
119
+
120
+ # fix sqrt3 --> sqrt{3}
121
+ string = _fix_sqrt(string)
122
+
123
+ # remove spaces
124
+ string = string.replace(" ", "")
125
+
126
+ # \frac1b or \frac12 --> \frac{1}{b} and \frac{1}{2}, etc. Even works with \frac1{72} (but not \frac{72}1). Also does a/b --> \\frac{a}{b}
127
+ string = _fix_fracs(string)
128
+
129
+ # manually change 0.5 --> \frac{1}{2}
130
+ if string == "0.5":
131
+ string = "\\frac{1}{2}"
132
+
133
+ # NOTE: X/Y changed to \frac{X}{Y} in dataset, but in simple cases fix in case the model output is X/Y
134
+ string = _fix_a_slash_b(string)
135
+
136
+ return string
137
+
138
+ def is_equiv(str1, str2, verbose=False):
139
+ if str1 is None and str2 is None:
140
+ print("WARNING: Both None")
141
+ return True
142
+ if str1 is None or str2 is None:
143
+ return False
144
+
145
+ try:
146
+ ss1 = _strip_string(str1)
147
+ ss2 = _strip_string(str2)
148
+ if verbose:
149
+ print(ss1, ss2)
150
+ return ss1 == ss2
151
+ except:
152
+ return str1 == str2