ratishsp commited on
Commit
f855023
1 Parent(s): 46ea3cf

support for linearized_input field

Browse files
Files changed (1) hide show
  1. RotoWire_English-German.py +271 -1
RotoWire_English-German.py CHANGED
@@ -18,7 +18,7 @@ import json
18
  import os
19
 
20
  import datasets
21
-
22
 
23
  # Find for instance the citation on arxiv or on the dataset repo/website
24
  _CITATION = """\
@@ -48,6 +48,13 @@ _URLs = {
48
  "test": "test.json"
49
  }
50
 
 
 
 
 
 
 
 
51
 
52
  class RotowireEnglishGerman(datasets.GeneratorBasedBuilder):
53
  """Dataset for WNGT2019 shared task on Document-level Generation and Translation."""
@@ -194,6 +201,268 @@ class RotowireEnglishGerman(datasets.GeneratorBasedBuilder):
194
  ),
195
  ]
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  def _generate_examples(
198
  self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
199
  ):
@@ -204,4 +473,5 @@ class RotowireEnglishGerman(datasets.GeneratorBasedBuilder):
204
  with open(filepath, encoding="utf-8") as f:
205
  all_data = json.load(f)
206
  for id_, data in enumerate(all_data):
 
207
  yield id_, data
 
18
  import os
19
 
20
  import datasets
21
+ import re
22
 
23
  # Find for instance the citation on arxiv or on the dataset repo/website
24
  _CITATION = """\
 
48
  "test": "test.json"
49
  }
50
 
51
+ NUM_PLAYERS = 13
52
+ player_line = "<PLAYER> %s <TEAM> %s <POS> %s <RANK> %s <MIN> %d <PTS> %d <FG> %d %d %d <FG3> %d %d %d " \
53
+ "<FT> %d %d %d <REB> %d <AST> %d <STL> %s " \
54
+ "<BLK> %d <DREB> %d <OREB> %d <TO> %d"
55
+
56
+ team_line = "%s <TEAM> %s <CITY> %s <TEAM-RESULT> %s <TEAM-PTS> %d <WINS-LOSSES> %d %d <QTRS> %d %d %d %d " \
57
+ "<TEAM-AST> %d <3PT> %d <TEAM-FG> %d <TEAM-FT> %d <TEAM-REB> %d <TEAM-TO> %d"
58
 
59
  class RotowireEnglishGerman(datasets.GeneratorBasedBuilder):
60
  """Dataset for WNGT2019 shared task on Document-level Generation and Translation."""
 
201
  ),
202
  ]
203
 
