-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_data2.py
241 lines (197 loc) · 7.25 KB
/
normalize_data2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import pickle
import string
safe_to_wrap = set(string.ascii_letters) | set(string.digits) | {
"\\infty",
"\\prime",
"\\theta",
"\\alpha",
"\\gamma",
"\\mu",
"\\beta",
"\\phi",
"\\pi",
"\\lambda",
"\\Delta"
}
def skip_brackets(formula, index, brackets=("{", "}"), direction=1):
"""
Returns new index with brackets skipped.
Assumes that the formula does have closing brackets after index.
:param formula: list of tokens
:param index: index
:param brackets: tuple of brackets
:param direction: the order of traversing (+1, or -1)
:return: new index with skipped brackets
"""
assert formula[index] == brackets[0], "Formula {} index {} does not start with bracket {}".format("".join(formula),
index,
brackets[0])
assert direction == 1 or direction == -1, "Direction must be between -1 or 1"
index += direction
opening = 0
while formula[index] != brackets[1] or opening != 0:
if formula[index] == brackets[0]:
opening += 1
elif formula[index] == brackets[1]:
opening -= 1
index += direction
return index + direction
def wrap(formula, index):
"""
Wraps the token in formula with { } brackets.
:param formula: list of tokens
:param index: index
:return: formula with wrapped token
"""
assert formula[index] in safe_to_wrap, "Not safe to wrap in {} at index {}".format("".join(formula), index)
formula.insert(index, "{")
formula.insert(index + 2, "}")
return formula
# Has dependency on fraction normalization
def normalize_under_superscript(formula):
new_formula = list(formula)
index = 0
while index < len(new_formula):
token = new_formula[index]
if (token == "^" or token == "_") and new_formula[index + 1] != "{":
if new_formula[index + 1] == "\\frac":
new_formula.insert(index + 1, "{")
index += 3
# Since we've normalized
# We jump over the frac and two parameters
for i in range(2):
index = skip_brackets(new_formula, index)
new_formula.insert(index, "}")
else:
wrap(new_formula, index + 1)
index += 3
index += 1
return new_formula
def remove_brackets(formula, index):
closing = skip_brackets(formula, index) - 1
del formula[closing]
del formula[index]
return formula
def normalize_fractions(formula):
new_formula = list(formula)
index = 0
while index < len(new_formula):
if new_formula[index] == "\\frac":
if index > 0 and new_formula[index - 1] == "{" and index < len(new_formula) and new_formula[index + 1] == "}":
# Unwrap frac
del new_formula[index + 1]
del new_formula[index - 1]
index -= 1
index += 1
for i in range(2):
if new_formula[index] != "{":
wrap(new_formula, index)
index += 3
else:
index = skip_brackets(new_formula, index)
else:
index += 1
return new_formula
def remove_unnecessary_brackets(formula):
new_formula = list(formula)
index = 0
while index < len(new_formula):
token = new_formula[index]
if token == "{":
if index == 0:
new_formula = remove_brackets(new_formula, index)
else:
prev_token = new_formula[index - 1]
if prev_token == "}":
prev_index = skip_brackets(new_formula, index - 1, ("}", "{"), -1)
if new_formula[prev_index] != "\\frac":
new_formula = remove_brackets(new_formula, index)
elif prev_token not in {"\\frac", "_", "^", "\\sqrt", "]"}:
new_formula = remove_brackets(new_formula, index)
index -= 1
index += 1
return new_formula
def normalize_sqrt(formula):
new_formula = list(formula)
index = 0
while index < len(new_formula):
if new_formula[index] == "\\sqrt":
index += 1
if new_formula[index] == "[":
index = skip_brackets(new_formula, index, ("[", "]"))
if new_formula[index] != "{":
wrap(new_formula, index)
index += 3
index += 1
return new_formula
def normalize_under_superscript_order(formula):
new_formula = list(formula)
index = 0
while index < len(new_formula):
if new_formula[index] == "_":
next_ind = skip_brackets(new_formula, index + 1)
if next_ind < len(new_formula) and new_formula[next_ind] == "^":
last_inde = skip_brackets(new_formula, next_ind + 1)
underscript = new_formula[index+1:next_ind]
overscript = new_formula[next_ind+1:last_inde]
cur = index
new_formula[cur] = "^"
cur += 1
new_formula[cur:cur+len(overscript)] = overscript
cur += len(overscript)
new_formula[cur] = "_"
cur += 1
new_formula[cur:cur+len(underscript)] = underscript
index += 1
return new_formula
def normalize_square_brackets(formula, input):
for j, (char, box) in enumerate(input):
if char == '[':
char = '\\['
elif char == ']':
char = '\\]'
input[j] = (char, box)
for j, char in enumerate(formula):
if char == '[':
if j == 0 or formula[j - 1] != '\\sqrt':
k = skip_brackets(formula, j, ('[', ']')) - 1
formula[j] = '\\['
formula[k] = '\\]'
return formula, input
def normalize(formula):
"""
Applies normalizations to formula
:param formula: list of tokens
:return: new formula
"""
for norm in [
normalize_fractions,
normalize_under_superscript,
normalize_sqrt,
remove_unnecessary_brackets,
normalize_under_superscript_order
]:
new_formula = norm(formula)
if new_formula != formula:
print("Changed from \n{}\nto\n{}\n".format("".join(formula), "".join(new_formula)))
formula = new_formula
return formula
def main():
token_file = "validating_data.pkl"
input_tokens_dir = "/Users/balazs/token_trace_orig"
input_tokens_path = os.path.join(input_tokens_dir, token_file)
tokens = pickle.load(open(input_tokens_path, 'rb'))
for index, (formula, input) in enumerate(tokens):
try:
formula = normalize(formula)
except AssertionError as e:
print("Could not parse")
print(formula)
raise e
tokens[index] = normalize_square_brackets(formula, input)
output_tokens_dir = "/Users/balazs/token_trace_normalized2"
output_tokens_path = os.path.join(output_tokens_dir, token_file)
pickle.dump(tokens, open(output_tokens_path, 'wb'))
if __name__ == "__main__":
main()