File size: 6,376 Bytes
5e9d12e
 
 
 
 
 
 
 
 
 
 
 
 
b11f1fe
5e9d12e
b11f1fe
5e9d12e
b11f1fe
 
5e9d12e
 
 
 
 
f5ea9ab
5e9d12e
 
 
 
b11f1fe
5e9d12e
 
 
 
 
 
 
 
 
 
 
b11f1fe
5e9d12e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b11f1fe
 
 
 
 
 
 
 
 
5e9d12e
 
 
 
 
 
 
 
 
b11f1fe
 
 
 
 
 
 
 
 
5e9d12e
 
 
 
 
 
 
 
 
b11f1fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d12e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import csv
import os

import datasets

logger = datasets.logging.get_logger(__name__)

_DESCRIPTION = """
"""

_URLS = {
    "ccd": "https://drive.google.com/u/0/uc?id=13UYcJ6BcojsCKy-yc9qgHTyBMCCWB-w1&export=download",
    "clothing": "https://drive.google.com/u/0/uc?id=1BwDS30xzFEDqP-z9adj4wVSlsSjOIGqc&export=download",
    "clothing_binary": "https://drive.google.com/u/0/uc?id=1P5aPKD0wU1NWUlh2QiIYwSPN-S3wD3ua&export=download",
    "electronics": "https://drive.google.com/u/0/uc?id=1ztIUsraLPJSKkkle_uTrd78hYsSKKmur&export=download",
    "electronics_binary": "https://drive.google.com/u/0/uc?id=103kJN6snOc2sSMH9ojd_PNohVQ1g4XUJ&export=download",
    "office": "https://drive.google.com/u/0/uc?id=1DbrvS02d75sXoxaaPh90bJdRvr15fUS5&export=download",
    "office_binary": "https://drive.google.com/u/0/uc?id=1ED4JnoTFu_4H80jBUJlqRrEor-taZ2Qz&export=download",
    "toxicity": "https://drive.google.com/u/0/uc?id=1iATGRaGuOqiUrj31jYjAzns8iS_BJh1h&export=download",
}

_FIELDS = {
    "amazon": ["date", "rating", "reviewText", "summary"],
    "ccd": ["date", "product", "subproduct", "issue", "subissue", "text"],
    "toxicity": ["rev_id", "toxicity", "date", "comment", "sample"],
}

_LABELS = {
    "amazon": ["1", "2", "3", "4", "5"],
    "amazon_binary": ["0", "1"],
    "ccd": [
        "Checking or savings account",
        "Credit card or prepaid card",
        "Credit reporting, credit repair services, or other personal consumer reports",
        "Debt collection",
        "Money transfer, virtual currency, or money service",
        "Mortgage",
        "Payday loan, title loan, or personal loan",
        "Student loan",
        "Vehicle loan or lease",
    ],
    "toxicity": [0, 1],
}


class QBConfig(datasets.BuilderConfig):
    def __init__(
        self,
        csv_fields,
        label_classes,
        label_column,
        text_column,
        url,
        **kwargs,
    ):
        super().__init__(version=datasets.Version("1.0.0", ""), **kwargs)
        self.csv_fields = csv_fields
        self.label_classes = label_classes
        self.label_column = label_column
        self.text_column = text_column
        self.url = url


class QB(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        QBConfig(
            name="ccd",
            description="Consumer Complaints Database",
            url=_URLS["ccd"],
            csv_fields=_FIELDS["ccd"],
            label_classes=_LABELS["ccd"],
            label_column="product",
            text_column="text",
        ),
        QBConfig(
            name="clothing",
            description="Amazon Reviews (Clothing)",
            url=_URLS["clothing"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="clothing_binary",
            description="Amazon Reviews (Clothing) with binary labels",
            url=_URLS["clothing_binary"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon_binary"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="electronics",
            description="Amazon Reviews (Electronics)",
            url=_URLS["electronics"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="electronics_binary",
            description="Amazon Reviews (Electronics) with binary labels",
            url=_URLS["electronics_binary"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon_binary"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="office",
            description="Amazon Reviews (Office)",
            url=_URLS["office"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="office_binary",
            description="Amazon Reviews (Office) with binary labels",
            url=_URLS["office_binary"],
            csv_fields=_FIELDS["amazon"],
            label_classes=_LABELS["amazon_binary"],
            label_column="rating",
            text_column="reviewText",
        ),
        QBConfig(
            name="toxicity",
            description="Wikipedia toxicity data set",
            url=_URLS["toxicity"],
            csv_fields=_FIELDS["toxicity"],
            label_classes=_LABELS["toxicity"],
            label_column="toxicity",
            text_column="comment",
        ),
    ]

    def _info(self):
        features = {
            "date": datasets.Value("string"),
            "id": datasets.Value("int32"),
            "label": datasets.features.ClassLabel(names=self.config.label_classes),
            "text": datasets.Value("string"),
        }
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(features),
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(self.config.url)
        logger.info(str(downloaded_files))
        train_filepath = os.path.join(downloaded_files, "train.csv")
        test_filepath = os.path.join(downloaded_files, "test.csv")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": train_filepath},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": test_filepath},
            ),
        ]

    def _generate_examples(self, filepath):
        logger.info(f"generating examples from {filepath}")
        idx = 0
        with open(filepath, encoding="utf-8") as f:
            reader = csv.DictReader(f, fieldnames=self.config.csv_fields)
            for row in reader:
                yield idx, {
                    "date": row["date"],
                    "id": idx,
                    "label": row[self.config.label_column],
                    "text": row[self.config.text_column],
                }
                idx += 1