data_juicer.format.formatter module

class data_juicer.format.formatter.BaseFormatter[source]

Bases: object

Base class to load dataset.

load_dataset(*args) Dataset[source]
class data_juicer.format.formatter.LocalFormatter(dataset_path: str, type: str, suffixes: str | List[str] | None = None, text_keys: List[str] = None, add_suffix=False, **kwargs)[source]

Bases: BaseFormatter

The class is used to load a dataset from local files or local directory.

__init__(dataset_path: str, type: str, suffixes: str | List[str] | None = None, text_keys: List[str] = None, add_suffix=False, **kwargs)[source]

Initialization method.

Parameters:
  • dataset_path – path to a dataset file or a dataset directory

  • type – a packaged dataset module type (json, csv, etc.)

  • suffixes – files with specified suffixes to be processed

  • text_keys – key names of field that stores sample text.

  • add_suffix – whether to add the file suffix to dataset meta info

  • kwargs – extra args

load_dataset(num_proc: int = 1, global_cfg=None) Dataset[source]

Load a dataset from dataset file or dataset directory, and unify its format.

Parameters:
  • num_proc – number of processes when loading the dataset

  • global_cfg – global cfg used in consequent processes,

Returns:

formatted dataset

class data_juicer.format.formatter.RemoteFormatter(dataset_path: str, text_keys: List[str] = None, **kwargs)[source]

Bases: BaseFormatter

The class is used to load a dataset from repository of huggingface hub.

__init__(dataset_path: str, text_keys: List[str] = None, **kwargs)[source]

Initialization method.

Parameters:
  • dataset_path – a dataset file or a dataset directory

  • text_keys – key names of field that stores sample text.

  • kwargs – extra args

load_dataset(num_proc: int = 1, global_cfg=None) Dataset[source]

Load a dataset from HuggingFace, and unify its format.

Parameters:
  • num_proc – number of processes when loading the dataset

  • global_cfg – the global cfg used in consequent processes,

Returns:

formatted dataset

data_juicer.format.formatter.add_suffixes(datasets: DatasetDict, num_proc: int = 1) Dataset[source]

Add suffix filed to datasets.

Parameters:
  • datasets – a DatasetDict object

  • num_proc – number of processes to add suffixes

Returns:

datasets with suffix features.

data_juicer.format.formatter.unify_format(dataset: Dataset, text_keys: List[str] | str = 'text', num_proc: int = 1, global_cfg: dict | Namespace = None) Dataset[source]

Get an unified internal format, conduct the following modifications.

  1. check keys of dataset

  2. filter out those samples with empty or None text

Parameters:
  • dataset – input dataset

  • text_keys – original text key(s) of dataset.

  • num_proc – number of processes for mapping

  • global_cfg – the global cfg used in consequent processes, since cfg.text_key may be modified after unifying

Returns:

unified_format_dataset