File size: 1,214 Bytes
cbb6264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import subprocess
from datasets import load_dataset, Dataset
import json 
from tqdm import tqdm

# dataset streaming (will only download the data as needed)
ds = load_dataset("../datasets/the-stack-smol", data_dir='data/typescript')
print('loaded')

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')