Datasets:
Download parse.py from bleugreen/typescript-chunks: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/datasets/bleugreen/typescript-chunks/resolve/main/parse.py
- Command line
-
hf download hf://datasets/bleugreen/typescript-chunks/parse.py
-
curl -L -o parse.py https://huggingface.co/datasets/bleugreen/typescript-chunks/resolve/main/parse.py
1.13 kB
| import subprocess | |
| from datasets import load_dataset, Dataset | |
| import json | |
| from tqdm import tqdm | |
| ds = load_dataset("bigcode/the-stack-smol", data_dir='data/typescript') | |
| def split_ts_into_chunks(ts_code): | |
| result = subprocess.run( | |
| ['node', 'parse_ts.js'], | |
| input=ts_code, | |
| text=True, | |
| ) | |
| if result.returncode != 0: | |
| raise Exception('Error in TypeScript parsing') | |
| with open('semantic_chunks.jsonl', 'r') as file: | |
| lines = file.read().splitlines() | |
| chunks = [json.loads(line) for line in lines] | |
| with open('semantic_chunks.jsonl', 'w'): | |
| pass | |
| return chunks | |
| def chunk_ts_file(data): | |
| funcs = split_ts_into_chunks(data['content']) | |
| for i in range(len(funcs)): | |
| funcs[i]['repo'] = data['repository_name'] | |
| funcs[i]['path'] = data['path'] | |
| funcs[i]['language'] = data['lang'] | |
| return funcs | |
| chunks = [] | |
| for i in tqdm(range(len(ds['train']))): | |
| chunk = chunk_ts_file(ds['train'][i]) | |
| chunks +=(chunk) | |
| if i%100 == 0: | |
| print(len(chunks)) | |
| dataset = Dataset.from_list(chunks) | |
| print(dataset) | |
| dataset.to_json('ts-chunks.json') | |