-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_math.py
239 lines (228 loc) · 5.93 KB
/
clean_math.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
import re
from database import get_db_conn
def get_editorial(name):
conn = get_db_conn()
cursor = conn.cursor()
cursor.execute("""
SELECT content
FROM editorials
WHERE name = %s
""", (name,))
editorial = cursor.fetchone()
cursor.close()
conn.close()
return editorial[0]
def get_problem_names():
conn = get_db_conn()
cursor = conn.cursor()
cursor.execute(
"""
SELECT p.name
FROM problems p
JOIN editorials e ON p.name = e.name
ORDER BY p.cf_rating ASC
""")
problems = cursor.fetchall()
conn.close()
return problems
def escaped(editorial: str, index: int) -> bool:
for i in range(index - 1, -1, -1):
if editorial[i] != '\\':
return (index - i) % 2 == 0
return False
control_sequences = []
character_replace = {
'\u2009': ' ',
'\u200b': '',
'\u2002': ' ',
'’': "'",
'\xa0': ' ',
'\u2061': '',
'А': 'A',
'“': '"',
'р': 'p',
'’': "'",
'і': 'i',
'с': 'c',
'х': 'x',
'у': 'y',
'м': 'm',
'а': 'a',
'С': 'C',
'”': '"',
'‘': "'",
'…': '...',
'е': 'e',
'о': 'o',
'—': '-',
'–': '-',
'−': '-',
'и': ' and ',
'′': "'",
',': ',',
'⋅': '·',
'\\le': '≤',
'\\ge': '≥',
'\\cdots': '...',
'\\ldots': '...',
'\\dots': '...',
'\\cdot': '·',
'\\times': '×',
'\\div': '÷',
'\\leq': '≤',
'\\geq': '≥',
'\\neq': '≠',
'\\infty': '∞',
'\\notin': '∉',
'\\subset': '⊂',
'\\in': '∈',
'\\forall': '∀',
'\\exists': '∃',
'\\nexists': '∄',
'\\emptyset': '∅',
'\\varnothing': '∅',
'\\cup': '∪',
'\\cap': '∩',
'\\supset': '⊃',
'\\subseteq': '⊆',
'\\supseteq': '⊇',
'\\subsetneq': '⊊',
'\\supsetneq': '⊋',
'\\setminus': '∖',
'\\backslash': '∖',
'\\triangle': '△',
'\\nabla': '∇',
'\\partial': '∂',
'\\angle': '∠',
'\\parallel': '∥',
'\\perp': '⊥',
'\\cong': '≅',
'\\equiv': '≡',
'\\approx': '≈',
'\\simeq': '≃',
'\\sim': '∼',
'\\propto': '∝',
'\\varepsilon': 'ε',
'\\epsilon': 'ε',
'\\bigcup': '⋃',
'\\lfloor': '⌊',
'\\rfloor': '⌋',
'\\langle': '⟨',
'\\rangle': '⟩',
'\\sigma': 'σ',
'\\lvert': '|',
'\\rvert': '|',
'\\Sigma': 'Σ',
'\\cfrac': '\\frac',
'\\delta': 'δ',
'\\oplus': '⊕',
'\\Omega': 'Ω',
'\\wedge': '∧',
'\\rceil': '⌉',
'\\lceil': '⌈',
'\\colon': ':',
'\\omega': 'ω',
'\\prime': "'",
'\\dfrac': '\\frac',
'\\gamma': 'γ',
'\\vdots': '⋮',
'\\Phi': 'Φ',
'\\gt': '>',
'\\lt': '<',
'\\pi': 'π',
'\\pm': '±',
'\\to': '→',
'\\ll': '≪',
'\\mu': 'μ',
'\\ln': 'log',
'\\ne': '≠',
'\\nmid': '∤',
'\\eta': 'η',
'\\tau': 'τ',
'\\rho': 'ρ',
'\\ell': 'l',
'\\phi': 'φ',
'\\neg': '¬',
'\\deg': '°',
'\\circ': '°',
'\\vert': '|',
'\\tilde': '~',
'\\alpha': 'α',
'\\beta': 'β',
'\\Bigr': '',
'\\land': '∧',
'\\bigcap': '⋂',
'\\lambda': 'λ',
'\\limits': '',
'\\mapsto': '↦',
'\\varphi': 'φ',
'\\geqslant': '≥',
'\\leftarrow': '←',
'\\Theta': 'Θ',
'\\Delta': 'Δ',
'\\tfrac': '\\frac',
'\\Bigl': '',
'\\vee': '∨',
'\\not': '¬',
'\\Z': 'ℤ',
'\\Big': '',
'\\big': '',
'\\bigg': '',
'\\biggl': '',
'\\Biggr': '',
'\\Bigl': '',
'\\Bigg': '',
'\\mid': '|',
'\\iff': '⇔',
'\\implies': '⇒',
'\\p': 'p',
'\\quad': ' ',
'\\space': ' ',
'\\textrm': '\\text',
'\\textbf': '\\text',
'\\textit': '\\text',
'\\texttt': '\\text',
'\\mathsf': '\\mathsf',
}
unicode_characters = set()
if __name__ == '__main__':
character_replace = [(character, character_replace[character]) for character in character_replace]
character_replace = sorted(character_replace, key=lambda x: len(x[0]), reverse=True)
names = get_problem_names()
for name in names:
name = name[0]
editorial = get_editorial(name)
for character in character_replace:
editorial = editorial.replace(character[0], character[1])
dollar_start = -1
unicodes = re.sub(r"[\x00-\x7f]+", "", editorial)
if len(unicodes) > 0:
for character in unicodes:
unicode_characters.add((character, ord(character)))
if True:
for i in range(len(editorial)):
if editorial[i] == '$':
if not escaped(editorial, i):
if dollar_start == -1:
dollar_start = i
else:
math = editorial[dollar_start + 1:i]
dollar_start = -1
math_offset = 0
while '\\' in math[math_offset:]:
sequence_start = math.find('\\', math_offset)
if escaped(math, sequence_start):
math_offset = sequence_start + 1
continue
for j in range(sequence_start + 1, len(math)):
if not math[j].isalpha():
sequence = math[sequence_start:j]
control_sequences.append(sequence)
break
math_offset = sequence_start + 1
for character in unicode_characters:
print(character)
sequences = list(set(control_sequences))
sequences = sorted(sequences, key=lambda x: len(x), reverse=True)
for sequence in sequences:
print(sequence)