jupyter-parsed / segment_notebooks.py
lingjzhu's picture
Upload segment_notebooks.py
152bd83
raw
history blame
1.76 kB
import json
import itertools
from datasets import load_dataset
def segment_cells(content):
# segment notebooks into lists of individual cells
cells = []
cell_types = []
for cell in content['cells']:
# select only non-empty cells
if len(cell['source']) != 0:
cells.append(' '.join(cell['source']))
cell_types.append(cell['cell_type'])
return cells, cell_types
def parse_notebook(batch):
try:
cells, types = segment_cells(json.loads(batch['content']))
# get cell types and group them into lists
cell_type_groups = [list(g) for k,g in itertools.groupby(types)]
cell_types = [k for k,g in itertools.groupby(types)]
cell_groups = []
group_start = 0
for g in cell_type_groups:
cell_groups.append(cells[group_start:group_start+len(g)])
group_start += len(g)
batch['cells'] = cell_groups
batch['cell_types'] = cell_types
batch['cell_type_groups'] = cell_type_groups
except:
# if json.loads() returns error, skip and add a placeholder
batch['cells'] = [['empty']]
batch['cell_types'] = ['empty']
batch['cell_type_groups'] = [['empty']]
del batch['content']
return batch
if __name__ == "__main__":
# load dataset
dataset = load_dataset("bigcode/the-stack",data_dir="data/jupyter-notebook", split="train",use_auth_token=True)
# segment notebooks
dataset = dataset.map(segment)
# filter out erronous cells via placeholders
dataset = dataset.filter(lambda entry: entry['cell_types']!=['empty'])
# push to hub
dataset.push_to_hub("bigcode/jupyter-parsed")