204
+ def handle_na(self, value):
205
+ return "0" if value == "N/A" else value
206
+
207
+ def tokenize_initials(self, value):
208
+ attrib_value = re.sub(r"(\w)\.(\w)\.", r"\g<1>. \g<2>.", value)
209
+ return attrib_value
210
+
211
+ def sort_points(self, entry):
212
+ home_team_map = {}
213
+ vis_team_map = {}
214
+ bs = entry["box_score"]
215
+ nplayers = 0
216
+ for k, v in bs["PTS"].items():
217
+ nplayers += 1
218
+
219
+ num_home, num_vis = 0, 0
220
+ home_pts = []
221
+ vis_pts = []
222
+ for i in range(nplayers):
223
+ player_city = entry["box_score"]["TEAM_CITY"][str(i)]
224
+ player_name = bs["PLAYER_NAME"][str(i)]
225
+ if player_city == entry["home_city"]:
226
+ if num_home < NUM_PLAYERS:
227
+ home_team_map[player_name] = bs["PTS"][str(i)]
228
+ if bs["PTS"][str(i)] != "N/A":
229
+ home_pts.append(int(bs["PTS"][str(i)]))
230
+ num_home += 1
231
+ else:
232
+ if num_vis < NUM_PLAYERS:
233
+ vis_team_map[player_name] = bs["PTS"][str(i)]
234
+ if bs["PTS"][str(i)] != "N/A":
235
+ vis_pts.append(int(bs["PTS"][str(i)]))
236
+ num_vis += 1
237
+ if entry["home_city"] == entry["vis_city"] and entry["home_city"] == "Los Angeles":
238
+ num_home, num_vis = 0, 0
239
+ for i in range(nplayers):
240
+ player_name = bs["PLAYER_NAME"][str(i)]
241
+ if num_vis < NUM_PLAYERS:
242
+ vis_team_map[player_name] = bs["PTS"][str(i)]
243
+ if bs["PTS"][str(i)] != "N/A":
244
+ vis_pts.append(int(bs["PTS"][str(i)]))
245
+ num_vis += 1
246
+ elif num_home < NUM_PLAYERS:
247
+ home_team_map[player_name] = bs["PTS"][str(i)]
248
+ if bs["PTS"][str(i)] != "N/A":
249
+ home_pts.append(int(bs["PTS"][str(i)]))
250
+ num_home += 1
251
+ home_seq = sorted(home_pts, reverse=True)
252
+ vis_seq = sorted(vis_pts, reverse=True)
253
+ return home_team_map, vis_team_map, home_seq, vis_seq
254
+
255
+ def sort_player_and_points(self, entry):
256
+ bs = entry["box_score"]
257
+ nplayers = 0
258
+ for k, v in bs["PTS"].items():
259
+ nplayers += 1
260
+
261
+ num_home, num_vis = 0, 0
262
+ home_pts = []
263
+ vis_pts = []
264
+ for i in range(nplayers):
265
+ player_city = entry["box_score"]["TEAM_CITY"][str(i)]
266
+ player_name = bs["PLAYER_NAME"][str(i)]
267
+ if player_city == entry["home_city"]:
268
+ if num_home < NUM_PLAYERS:
269
+ if bs["PTS"][str(i)] != "N/A":
270
+ home_pts.append((player_name, int(bs["PTS"][str(i)])))
271
+ else:
272
+ home_pts.append((player_name, -1))
273
+ num_home += 1
274
+ else:
275
+ if num_vis < NUM_PLAYERS:
276
+ if bs["PTS"][str(i)] != "N/A":
277
+ vis_pts.append((player_name, int(bs["PTS"][str(i)])))
278
+ else:
279
+ vis_pts.append((player_name, -1))
280
+ num_vis += 1
281
+ if entry["home_city"] == entry["vis_city"] and entry["home_city"] == "Los Angeles":
282
+ num_home, num_vis = 0, 0
283
+ for i in range(nplayers):
284
+ player_name = bs["PLAYER_NAME"][str(i)]
285
+ if num_vis < NUM_PLAYERS:
286
+ if bs["PTS"][str(i)] != "N/A":
287
+ vis_pts.append((player_name, int(bs["PTS"][str(i)])))
288
+ else:
289
+ vis_pts.append((player_name, -1))
290
+ num_vis += 1
291
+ elif num_home < NUM_PLAYERS:
292
+ if bs["PTS"][str(i)] != "N/A":
293
+ home_pts.append((player_name, int(bs["PTS"][str(i)])))
294
+ else:
295
+ home_pts.append((player_name, -1))
296
+ num_home += 1
297
+ home_seq = sorted(home_pts, key=lambda x: -x[1])
298
+ vis_seq = sorted(vis_pts, key=lambda x: -x[1])
299
+ return home_seq, vis_seq
300
+
301
+ def get_players(self, entry):
302
+ player_team_map = {}
303
+ bs = entry["box_score"]
304
+ nplayers = 0
305
+ home_players, vis_players = [], []
306
+ for k, v in entry["box_score"]["PTS"].items():
307
+ nplayers += 1
308
+
309
+ num_home, num_vis = 0, 0
310
+ for i in range(nplayers):
311
+ player_city = entry["box_score"]["TEAM_CITY"][str(i)]
312
+ player_name = bs["PLAYER_NAME"][str(i)]
313
+ second_name = bs["SECOND_NAME"][str(i)]
314
+ first_name = bs["FIRST_NAME"][str(i)]
315
+ if player_city == entry["home_city"]:
316
+ if len(home_players) < NUM_PLAYERS:
317
+ home_players.append((player_name, second_name,
318
+ first_name))
319
+ player_team_map[player_name] = " ".join(
320
+ [player_city, entry["home_line"]["TEAM-NAME"]])
321
+ num_home += 1
322
+ else:
323
+ if len(vis_players) < NUM_PLAYERS:
324
+ vis_players.append((player_name, second_name,
325
+ first_name))
326
+ player_team_map[player_name] = " ".join(
327
+ [player_city, entry["vis_line"]["TEAM-NAME"]])
328
+ num_vis += 1
329
+
330
+ if entry["home_city"] == entry["vis_city"] and entry["home_city"] == "Los Angeles":
331
+ home_players, vis_players = [], []
332
+ num_home, num_vis = 0, 0
333
+ for i in range(nplayers):
334
+ player_name = bs["PLAYER_NAME"][str(i)]
335
+ second_name = bs["SECOND_NAME"][str(i)]
336
+ first_name = bs["FIRST_NAME"][str(i)]
337
+ if len(vis_players) < NUM_PLAYERS:
338
+ vis_players.append((player_name, second_name,
339
+ first_name))
340
+ player_team_map[player_name] = " ".join(
341
+ ["Los Angeles", entry["vis_line"]["TEAM-NAME"]])
342
+ num_vis += 1
343
+ elif len(home_players) < NUM_PLAYERS:
344
+ home_players.append((player_name, second_name,
345
+ first_name))
346
+ player_team_map[player_name] = " ".join(
347
+ ["Los Angeles", entry["home_line"]["TEAM-NAME"]])
348
+ num_home += 1
349
+
350
+ players = []
351
+ for ii, player_list in enumerate([home_players, vis_players]):
352
+ for j in range(NUM_PLAYERS):
353
+ players.append(player_list[j] if j < len(player_list) else ("N/A", "N/A", "N/A"))
354
+ return players, player_team_map
355
+
356
+ def get_result_player(self, player_name, home_name, vis_name, home_won, player_team_map):
357
+ if player_team_map[player_name] == home_name:
358
+ result = "won" if home_won else "lost"
359
+ elif player_team_map[player_name] == vis_name:
360
+ result = "lost" if home_won else "won"
361
+ else:
362
+ assert False
363
+ return result
364
+
365
+ def get_box_score(self, entry):
366
+ box_score_ = entry["box_score"]
367
+ if int(entry["home_line"]["TEAM-PTS"]) > int(entry["vis_line"]["TEAM-PTS"]):
368
+ home_won = True
369
+ else:
370
+ home_won = False
371
+ descs = []
372
+ desc = []
373
+ if home_won:
374
+ home_line = self.get_team_line(entry["home_line"], "won", "home")
375
+ vis_line = self.get_team_line(entry["vis_line"], "lost", "vis")
376
+ else:
377
+ home_line = self.get_team_line(entry["home_line"], "lost", "home")
378
+ vis_line = self.get_team_line(entry["vis_line"], "won", "vis")
379
+ descs.append(home_line)
380
+ descs.append(vis_line)
381
+ players_list, player_team_map = self.get_players(entry)
382
+ home_team_map, vis_team_map, home_player_pts, vis_player_pts = self.sort_points(entry)
383
+ home_player_seq, vis_player_seq = self.sort_player_and_points(entry)
384
+ desc = []
385
+ for player_name, _ in home_player_seq + vis_player_seq:
386
+ result = self.get_result_player(player_name, entry["home_city"] + " " + entry["home_line"]["TEAM-NAME"],
387
+ entry["vis_city"] + " " + entry["vis_line"]["TEAM-NAME"], home_won,
388
+ player_team_map)
389
+ player_line = self.get_player_line(box_score_, player_name, player_team_map, home_player_pts,
390
+ vis_player_pts, home_team_map, vis_team_map, result)
391
+ desc.append(player_line)
392
+ descs.extend(desc)
393
+ return descs
394
+
395
+ def get_rank(self, player_name, home_seq, vis_seq, home_team_map, vis_team_map, result):
396
+ if player_name in home_team_map:
397
+ if home_team_map[player_name] == 'N/A':
398
+ rank = 'HOME-DIDNTPLAY'
399
+ else:
400
+ rank = 'HOME-' + str(home_seq.index(int(home_team_map[player_name])))
401
+ elif player_name in vis_team_map:
402
+ if vis_team_map[player_name] == 'N/A':
403
+ rank = 'VIS-DIDNTPLAY'
404
+ else:
405
+ rank = 'VIS-' + str(vis_seq.index(int(vis_team_map[player_name])))
406
+ else:
407
+ print("player_name", player_name)
408
+ assert False
409
+ return rank
410
+
411
+ def get_player_line(self, bs, input_player_name, player_team_map, home_player_pts, vis_player_pts, home_team_map,
412
+ vis_team_map, result):
413
+ rank = self.get_rank(input_player_name, home_player_pts, vis_player_pts, home_team_map, vis_team_map, result)
414
+ player_names = list(bs["PLAYER_NAME"].items())
415
+ player_found = False
416
+ player_tup = None
417
+ for (pid, name) in player_names:
418
+ if name == input_player_name:
419
+ player_tup = (self.tokenize_initials(name), player_team_map[input_player_name],
420
+ bs["START_POSITION"][pid],
421
+ rank,
422
+ int(self.handle_na(bs["MIN"][pid])),
423
+ int(self.handle_na(bs["PTS"][pid])),
424
+ int(self.handle_na(bs["FGM"][pid])),
425
+ int(self.handle_na(bs["FGA"][pid])), int(self.handle_na(bs["FG_PCT"][pid])),
426
+ int(self.handle_na(bs["FG3M"][pid])), int(self.handle_na(bs["FG3A"][pid])),
427
+ int(self.handle_na(bs["FG3_PCT"][pid])),
428
+ int(self.handle_na(bs["FTM"][pid])), int(self.handle_na(bs["FTA"][pid])),
429
+ int(self.handle_na(bs["FT_PCT"][pid])),
430
+ int(self.handle_na(bs["REB"][pid])), int(self.handle_na(bs["AST"][pid])),
431
+ int(self.handle_na(bs["STL"][pid])),
432
+ int(self.handle_na(bs["BLK"][pid])), int(self.handle_na(bs["DREB"][pid])),
433
+ int(self.handle_na(bs["OREB"][pid])), int(self.handle_na(bs["TO"][pid])))
434
+ player_found = True
435
+ break
436
+ assert player_found
437
+ return player_line % (player_tup)
438
+
439
+ def get_team_line(self, line, result, type):
440
+ city = line["TEAM-CITY"]
441
+ name = line["TEAM-NAME"]
442
+ wins = int(line["TEAM-WINS"])
443
+ losses = int(line["TEAM-LOSSES"])
444
+ pts = int(line["TEAM-PTS"])
445
+ ast = int(line["TEAM-AST"])
446
+ three_pointers_pct = int(line["TEAM-FG3_PCT"])
447
+ field_goals_pct = int(line["TEAM-FG_PCT"])
448
+ free_throws_pct = int(line["TEAM-FT_PCT"])
449
+ pts_qtr1 = int(line["TEAM-PTS_QTR1"])
450
+ pts_qtr2 = int(line["TEAM-PTS_QTR2"])
451
+ pts_qtr3 = int(line["TEAM-PTS_QTR3"])
452
+ pts_qtr4 = int(line["TEAM-PTS_QTR4"])
453
+ reb = int(line["TEAM-REB"])
454
+ tov = int(line["TEAM-TOV"])
455
+ updated_type = "<" + type.upper() + ">"
456
+ team_tup = (updated_type, name, city, result, pts, wins, losses, pts_qtr1, pts_qtr2, pts_qtr3, pts_qtr4, ast,
457
+ three_pointers_pct, field_goals_pct, free_throws_pct, reb, tov)
458
+ return team_line % (team_tup)
459
+
460
+ def linearize_input(self, entry):
461
+ output = self.get_box_score(entry)
462
+ linearized_input = " ".join(output)
463
+ linearized_input = linearized_input.replace(" ", " ")
464
+ return linearized_input
465
+
466
  def _generate_examples(
467
  self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
468
  ):
 
473
  with open(filepath, encoding="utf-8") as f:
474
  all_data = json.load(f)
475
  for id_, data in enumerate(all_data):
476
+ data['linearized_input'] = self.linearize_input(data)
477
  yield id_, data