-
Notifications
You must be signed in to change notification settings - Fork 63
/
update.py
345 lines (301 loc) · 14.2 KB
/
update.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
"""
Index Generator for The Book of Statistical Proofs
_
This script loads all files from the proof/definition directories and
- checks if they are in "Table of Contents" (structured overview)
- writes them as a list into "Proof by Number" (chronological list)
- writes them as a list into "Proof by Topic" (an alphabetical list)
- writes them as a list into "Proof by Author" (sorted by contributor)
- writes them as a list into "Proofs without Source" (no references)
- writes them as a list into "Definition by Number" (chronological list)
- writes them as a list into "Definition by Topic" (an alphabetical list)
- writes them as a list into "Definition by Author" (sorted by contributor)
- writes them as a list into "Definitions without Source" (no references)
Author: Joram Soch, BCCN Berlin
E-Mail: [email protected]
First edit: 2019-09-27 12:55:00
Last edit: 2022-07-21 07:47:00
"""
# Import modules
#-----------------------------------------------------------------------------#
import os
import re
import numpy as np
from datetime import datetime
# List files in proof directory
#-----------------------------------------------------------------------------#
files = os.listdir('P/')
proofs = []
# Browse through list of files
#-----------------------------------------------------------------------------#
for file in files:
if '.md' in file:
# Read proof file
#---------------------------------------------------------------------#
file_obj = open('P/' + file, 'r')
file_txt = file_obj.readlines()
file_obj.close()
# Parse YAML header
#---------------------------------------------------------------------#
source = False
for line in file_txt:
if line.find('proof_id:') == 0:
proof_id = re.sub('"', '', line[10:-1])
if line.find('shortcut:') == 0:
shortcut = re.sub('"', '', line[10:-1])
if line.find('title:') == 0:
title = re.sub('"', '', line[7:-1])
title_edit = re.sub('[^a-zA-Z- ]', '', title)
title_sort = title_edit.lower()
if line.find('author:') == 0:
author = re.sub('"', '', line[8:-1])
if line.find('username:') == 0:
username = re.sub('"', '', line[10:-1])
if not username:
if not author:
username = 'unknown'
else:
username = author
if line.find('date:') == 0:
date = datetime.strptime(line[6:-1], '%Y-%m-%d %H:%M:%S')
if line.find(' - authors:') == 0:
source = True
# Write dictionary entry
#---------------------------------------------------------------------#
proofs.append({'proof_id': proof_id, 'shortcut': shortcut, 'title': title, \
'username': username, 'date': date, 'source': source, \
'title_sort': title_sort})
# List files in definition directory
#-----------------------------------------------------------------------------#
files = os.listdir('D/')
defs = []
# Browse through list of files
#-----------------------------------------------------------------------------#
for file in files:
if '.md' in file:
# Read proof file
#---------------------------------------------------------------------#
file_obj = open('D/' + file, 'r')
file_txt = file_obj.readlines()
file_obj.close()
# Parse YAML header
#---------------------------------------------------------------------#
source = False
for line in file_txt:
if line.find('def_id:') == 0:
def_id = re.sub('"', '', line[8:-1])
if line.find('shortcut:') == 0:
shortcut = re.sub('"', '', line[10:-1])
if line.find('title:') == 0:
title = re.sub('"', '', line[7:-1])
title_edit = re.sub('[^a-zA-Z- ]', '', title)
title_sort = title_edit.lower()
if line.find('author:') == 0:
author = re.sub('"', '', line[8:-1])
if line.find('username:') == 0:
username = re.sub('"', '', line[10:-1])
if not username:
if not author:
username = 'unknown'
else:
username = author
if line.find('date:') == 0:
date = datetime.strptime(line[6:-1], '%Y-%m-%d %H:%M:%S')
if line.find(' - authors:') == 0:
source = True
# Write dictionary entry
#---------------------------------------------------------------------#
defs.append({'def_id': def_id, 'shortcut': shortcut, 'title': title, \
'username': username, 'date': date, 'source': source, \
'title_sort': title_sort})
# Count number of files
#-----------------------------------------------------------------------------#
del title_edit, title_sort
num_pr = len(proofs)
num_def = len(defs)
pr_ids = [proof['proof_id'] for proof in proofs]
def_ids = [definition['def_id'] for definition in defs]
pr_titles = [proof['title_sort'] for proof in proofs]
def_titles = [definition['title_sort'] for definition in defs]
# Output number of files
#-----------------------------------------------------------------------------#
print('\n-> StatProofBook Index Generator:')
print(' - ' + str(num_pr) + ' files found in proof directory!')
print(' - ' + str(num_def) + ' files found in definition directory!')
# Table of Contents: read index file
#-----------------------------------------------------------------------------#
print('\n1. Table of Contents:')
f1 = open('I/ToC.md', 'r')
ToCs = f1.readlines()
f1.close()
# Table of Contents: check for proof Shortcuts
#-----------------------------------------------------------------------------#
incl = np.zeros(num_pr, dtype=bool)
for (i, proof) in enumerate(proofs):
for line in ToCs:
if line.find('(/P/' + proof['shortcut'] + ')') > -1:
incl[i] = True
if ~incl[i]:
print(' - WARNING: proof "' + proof['shortcut'] + '" is not in table of contents!')
if all(incl):
print(' - ' + str(sum(incl)) + ' proofs found in table of contents!')
# Table of Contents: check for definition Shortcuts
#-----------------------------------------------------------------------------#
incl = np.zeros(num_def, dtype=bool)
for (i, definition) in enumerate(defs):
for line in ToCs:
if line.find('(/D/' + definition['shortcut'] + ')') > -1:
incl[i] = True
if ~incl[i]:
print(' - WARNING: definition "' + definition['shortcut'] + '" is not in table of contents!')
if all(incl):
print(' - ' + str(sum(incl)) + ' definitions found in table of contents!')
# Proof by Number: prepare index file
#-----------------------------------------------------------------------------#
print('\n2a.Proof by Number:')
f2a = open('I/PbN.md', 'w')
f2a.write('---\nlayout: page\ntitle: "Proof by Number"\n---\n\n\n')
f2a.write('| ID | Shortcut | Theorem | Author | Date |\n')
f2a.write('|:-- |:-------- |:------- |:------ |:---- |\n')
# Proof by Number: sort by Proof ID
#-----------------------------------------------------------------------------#
pr_nos = [int(pr_id[1:]) for pr_id in pr_ids]
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(pr_nos)])]
for i in sort_ind:
f2a.write('| ' + proofs[i]['proof_id'] + ' | ' + proofs[i]['shortcut'] + ' | [' + \
proofs[i]['title'] + '](/P/' + proofs[i]['shortcut'] + ') | ' + \
proofs[i]['username'] + ' | ' + proofs[i]['date'].strftime('%Y-%m-%d') + ' |\n')
f2a.close()
del pr_nos, sort_ind
print(' - successfully written to disk!')
# Definition by Number: prepare index file
#-----------------------------------------------------------------------------#
print('\n2b.Definition by Number:')
f2b = open('I/DbN.md', 'w')
f2b.write('---\nlayout: page\ntitle: "Definition by Number"\n---\n\n\n')
f2b.write('| ID | Shortcut | Theorem | Author | Date |\n')
f2b.write('|:-- |:-------- |:------- |:------ |:---- |\n')
# Definition by Number: sort by Definition ID
#-----------------------------------------------------------------------------#
def_nos = [int(def_id[1:]) for def_id in def_ids]
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(def_nos)])]
for i in sort_ind:
f2b.write('| ' + defs[i]['def_id'] + ' | ' + defs[i]['shortcut'] + ' | [' + \
defs[i]['title'] + '](/D/' + defs[i]['shortcut'] + ') | ' + \
defs[i]['username'] + ' | ' + defs[i]['date'].strftime('%Y-%m-%d') + ' |\n')
f2b.close()
del def_nos, sort_ind
print(' - successfully written to disk!')
# Proof by Topic: prepare index file
#-----------------------------------------------------------------------------#
print('\n3a.Proof by Topic:')
f3a = open('I/PbT.md', 'w')
f3a.write('---\nlayout: page\ntitle: "Proof by Topic"\n---\n\n')
# Proof by Topic: sort by Title
#-----------------------------------------------------------------------------#
prev_lett = '0'
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(pr_titles)])]
for i in sort_ind:
title_sort = pr_titles[i]
if title_sort[0] != prev_lett:
f3a.write('\n### ' + title_sort[0].upper() + '\n\n')
prev_lett = title_sort[0]
f3a.write('- [' + proofs[i]['title'] + '](/P/' + proofs[i]['shortcut'] + ')\n')
f3a.close()
del prev_lett, sort_ind, title_sort
print(' - successfully written to disk!')
# Definition by Topic: prepare index file
#-----------------------------------------------------------------------------#
print('\n3b.Definition by Topic:')
f3b = open('I/DbT.md', 'w')
f3b.write('---\nlayout: page\ntitle: "Definition by Topic"\n---\n\n')
# Definition by Topic: sort by Title
#-----------------------------------------------------------------------------#
prev_lett = '0'
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(def_titles)])]
for i in sort_ind:
title_sort = def_titles[i]
if title_sort[0] != prev_lett:
f3b.write('\n### ' + title_sort[0].upper() + '\n\n')
prev_lett = title_sort[0]
f3b.write('- [' + defs[i]['title'] + '](/D/' + defs[i]['shortcut'] + ')\n')
f3b.close()
del prev_lett, sort_ind, title_sort
print(' - successfully written to disk!')
# Proof by Author: prepare index file
#-----------------------------------------------------------------------------#
print('\n4a.Proof by Author:')
f4a = open('I/PbA.md', 'w')
f4a.write('---\nlayout: page\ntitle: "Proof by Author"\n---\n\n')
# Proof by Authors: sort by Username
#-----------------------------------------------------------------------------#
pr_words = ['proof', 'proofs']
pr_users = [proof['username'].lower() for proof in proofs]
sect_hdr = '\n### {} ({} {})\n\n'
unique_users = list(set(pr_users))
unique_users.sort()
for user in unique_users:
user_proofs = [proof for proof in proofs if proof['username'].lower() == user]
user_titles = [proof['title_sort'] for proof in user_proofs]
user_name = user_proofs[0]['username']
f4a.write(sect_hdr.format(user_name, len(user_proofs), pr_words[len(user_proofs)>1]))
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(user_titles)])]
for i in sort_ind:
f4a.write('- [' + user_proofs[i]['title'] + '](/P/' + user_proofs[i]['shortcut'] + ')\n')
f4a.close()
del pr_words, pr_users, sect_hdr, sort_ind
print(' - successfully written to disk!')
# Definition by Author: prepare index file
#-----------------------------------------------------------------------------#
print('\n4b.Definition by Author:')
f4b = open('I/DbA.md', 'w')
f4b.write('---\nlayout: page\ntitle: "Definition by Author"\n---\n\n')
# Definition by Authors: sort by Username
#-----------------------------------------------------------------------------#
def_words = ['definition', 'definitions']
def_users = [definition['username'].lower() for definition in defs]
sect_hdr = '\n### {} ({} {})\n\n'
unique_users = list(set(def_users))
unique_users.sort()
for user in unique_users:
user_defs = [definition for definition in defs if definition['username'].lower() == user]
user_titles = [definition['title_sort'] for definition in user_defs]
user_name = user_defs[0]['username']
f4b.write(sect_hdr.format(user_name, len(user_defs), def_words[len(user_defs)>1]))
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(user_titles)])]
for i in sort_ind:
f4b.write('- [' + user_defs[i]['title'] + '](/D/' + user_defs[i]['shortcut'] + ')\n')
f4b.close()
del def_words, def_users, sect_hdr, sort_ind
print(' - successfully written to disk!')
# Proofs without Source: prepare index file
#-----------------------------------------------------------------------------#
print('\n5a.Proofs without Source:')
f5a = open('I/PwS.md', 'w')
f5a.write('---\nlayout: page\ntitle: "Proofs without Source"\n---\n\n\n')
# Proofs without Source: sort by Title
#-----------------------------------------------------------------------------#
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(pr_titles)])]
for i in sort_ind:
source = proofs[i]['source']
if not source:
f5a.write('- [' + proofs[i]['title'] + '](/P/' + proofs[i]['shortcut'] + ')\n')
f5a.close()
del sort_ind, source
print(' - successfully written to disk!')
# Definitions without Source: prepare index file
#-----------------------------------------------------------------------------#
print('\n5b.Definitions without Source:')
f5b = open('I/DwS.md', 'w')
f5b.write('---\nlayout: page\ntitle: "Definitions without Source"\n---\n\n\n')
# Definitions without Source: sort by Title
#-----------------------------------------------------------------------------#
sort_ind = [i for (v, i) in sorted([(v, i) for (i, v) in enumerate(def_titles)])]
for i in sort_ind:
source = defs[i]['source']
if not source:
f5b.write('- [' + defs[i]['title'] + '](/D/' + defs[i]['shortcut'] + ')\n')
f5b.close()
del sort_ind, source
print(' - successfully written to disk!')