File size: 8,103 Bytes
a0b5f40
b5a826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534f038
b5a826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534f038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a826e
534f038
b5a826e
 
534f038
 
 
 
 
 
 
b5a826e
 
 
 
 
 
 
 
 
 
 
972ccd2
b5a826e
 
 
 
 
 
 
 
 
534f038
a899692
b5a826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534f038
b5a826e
 
 
 
 
534f038
b5a826e
 
 
 
 
 
534f038
b5a826e
 
534f038
b5a826e
534f038
b5a826e
 
534f038
b5a826e
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright 2022 Frankie Robertson and The HuggingFace Datasets Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This dataset is a quick-and-dirty benchmark for predicting ratings across
different domains and on different rating scales based on text. It pulls in a
bunch of rating datasets, takes at most 1000 instances from each and combines
them into a big dataset.
"""

import datasets
from dataclasses import dataclass
from typing import Callable, Any
import operator
import shutil
import os
import sys
from os.path import join as pjoin

from datasets import concatenate_datasets, load_dataset


@dataclass
class SubDataset:
    source: Any
    nick: str
    scale_points: int
    get_review: Callable[[Any], Any]
    get_rating: Callable[[Any], Any]


def warn(msg):
    print(file=sys.stderr)
    print(f" ** Warning: {msg} **", file=sys.stderr)
    print(file=sys.stderr)


@dataclass
class SplitHFSrc:
    name: str

    def load(self):
        return load_dataset(self.name, streaming=True)


@dataclass
class TrainOnlyHFSrc:
    name: str

    def load(self):
        if hasattr(self, "_cached"):
            return self._cached
        self._cached = load_dataset(self.name)["train"].train_test_split(test_size=0.5, seed=42)
        return self._cached


_kaggle_api = None


def get_kaggle_api():
    global _kaggle_api 
    if _kaggle_api is not None:
        return _kaggle_api 
    from kaggle.api.kaggle_api_extended import KaggleApi
    _kaggle_api = KaggleApi()
    _kaggle_api.authenticate()
    return _kaggle_api


@dataclass
class KaggleSrc:
    name: str
    file: str

    def load(self):
        if hasattr(self, "_cached"):
            return self._cached
        kaggle_api = get_kaggle_api()
        dir_name = self.name.replace("/", "__")
        if os.path.exists(dir_name):
            shutil.rmtree(dir_name)
        os.mkdir(dir_name)
        try:
            file_path = pjoin(dir_name, self.file.rsplit("/", 1)[-1])
            kaggle_api.dataset_download_file(self.name, self.file, path=dir_name)
            dataset = load_dataset("csv", data_files=file_path + ".zip")
            return dataset["train"].train_test_split(test_size=0.5, seed=42)
        finally:
            shutil.rmtree(dir_name)


def int_or_drop(col):
    def inner(row):
        try:
            n = float(row[col])
        except ValueError:
            return None
        return round_near(n)
    return inner


gi = operator.itemgetter


def round_near(x, eps=0.001):
    x_rnd = int(x + 0.5)
    if abs(x_rnd - x) > eps:
        warn("got {x_rnd} when rounding {x}")
    return x_rnd


def dec(inner):
    def wrap(x):
        y = inner(x)
        if y is None:
            return None
        res = y - 1
        if res < 0:
            warn(
                "tried to convert 1-based index to 0-based index "
                "but ended up with negative"
            )
        return res
    return wrap


DATASETS = [
    SubDataset(SplitHFSrc("juliensimon/amazon-shoe-reviews"), "amazon-shoes", 5, gi("text"), gi("labels")),
    # TODO: Appears to be corrupt
    #SubDataset("florentgbelidji/edmunds-car-ratings", "car-ratings", 40, lambda row: row["Review"].strip(), lambda row: round_near(row["Rating"] * 8) - 7),
    SubDataset(TrainOnlyHFSrc("florentgbelidji/car-reviews"), "car-ratings", 5, gi("Review"), dec(gi("Rating"))),
    SubDataset(SplitHFSrc("codyburker/yelp_review_sampled"), "yelp", 5, gi("text"), dec(gi("stars"))),
    SubDataset(SplitHFSrc("kkotkar1/course-reviews"), "course-reviews", 5, gi("review"), dec(gi("label"))),
    SubDataset(TrainOnlyHFSrc("app_reviews"), "app-reviews", 5, gi("review"), dec(gi("star"))),
    SubDataset(TrainOnlyHFSrc("LoganKells/amazon_product_reviews_video_games"), "amazon-games", 5, gi("reviewText"), lambda row: round_near(row["overall"])),
    SubDataset(KaggleSrc("zynicide/wine-reviews", "winemag-data-130k-v2.csv"), "wine-reviews", 100, gi("description"), dec(gi("points"))),
    SubDataset(KaggleSrc("sadmadlad/imdb-user-reviews", "Pulp Fiction/movieReviews.csv"), "imdb-user-reviews", 10, gi("Review"), dec(int_or_drop("User's Rating out of 10"))),
    # TODO: Unicode decoding error
    #SubDataset(KaggleSrc("arushchillar/disneyland-reviews", "DisneylandReviews.csv"), "disneyland-reviews", 5, gi("Review_Text"), gi("Rating")),
]

_DESCRIPTION = __doc__

_HOMEPAGE = ""

_LICENSE = "Mixed"


class CrossDomainReviews(datasets.GeneratorBasedBuilder):
    _DESCRIPTION

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "text": datasets.Value("string"),
                "rating": datasets.Value("uint8"),
                "scale_points": datasets.Value("uint8"),
                "dataset": datasets.ClassLabel(names=[ds.nick for ds in DATASETS])
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            supervised_keys=("text", "rating"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation="",
        )

    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "split": "test"
                },
            ),
        ]

    def _generate_examples(self, split):
        key = 0
        for ds_info in DATASETS:
            subdataset = ds_info.source.load()
            lowest = float("inf")
            highest = float("-inf")
            got = 0
            for row in subdataset[split]:
                review = ds_info.get_review(row)
                if review is None:
                    continue
                rating = ds_info.get_rating(row)
                if rating is None:
                    continue
                assert 0 <= rating < ds_info.scale_points, f"Expected {rating} in half-open (Python-style) range [0, {ds_info.scale_points})"
                lowest = min(lowest, rating)
                highest = max(highest, rating)
                yield key, {
                    "text": review,
                    "rating": rating,
                    "scale_points": ds_info.scale_points,
                    "dataset": ds_info.nick
                }
                key += 1
                got += 1
                if got >= 1000:
                    break
            if lowest != 0:
                warn(
                    f"Lowest rating in {ds_info.nick} was {lowest}, "
                    "would suppose it would be 0"
                )
            if highest != ds_info.scale_points - 1:
                warn(
                    f"Highest rating in {ds_info.nick} was {highest}, "
                    f"would suppose it would be {ds_info.scale_points - 1}"
                )