File size: 720 Bytes
02a40af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from datasets import Dataset
from sklearn.datasets import fetch_20newsgroups


def main():
    for split in ["train", "test"]:
        # Follow recommendation to strip newsgroup metadata
        # https://scikit-learn.org/0.19/datasets/twenty_newsgroups.html#filtering-text-for-more-realistic-training
        data = fetch_20newsgroups(subset=split, remove=("headers", "footers", "quotes"))
        id2label = {idx: label for idx, label in enumerate(data["target_names"])}
        d = {"text": data["data"], "label": data["target"]}
        dset = Dataset.from_dict(d)
        dset = dset.map(lambda x: {"label_text": id2label[x["label"]]})
        dset.to_json(f"{split}.jsonl")


if __name__ == "__main__":
    main()