|
import json |
|
import itertools |
|
from datasets import load_dataset |
|
|
|
|
|
def segment_cells(content): |
|
|
|
|
|
cells = [] |
|
cell_types = [] |
|
for cell in content['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'])) |
|
|
|
|
|
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: |
|
|
|
batch['cells'] = [['empty']] |
|
batch['cell_types'] = ['empty'] |
|
batch['cell_type_groups'] = [['empty']] |
|
|
|
del batch['content'] |
|
return batch |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
dataset = load_dataset("bigcode/the-stack",data_dir="data/jupyter-notebook", split="train",use_auth_token=True) |
|
|
|
dataset = dataset.map(segment) |
|
|
|
dataset = dataset.filter(lambda entry: entry['cell_types']!=['empty']) |
|
|
|
dataset.push_to_hub("bigcode/jupyter-parsed") |