import datasets import pandas as pd class ArXivPII(datasets.GeneratorBasedBuilder): BUILDER_CONFIG_CLASS = datasets.BuilderConfig def _info(self) -> datasets.DatasetInfo: features = datasets.Features({ "arxiv_id": datasets.Value("string"), "text": datasets.Value("string"), "labels": datasets.Sequence({ "start": datasets.Value("int16"), "end": datasets.Value("int16"), "label": datasets.ClassLabel(names=["NAME", "EMAIL", "PHONE"]), }), }) return datasets.DatasetInfo( features=features, description="ArXiv PII Named Entity Recognition Dataset", version=datasets.Version("1.0.0"), ) def _split_generators(self, dl_manager: datasets.DownloadManager): splits = [] url = "https://huggingface.co/datasets/sorokin/arxiv-pii/raw/main/data/" for name in ["2312", "2401"]: pages_path = dl_manager.download(url + name + "/pages.csv") labels_path = dl_manager.download(url + name + "/labels.csv") splits.append( datasets.SplitGenerator( name=name, gen_kwargs={"pages_path": pages_path, "labels_path": labels_path}, ) ) return splits def _generate_examples(self, pages_path, labels_path): example_id = 0 df_pages = pd.read_csv(pages_path, index_col="arxiv_id") df_labels = pd.read_csv(labels_path, index_col="arxiv_id") for arxiv_id, page in df_pages.iterrows(): labels = [] if arxiv_id in df_labels.index: rows = df_labels.loc[[arxiv_id]] for _, e in rows.iterrows(): labels.append({"start": e.start, "end": e.end, "label": e.label}) yield example_id, {"arxiv_id": arxiv_id, "text": page.text, "labels": labels} example_id += 1