frankier commited on
Commit
534f038
·
1 Parent(s): a0b5f40

Change to 0-based labels

Browse files

Correspondingly out_of => scale_points

Files changed (1) hide show
  1. cross_domain_reviews.py +41 -26
cross_domain_reviews.py CHANGED
@@ -35,27 +35,17 @@ from datasets import concatenate_datasets, load_dataset
35
  class SubDataset:
36
  source: Any
37
  nick: str
38
- out_of: int
39
  get_review: Callable[[Any], Any]
40
  get_rating: Callable[[Any], Any]
41
 
42
 
43
- gi = operator.itemgetter
44
-
45
-
46
  def warn(msg):
47
  print(file=sys.stderr)
48
  print(f" ** Warning: {msg} **", file=sys.stderr)
49
  print(file=sys.stderr)
50
 
51
 
52
- def round_near(x, eps=0.001):
53
- x_rnd = int(x + 0.5)
54
- if abs(x_rnd - x) > eps:
55
- warn("got {x_rnd} when rounding {x}")
56
- return x_rnd
57
-
58
-
59
  @dataclass
60
  class SplitHFSrc:
61
  name: str
@@ -120,17 +110,42 @@ def int_or_drop(col):
120
  return inner
121
 
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  DATASETS = [
124
- SubDataset(SplitHFSrc("juliensimon/amazon-shoe-reviews"), "amazon-shoes", 5, gi("text"), lambda row: row["labels"] + 1),
125
  # TODO: Appears to be corrupt
126
  #SubDataset("florentgbelidji/edmunds-car-ratings", "car-ratings", 40, lambda row: row["Review"].strip(), lambda row: round_near(row["Rating"] * 8) - 7),
127
- SubDataset(TrainOnlyHFSrc("florentgbelidji/car-reviews"), "car-ratings", 5, gi("Review"), gi("Rating")),
128
- SubDataset(SplitHFSrc("codyburker/yelp_review_sampled"), "yelp", 5, gi("text"), gi("stars")),
129
- SubDataset(SplitHFSrc("kkotkar1/course-reviews"), "course-reviews", 5, gi("review"), gi("label")),
130
- SubDataset(TrainOnlyHFSrc("app_reviews"), "app-reviews", 5, gi("review"), gi("star")),
131
- SubDataset(TrainOnlyHFSrc("LoganKells/amazon_product_reviews_video_games"), "amazon-games", 5, gi("reviewText"), lambda row: round_near(row["overall"]) + 1),
132
- SubDataset(KaggleSrc("zynicide/wine-reviews", "winemag-data-130k-v2.csv"), "wine-reviews", 100, gi("description"), gi("points")),
133
- SubDataset(KaggleSrc("sadmadlad/imdb-user-reviews", "Pulp Fiction/movieReviews.csv"), "imdb-user-reviews", 10, gi("Review"), int_or_drop("User's Rating out of 10")),
134
  # TODO: Unicode decoding error
135
  #SubDataset(KaggleSrc("arushchillar/disneyland-reviews", "DisneylandReviews.csv"), "disneyland-reviews", 5, gi("Review_Text"), gi("Rating")),
136
  ]
@@ -152,7 +167,7 @@ class CrossDomainReviews(datasets.GeneratorBasedBuilder):
152
  {
153
  "text": datasets.Value("string"),
154
  "rating": datasets.Value("uint8"),
155
- "out_of": datasets.Value("uint8"),
156
  "dataset": datasets.Value("string")
157
  }
158
  )
@@ -202,26 +217,26 @@ class CrossDomainReviews(datasets.GeneratorBasedBuilder):
202
  rating = ds_info.get_rating(row)
203
  if rating is None:
204
  continue
205
- assert 1 <= rating <= ds_info.out_of, f"Expected {rating} (inclusively) between 1 and {ds_info.out_of}"
206
  lowest = min(lowest, rating)
207
  highest = max(highest, rating)
208
  yield key, {
209
  "text": review,
210
  "rating": rating,
211
- "out_of": ds_info.out_of,
212
  "dataset": ds_info.nick
213
  }
214
  key += 1
215
  got += 1
216
  if got >= 1000:
217
  break
218
- if lowest != 1:
219
  warn(
220
  f"Lowest rating in {ds_info.nick} was {lowest}, "
221
- "would suppose it would be 1"
222
  )
