File size: 3,156 Bytes
b109984
 
 
 
 
 
2d791da
b109984
 
 
 
 
 
 
 
2d791da
 
 
b109984
 
 
 
 
 
 
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
2d791da
b109984
 
 
 
 
 
 
 
 
2d791da
b109984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d791da
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""WRENCH classification dataset."""

import json

import datasets


class WrenchConfig(datasets.BuilderConfig):
    """BuilderConfig for WRENCH."""

    def __init__(
        self,
        dataset_path,
        **kwargs,
    ):
        super(WrenchConfig, self).__init__(
            version=datasets.Version("1.0.0", ""), **kwargs
        )
        self.dataset_path = dataset_path


class Wrench(datasets.GeneratorBasedBuilder):
    """WRENCH classification dataset."""

    BUILDER_CONFIGS = [
        WrenchConfig(
            name="imdb",
            dataset_path="./classification/imdb",
        ),
        WrenchConfig(
            name="yelp",
            dataset_path="./classification/yelp",
        ),
        WrenchConfig(
            name="youtube",
            dataset_path="./classification/youtube",
        ),
        WrenchConfig(
            name="sms",
            dataset_path="./classification/sms",
        ),
        WrenchConfig(
            name="agnews",
            dataset_path="./classification/agnews",
        ),
        WrenchConfig(
            name="trec",
            dataset_path="./classification/trec",
        ),
        WrenchConfig(
            name="cdr",
            dataset_path="./classification/cdr",
        ),
        WrenchConfig(
            name="semeval",
            dataset_path="./classification/semeval",
        ),
        WrenchConfig(
            name="chemprot",
            dataset_path="./classification/chemprot",
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "label": datasets.Value("int8"),
                    "weak_labels": datasets.Sequence(datasets.Value("int8")),
                }
            )
        )

    def _split_generators(self, dl_manager):
        dataset_path = self.config.dataset_path
        train_path = dl_manager.download_and_extract(f"{dataset_path}/train.json")
        valid_path = dl_manager.download_and_extract(f"{dataset_path}/valid.json")
        test_path = dl_manager.download_and_extract(f"{dataset_path}/test.json")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, gen_kwargs={"filepath": valid_path}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}
            ),
        ]

    def _generate_examples(self, filepath):
        """Generate Custom examples."""

        with open(filepath, encoding="utf-8") as f:
            json_data = json.load(f)

            for idx in json_data:
                data = json_data[idx]

                text = data["data"]["text"]
                weak_labels = data["weak_labels"]
                label = data["label"]

                yield int(idx), {
                    "text": text,
                    "label": label,
                    "weak_labels": weak_labels,
                }