Hamees Ul Hasan Sayed
commited on
Upload crawl.py
Browse files
crawl.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
import json
|
4 |
+
|
5 |
+
def read(filename):
|
6 |
+
"""Read the contents of a file."""
|
7 |
+
with open(filename, 'r') as f:
|
8 |
+
return f.read()
|
9 |
+
|
10 |
+
def read_ipynb(filename):
|
11 |
+
cells = json.loads(read(filename))["cells"]
|
12 |
+
|
13 |
+
text = ""
|
14 |
+
|
15 |
+
for i, cell in enumerate(cells):
|
16 |
+
if cell['cell_type'] == "markdown":
|
17 |
+
if i == 0:
|
18 |
+
text += f"{''.join(cell['source'])}\n"
|
19 |
+
else:
|
20 |
+
text += f"\n{''.join(cell['source'])}\n"
|
21 |
+
elif cell['cell_type'] == "code":
|
22 |
+
text += f"\n```\n{''.join(cell['source'])}\n```"
|
23 |
+
|
24 |
+
return text
|
25 |
+
|
26 |
+
def read_files(base):
|
27 |
+
"""Collect text and corresponding source paths from files in a directory."""
|
28 |
+
text = []
|
29 |
+
source = []
|
30 |
+
|
31 |
+
home = "./docs/index.rst"
|
32 |
+
installation = f"{base}/installation"
|
33 |
+
fundamentals = f"{base}/fundamentals"
|
34 |
+
notebooks = f"./docs/source/examples/notebooks/getting_started"
|
35 |
+
|
36 |
+
text.append(read(home))
|
37 |
+
source.append(f"PyBaMM/{home[2:]}")
|
38 |
+
|
39 |
+
for file in os.listdir(base):
|
40 |
+
if file == "getting_started.md":
|
41 |
+
text.append(read(f"{base}/{file}"))
|
42 |
+
source.append(f"PyBaMM/{base[2:]}/{file}")
|
43 |
+
|
44 |
+
for file in os.listdir(installation):
|
45 |
+
if file.endswith(".rst"):
|
46 |
+
text.append(read(f"{installation}/{file}"))
|
47 |
+
source.append(f"PyBaMM/{installation[2:]}/{file}")
|
48 |
+
|
49 |
+
for file in os.listdir(fundamentals):
|
50 |
+
if file.endswith(".md"):
|
51 |
+
text.append(read(f"{fundamentals}/{file}"))
|
52 |
+
source.append(f"PyBaMM/{fundamentals[2:]}/{file}")
|
53 |
+
|
54 |
+
for file in os.listdir(notebooks):
|
55 |
+
if file.endswith(".ipynb"):
|
56 |
+
text.append(read_ipynb(f"{notebooks}/{file}"))
|
57 |
+
source.append(f"PyBaMM/{notebooks[2:]}/{file}")
|
58 |
+
|
59 |
+
return text, source
|
60 |
+
|
61 |
+
|
62 |
+
def write_to_csv(text, source, output_file="dataset.csv"):
|
63 |
+
"""Write text and source paths to a CSV file."""
|
64 |
+
with open(output_file, mode='w') as dataset:
|
65 |
+
dataset = csv.writer(dataset, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
66 |
+
dataset.writerow(['text', 'source'])
|
67 |
+
for text, source in zip(text, source):
|
68 |
+
dataset.writerow([text, source])
|
69 |
+
|
70 |
+
|
71 |
+
def main():
|
72 |
+
base = "./docs/source/user_guide"
|
73 |
+
text, source = read_files(base)
|
74 |
+
write_to_csv(text, source)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
main()
|