223
- if highest != ds_info.out_of:
224
  warn(
225
  f"Highest rating in {ds_info.nick} was {highest}, "
226
- f"would suppose it would be {ds_info.out_of}"
227
  )
 
35
  class SubDataset:
36
  source: Any
37
  nick: str
38
+ scale_points: int
39
  get_review: Callable[[Any], Any]
40
  get_rating: Callable[[Any], Any]
41
 
42
 
 
 
 
43
  def warn(msg):
44
  print(file=sys.stderr)
45
  print(f" ** Warning: {msg} **", file=sys.stderr)
46
  print(file=sys.stderr)
47
 
48
 
 
 
 
 
 
 
 
49
  @dataclass
50
  class SplitHFSrc:
51
  name: str
 
110
  return inner
111
 
112
 
113
+ gi = operator.itemgetter
114
+
115
+
116
+ def round_near(x, eps=0.001):
117
+ x_rnd = int(x + 0.5)
118
+ if abs(x_rnd - x) > eps:
119
+ warn("got {x_rnd} when rounding {x}")
120
+ return x_rnd
121
+
122
+
123
+ def dec(inner):
124
+ def wrap(x):
125
+ y = inner(x)
126
+ if y is None:
127
+ return None
128
+ res = y - 1
129
+ if res < 0:
130
+ warn(
131
+ "tried to convert 1-based index to 0-based index "
132
+ "but ended up with negative"
133
+ )
134
+ return res
135
+ return wrap
136
+
137
+
138
  DATASETS = [
139
+ SubDataset(SplitHFSrc("juliensimon/amazon-shoe-reviews"), "amazon-shoes", 5, gi("text"), gi("labels")),
140
  # TODO: Appears to be corrupt
141
  #SubDataset("florentgbelidji/edmunds-car-ratings", "car-ratings", 40, lambda row: row["Review"].strip(), lambda row: round_near(row["Rating"] * 8) - 7),
142
+ SubDataset(TrainOnlyHFSrc("florentgbelidji/car-reviews"), "car-ratings", 5, gi("Review"), dec(gi("Rating"))),
143
+ SubDataset(SplitHFSrc("codyburker/yelp_review_sampled"), "yelp", 5, gi("text"), dec(gi("stars"))),
144
+ SubDataset(SplitHFSrc("kkotkar1/course-reviews"), "course-reviews", 5, gi("review"), dec(gi("label"))),
145
+ SubDataset(TrainOnlyHFSrc("app_reviews"), "app-reviews", 5, gi("review"), dec(gi("star"))),
146
+ SubDataset(TrainOnlyHFSrc("LoganKells/amazon_product_reviews_video_games"), "amazon-games", 5, gi("reviewText"), lambda row: round_near(row["overall"])),
147
+ SubDataset(KaggleSrc("zynicide/wine-reviews", "winemag-data-130k-v2.csv"), "wine-reviews", 100, gi("description"), dec(gi("points"))),
148
+ 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"))),
149
  # TODO: Unicode decoding error
150
  #SubDataset(KaggleSrc("arushchillar/disneyland-reviews", "DisneylandReviews.csv"), "disneyland-reviews", 5, gi("Review_Text"), gi("Rating")),
151
  ]
 
167
  {
168
  "text": datasets.Value("string"),
169
  "rating": datasets.Value("uint8"),
170
+ "scale_points": datasets.Value("uint8"),
171
  "dataset": datasets.Value("string")
172
  }
173
  )
 
217
  rating = ds_info.get_rating(row)
218
  if rating is None:
219
  continue
220
+ assert 0 <= rating < ds_info.scale_points, f"Expected {rating} in half-open (Python-style) range [0, {ds_info.scale_points})"
221
  lowest = min(lowest, rating)
222
  highest = max(highest, rating)
223
  yield key, {
224
  "text": review,
225
  "rating": rating,
226
+ "scale_points": ds_info.scale_points,
227
  "dataset": ds_info.nick
228
  }
229
  key += 1
230
  got += 1
231
  if got >= 1000:
232
  break
233
+ if lowest != 0:
234
  warn(
235
  f"Lowest rating in {ds_info.nick} was {lowest}, "
236
+ "would suppose it would be 0"
237
  )
238
+ if highest != ds_info.scale_points - 1:
239
  warn(
240
  f"Highest rating in {ds_info.nick} was {highest}, "
241
+ f"would suppose it would be {ds_info.scale_points - 1}"
242
  )