github-jupyter-text-code-pairs / preprocessing.py
loubnabnl's picture
loubnabnl HF staff
add preprocessing file
8e9f6e1
raw
history blame
2.11 kB
from tqdm import tqdm
from datasets import load_dataset, Dataset
def parse_data(ds):
"""Parse data into markdown-code pairs"""
markdowns = []
code_snippets = []
paths = []
repo_names = []
licenses = []
for i in tqdm(range(len(ds))):
inner_markdowns = []
inner_code_snippets = []
types = ds[i]["types"]
path = ds[i]["path"]
repo = ds[i]["repo_name"]
license = ds[i]["license"]
if types[0] == "code":
# drop first cell of code to have the notebook start with markdown
cells = ds[i]["cells"][1:]
types = types[1:]
else:
# drop first the two cells of markdown followed by code
# the first markown cell of a notebook is often a long description of the whole notebook
cells = ds[i]["cells"][2:]
types = ds[i]["types"][2:]
if len(cells) % 2 == 0:
inner_markdowns = [cells[j] for j in range(len(cells)) if j % 2 == 0]
inner_code_snippets = [cells[j+1] for j in range(len(cells) - 1) if j % 2 == 0]
else:
# delete last markdown cell that has no code next
inner_markdowns = [cells[j] for j in range(len(cells) - 1) if j % 2 == 0]
inner_code_snippets = [cells[j+1] for j in range(len(cells) - 2) if j % 2 == 0]
markdowns.extend(inner_markdowns)
code_snippets.extend(inner_code_snippets)
paths.extend([path] * len(inner_markdowns))
repo_names.extend([repo] * len(inner_markdowns))
licenses.extend([license] * len(inner_markdowns))
return markdowns, code_snippets, paths, repo_names, licenses
if __name__ == "__main__":
ds = load_dataset("codeparrot/github-jupyter-parsed", split="train")
print("Parsing data...")
markdowns, code_snippets, paths, repo_names, licenses = parse_data(ds)
data = {"markdown": markdowns, "code": code_snippets, "path": paths, "repo_name": repo_names, "license": licenses}
parsed_data = Dataset.from_dict(data)
parsed_data.push_to_hub("codeparrot/github-jupyter-parsed-v2")