-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Support load_json_file with json.load (#610)
support load_json_file with json.load
- Loading branch information
Showing
2 changed files
with
31 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
import os | ||
|
||
from datasets import Dataset, concatenate_datasets | ||
|
||
|
||
def load_json_file(data_files=None, data_dir=None, suffix=None): | ||
assert (data_files is not None) != (data_dir is not None) | ||
if data_dir is not None: | ||
data_files = os.listdir(data_dir) | ||
data_files = [os.path.join(data_dir, fn) for fn in data_files] | ||
if suffix is not None: | ||
data_files = [fp for fp in data_files if fp.endswith(suffix)] | ||
elif isinstance(data_files, str): | ||
data_files = [data_files] | ||
|
||
dataset_list = [] | ||
for fp in data_files: | ||
with open(fp, encoding='utf-8') as file: | ||
data = json.load(file) | ||
ds = Dataset.from_list(data) | ||
dataset_list.append(ds) | ||
dataset = concatenate_datasets(dataset_list) | ||
return dataset |