|
from datasets import DatasetBuilder, GeneratorBasedBuilder, SplitGenerator, Features, Value |
|
import json |
|
import datasets |
|
import os |
|
from glob import glob |
|
import zstandard as zstd |
|
from datasets.utils import Version |
|
import io |
|
|
|
class MyUSPTODataset(GeneratorBasedBuilder): |
|
VERSION = Version("1.0.0") |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description="USPTO Patents Dataset", |
|
features=datasets.Features( |
|
{ |
|
"text": datasets.Value("string"), |
|
|
|
} |
|
), |
|
supervised_keys=None, |
|
|
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": "train", |
|
"split": None, |
|
}, |
|
|
|
), |
|
datasets.SplitGenerator( |
|
name="validation_pile", |
|
|
|
gen_kwargs={ |
|
"data_dir": "val", |
|
"split": "pile", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name="validation_domain", |
|
|
|
gen_kwargs={ |
|
"data_dir": "val", |
|
"split": "domain", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name="test_pile", |
|
|
|
gen_kwargs={ |
|
"data_dir": "test_pile", |
|
"split": "pile" |
|
}, |
|
|
|
), |
|
datasets.SplitGenerator( |
|
name="test_domain", |
|
|
|
gen_kwargs={ |
|
"data_dir": "test_domain", |
|
"split": "domain" |
|
}, |
|
|
|
), |
|
|
|
] |
|
|
|
def _generate_examples(self, data_dir, split): |
|
dctx = zstd.ZstdDecompressor() |
|
idx = -1 |
|
file_paths = glob(os.path.join(data_dir, f"*.jsonl.zst")) |
|
if split is not None: |
|
file_paths = [f for f in file_paths if split in f] |
|
for file in file_paths: |
|
with open(file, "rb") as f: |
|
reader = dctx.stream_reader(f) |
|
buffer = io.BufferedReader(reader) |
|
for _, line in enumerate(buffer.readlines()): |
|
data = json.loads(line) |
|
idx += 1 |
|
yield idx, data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|