| from datasets import DatasetInfo, GeneratorBasedBuilder, Split, SplitGenerator, load_dataset, Features, Value, Version |
|
|
| class BiasneutralDataset(GeneratorBasedBuilder): |
| """ |
| This dataset filters entries from BookCorpus based on provided indices in the BIASNEUTRAL dataset. |
| """ |
|
|
| VERSION = Version("1.0.1") |
| |
| _CITATION = """ |
| @misc{drechsel2025gradiendmonosemanticfeaturelearning, |
| title={{GRADIEND}: Feature Learning within Neural Networks Exemplified through Biases}, |
| author={Jonathan Drechsel and Steffen Herbold}, |
| year={2025}, |
| eprint={2502.01406}, |
| archivePrefix={arXiv}, |
| primaryClass={cs.LG}, |
| url={https://arxiv.org/abs/2502.01406}, |
| } |
| """ |
|
|
| def _info(self): |
| return DatasetInfo( |
| description="This dataset consists of BookCorpus entries containing only gender-neutral words (excluding e.g., he, actor, ...).", |
| features=Features({ |
| "index": Value("int32"), |
| "text": Value("string"), |
| }), |
| supervised_keys=None, |
| citation=self._CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| index_file_url = "https://huggingface.co/datasets/aieng-lab/biasneutral/resolve/main/indices.csv" |
|
|
| |
| indices_file = dl_manager.download_and_extract(index_file_url) |
|
|
| |
| print("Loading BookCorpus dataset...") |
| bookcorpus = load_dataset('bookcorpus', trust_remote_code=True)['train'] |
| print("BookCorpus dataset loaded.") |
|
|
| return [ |
| SplitGenerator(name=Split.TRAIN, gen_kwargs={"indices_file": indices_file, "bookcorpus": bookcorpus}), |
| ] |
|
|
| def _generate_examples(self, indices_file: str, bookcorpus): |
| """ |
| Generate examples by filtering the BookCorpus dataset using provided indices. |
| """ |
|
|
| |
| with open(indices_file, "r", encoding="utf-8") as f: |
| next(f) |
| indices_set = {int(line.strip().split(",")[0]) for line in f} |
|
|
| |
| for idx, sample in enumerate(bookcorpus): |
| if idx in indices_set: |
| yield idx, {"index": idx, "text": sample['text']} |
|
|