Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/book_supplements_md.py
1191 views
1
import pandas as pd
2
import glob
3
import re
4
5
def make_markdown(infile, outfile, num, name, sep=','):
6
'''infile is a csv file, outfile is markdown'''
7
print('\nprocessing', infile)
8
df = pd.read_csv(infile, sep=sep)
9
notebook_url = 'https://colab.research.google.com/github/probml/probml-notebooks/blob/master/notebooks'
10
markdown_url = 'https://colab.research.google.com/github/probml/probml-notebooks/blob/master/markdown'
11
d2l_url = 'https://colab.research.google.com/github/probml/probml-notebooks/blob/master/notebooks-d2l'
12
13
contents = []
14
contents.append(f'# Chapter {num} ({name}): Supplementary material')
15
contents.append('|Title|Software|Link|')
16
contents.append('-|-|-')
17
18
for i in range(len(df)):
19
entry = df.loc[i]
20
title = entry['Title']
21
lang = entry['Language']
22
link = entry['Link']
23
entry_type = entry['Type']
24
if entry_type == 'Notebook':
25
link = f'{notebook_url}/{link}'
26
elif entry_type == 'Markdown':
27
link = f'{markdown_url}/{link}'
28
elif entry_type == 'd2lbook':
29
link = f'{d2l_url}/{link}'
30
line = f'|{title}|{lang}|[{entry_type}]({link})'
31
contents.append(line)
32
33
out = '\n'.join(contents)
34
with open(outfile, 'w') as f:
35
f.write(out)
36
37
38
def extract_chapter_num(fullname):
39
# fullname is eg '/Users/kpmurphy/github/pml-book/pml1/supplements/chap22.md'
40
# return 22
41
parts = fullname.split('/')
42
fname = parts[-1]
43
x = re.findall('\d{1,2}', fname)
44
y=int(x[0])
45
return y
46
47
def make_all_markdown(supplements_dir, titles):
48
filenames = glob.glob(f"{supplements_dir}/*.csv")
49
for f_csv in filenames:
50
f_md = f_csv.replace('csv', 'md')
51
num = extract_chapter_num(f_md)
52
name = titles[num]
53
print(f'making {f_md}, num {num}, name {name}')
54
make_markdown(f_csv, f_md, num, name)
55
56
57
def get_chapter_titles(fname):
58
df = pd.read_csv(fname, header=None, names=['Title'])
59
dd = {}
60
for i in range(len(df)):
61
chapnum = i + 1
62
chapname = df.loc[i]['Title']
63
dd[chapnum] = chapname
64
return dd
65
66
67
root='/Users/kpmurphy/github/pml-book/pml1'
68
69
titles = get_chapter_titles(f'{root}/TOC/chapters.txt')
70
71
make_all_markdown(f'{root}/supplements', titles)
72
73