ratishsp
commited on
Commit
•
c1d9d32
1
Parent(s):
46ea39f
added support to linearize the input
Browse files- mlb_data_to_text.py +189 -1
mlb_data_to_text.py
CHANGED
@@ -18,6 +18,7 @@
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
|
|
21 |
|
22 |
import datasets
|
23 |
|
@@ -50,6 +51,31 @@ _LICENSE = ""
|
|
50 |
|
51 |
_URL = "data.tar.bz2"
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
class MlbDataToText(datasets.GeneratorBasedBuilder):
|
55 |
"""MLB dataset for data to text generation"""
|
@@ -261,6 +287,167 @@ class MlbDataToText(datasets.GeneratorBasedBuilder):
|
|
261 |
),
|
262 |
]
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
def _generate_examples(
|
265 |
self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
266 |
):
|
@@ -285,5 +472,6 @@ class MlbDataToText(datasets.GeneratorBasedBuilder):
|
|
285 |
"summary_eval": data["summary_eval"],
|
286 |
"gem_id": data["gem_id"],
|
287 |
"target": data["summary_eval"],
|
288 |
-
"references": [data["summary_eval"]]
|
|
|
289 |
}
|
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
21 |
+
import re
|
22 |
|
23 |
import datasets
|
24 |
|
|
|
51 |
|
52 |
_URL = "data.tar.bz2"
|
53 |
|
54 |
+
team_verbalization_map = {"team_errors": "<TEAM_ERRORS>", "team_hits": "<TEAM_HITS>", "team_runs": "<TEAM_RUNS>"}
|
55 |
+
pitcher_verbalization_map = {"p_bb": "<PITCH-BASE-ON-BALLS>", "p_er": "<EARNED-RUN>", "p_era": "<EARNED-RUN-AVG>",
|
56 |
+
"p_h": "<PITCH-HITS>", "p_hr": "<PITCH-HOME-RUN>", "p_l": "<PITCH-LOSS>",
|
57 |
+
"p_loss": "<PITCH-LOSING-PITCHER>", "p_s": "<PITCH-STRIKES-THROWN>",
|
58 |
+
"p_np": "<PITCH-COUNT>", "p_r": "<PITCH-RUNS>", "p_save": "<PITCH-SAVING-PITCHER>",
|
59 |
+
"p_so": "<PITCH-STRIKE-OUT>", "p_bf": "<PITCH-BATTERS-FACED>",
|
60 |
+
"p_bs": "<PITCH-BLOWN-SAVE>",
|
61 |
+
"p_sv": "<PITCH-SAVE>", "p_w": "<PITCH-WIN>", "p_ip1": "<INNINGS-PITCHED-1>",
|
62 |
+
"p_ip2": "<INNINGS-PITCHED-2>", "p_win": "<PITCH-WINNING-PITCHER>",
|
63 |
+
"p_out": "<PITCH-OUT>"}
|
64 |
+
batter_verbalization_map = {"h": "<HITS>", "r": "<RUNS>", "hr": "<HOME-RUN>", "ab": "<ATBAT>", "avg": "<AVG>",
|
65 |
+
"rbi": "<RBI>", "cs": "<CAUGHT-STEAL>", "hbp": "<HIT-BY-PITCH>", "a": "<ASSIST>",
|
66 |
+
"bb": "<BASE-ON-BALL>", "e": "<ERROR>", "obp": "<ON-BASE-PCT>", "po": "<PUTOUT>",
|
67 |
+
"pos": "<POS>", "sb": "<STOLEN-BASE>", "sf": "<SAC-FLY>", "slg": "<SLUG>",
|
68 |
+
"so": "<STRIKEOUT>"
|
69 |
+
}
|
70 |
+
pbyp_verbalization_map = {"o": "<PBYP-OUTS>", "b": "<PBYP-BALLS>", "s": "<PBYP-STRIKES>", "b1": "<PBYP-B1>",
|
71 |
+
"b2": "<PBYP-B2>", "b3": "<PBYP-B3>", "batter": "<PBYP-BATTER>",
|
72 |
+
"pitcher": "<PBYP-PITCHER>",
|
73 |
+
"scorers": "<PBYP-SCORERS>", "event": "<PBYP-EVENT>", "event2": "<PBYP-EVENT2>",
|
74 |
+
"fielder_error": "<PBYP-FIELDER-ERROR>", "runs": "<PBYP-RUNS>", "rbi": "<PBYP-RBI>",
|
75 |
+
"error_runs": "<PBYP-ERROR-RUNS>", "top": "<TOP>", "bottom": "<BOTTOM>"}
|
76 |
+
|
77 |
+
player_verbalization_map = dict(pitcher_verbalization_map, **batter_verbalization_map)
|
78 |
+
|
79 |
|
80 |
class MlbDataToText(datasets.GeneratorBasedBuilder):
|
81 |
"""MLB dataset for data to text generation"""
|
|
|
287 |
),
|
288 |
]
|
289 |
|
290 |
+
def tokenize_initials(self, value):
|
291 |
+
attrib_value = re.sub(r"(\w)\.(\w)\.", r"\g<1>. \g<2>.", value)
|
292 |
+
return attrib_value
|
293 |
+
|
294 |
+
def get_team_line_attributes(self, entry, name):
|
295 |
+
if name == entry["home_line"]["team_name"]:
|
296 |
+
line = entry["home_line"]
|
297 |
+
type = "home"
|
298 |
+
elif name == entry["vis_line"]["team_name"]:
|
299 |
+
line = entry["vis_line"]
|
300 |
+
type = "vis"
|
301 |
+
else:
|
302 |
+
assert False
|
303 |
+
|
304 |
+
city = line["team_city"]
|
305 |
+
name = line["team_name"]
|
306 |
+
result = line["result"]
|
307 |
+
updated_type = "<" + type.upper() + ">"
|
308 |
+
team_tup = (updated_type, name, city, result)
|
309 |
+
team_line = "%s <TEAM> %s <CITY> %s <TEAM-RESULT> %s"
|
310 |
+
sentence1 = team_line % (team_tup)
|
311 |
+
other_attributes = []
|
312 |
+
attributes = ["team_runs", "team_hits", "team_errors"]
|
313 |
+
for attrib in attributes:
|
314 |
+
template_string = " ".join([team_verbalization_map[attrib], "%s"])
|
315 |
+
other_attributes.append(template_string % line[attrib])
|
316 |
+
other_attributes = " ".join(other_attributes)
|
317 |
+
team_info = sentence1
|
318 |
+
if len(other_attributes) > 0:
|
319 |
+
team_info = " ".join([sentence1, other_attributes])
|
320 |
+
innings = line["innings"]
|
321 |
+
inning_verbalization = []
|
322 |
+
for inning in innings:
|
323 |
+
inning_phrase = "<INN> %s %s" % (inning["inn"], inning["runs"])
|
324 |
+
inning_verbalization.append(inning_phrase)
|
325 |
+
inning_sentence = " ".join(inning_verbalization)
|
326 |
+
team_info = " ".join([team_info, inning_sentence])
|
327 |
+
return team_info
|
328 |
+
|
329 |
+
def get_player_line(self, entry):
|
330 |
+
players = []
|
331 |
+
for player in entry["box_score"]:
|
332 |
+
if player["full_name"] == "N/A":
|
333 |
+
continue
|
334 |
+
player_line = "<PLAYER> %s <TEAM> %s <POS> %s"
|
335 |
+
player_tup = (self.tokenize_initials(player["full_name"]), player["team"], player["pos"])
|
336 |
+
player_basic_info = player_line % (player_tup)
|
337 |
+
other_attributes = []
|
338 |
+
for attrib in ["r", "h", "hr", "rbi", "e", "ab", "avg", "cs", "hbp", "bb", "sb", "sf", "so", "a", "po",
|
339 |
+
"p_ip1", "p_ip2", "p_w", "p_l", "p_h", "p_r", "p_er", "p_bb", "p_so", "p_hr", "p_np", "p_s",
|
340 |
+
"p_era", "p_win", "p_loss", "p_save", "p_sv", "p_bf", "p_out", "p_bs"]:
|
341 |
+
if player[attrib] == "N/A":
|
342 |
+
continue
|
343 |
+
if attrib in ['sb', 'sf', 'e', 'po', 'a', 'cs', 'hbp', 'hr', 'so', 'bb', "p_hr", "p_sv",
|
344 |
+
"p_bs"] and int(player[attrib]) == 0:
|
345 |
+
continue
|
346 |
+
if attrib in ['avg'] and player[attrib] == ".000":
|
347 |
+
continue
|
348 |
+
template_string = " ".join([player_verbalization_map[attrib], "%s"])
|
349 |
+
other_attributes.append(template_string % (player[attrib]))
|
350 |
+
player_other_attributes = " ".join(other_attributes)
|
351 |
+
if other_attributes:
|
352 |
+
player_info = " ".join([player_basic_info, player_other_attributes])
|
353 |
+
else:
|
354 |
+
player_info = player_basic_info
|
355 |
+
|
356 |
+
players.append(player_info)
|
357 |
+
return players
|
358 |
+
|
359 |
+
def get_runs_desc(self, inning_play):
|
360 |
+
obs_desc = []
|
361 |
+
for attrib in ["runs", "rbi", "error_runs"]:
|
362 |
+
if attrib in inning_play and inning_play[attrib] != "N/A" and int(inning_play[attrib]) > 0:
|
363 |
+
desc = " ".join([pbyp_verbalization_map[attrib], "%d"])
|
364 |
+
obs_desc.append(desc % (int(inning_play[attrib])))
|
365 |
+
return obs_desc
|
366 |
+
|
367 |
+
def get_obs_desc(self, inning_play):
|
368 |
+
obs_desc = []
|
369 |
+
for attrib in ["o", "b", "s"]:
|
370 |
+
if attrib in inning_play:
|
371 |
+
desc = " ".join([pbyp_verbalization_map[attrib], "%d"])
|
372 |
+
obs_desc.append(desc % (int(inning_play[attrib])))
|
373 |
+
return obs_desc
|
374 |
+
|
375 |
+
def get_name_desc(self, attrib, inning_play, obs_desc):
|
376 |
+
if attrib in inning_play:
|
377 |
+
desc = " ".join([pbyp_verbalization_map[attrib], "%s"])
|
378 |
+
attrib_value = self.tokenize_initials(inning_play[attrib])
|
379 |
+
obs_desc.append(desc % (attrib_value))
|
380 |
+
|
381 |
+
def get_name_desc_entity(self, attrib, entity_name, obs_desc):
|
382 |
+
desc = " ".join([pbyp_verbalization_map[attrib], "%s"])
|
383 |
+
attrib_value = self.tokenize_initials(entity_name)
|
384 |
+
obs_desc.append(desc % (attrib_value))
|
385 |
+
|
386 |
+
def get_team_scores_desc(self, away, home, inning_play, obs_desc):
|
387 |
+
if "home_team_runs" in inning_play and "away_team_runs" in inning_play and inning_play[
|
388 |
+
"home_team_runs"] != "N/A" and inning_play["away_team_runs"] != "N/A":
|
389 |
+
desc = "<TEAM-SCORES> %s %d %s %d" % (
|
390 |
+
home, int(inning_play["home_team_runs"]), away, int(inning_play["away_team_runs"]))
|
391 |
+
obs_desc.append(desc)
|
392 |
+
|
393 |
+
def get_attrib_value_desc(self, attrib, inning_play, obs_desc):
|
394 |
+
if attrib in inning_play and inning_play[attrib] != "N/A":
|
395 |
+
desc = " ".join([pbyp_verbalization_map[attrib], "%s"])
|
396 |
+
obs_desc.append(desc % (inning_play[attrib]))
|
397 |
+
|
398 |
+
def get_play_by_play_desc(self, home, away, inning, inning_play, play_index,
|
399 |
+
top_bottom):
|
400 |
+
inning_line = " ".join(
|
401 |
+
["<INNING> %s", pbyp_verbalization_map[top_bottom], "<BATTING> %s <PITCHING> %s <PLAY> %d"])
|
402 |
+
if top_bottom == "top":
|
403 |
+
inning_attrib = (inning, away, home, play_index)
|
404 |
+
else:
|
405 |
+
inning_attrib = (inning, home, away, play_index)
|
406 |
+
inning_desc = inning_line % (inning_attrib)
|
407 |
+
other_attrib_desc = [inning_desc]
|
408 |
+
other_attrib_desc.extend(self.get_runs_desc(inning_play))
|
409 |
+
other_attrib_desc.extend(self.get_obs_desc(inning_play))
|
410 |
+
for attrib in ["batter", "pitcher", "fielder_error"]:
|
411 |
+
if attrib in inning_play and inning_play[attrib] != "N/A":
|
412 |
+
self.get_name_desc(attrib, inning_play, other_attrib_desc)
|
413 |
+
for attrib in ["scorers", "b2", "b3"]:
|
414 |
+
if attrib in inning_play and len(inning_play[attrib]) > 0 and inning_play[attrib][0] != "N/A":
|
415 |
+
for baserunner_instance in inning_play[attrib]:
|
416 |
+
self.get_name_desc_entity(attrib, baserunner_instance, other_attrib_desc)
|
417 |
+
self.get_attrib_value_desc("event", inning_play, other_attrib_desc)
|
418 |
+
self.get_attrib_value_desc("event2", inning_play, other_attrib_desc)
|
419 |
+
self.get_team_scores_desc(away, home, inning_play, other_attrib_desc)
|
420 |
+
return other_attrib_desc
|
421 |
+
|
422 |
+
def get_play_by_play_all_entities_inning(self, inning_data, home, away, inning, side):
|
423 |
+
play_by_play_desc = []
|
424 |
+
|
425 |
+
play_index = 1
|
426 |
+
inning_plays = inning_data[side]
|
427 |
+
for inning_play in inning_plays:
|
428 |
+
other_attrib_desc = self.get_play_by_play_desc(home, away, inning, inning_play, play_index, side)
|
429 |
+
other_attrib_desc = " ".join(other_attrib_desc)
|
430 |
+
play_index += 1
|
431 |
+
play_by_play_desc.append(other_attrib_desc)
|
432 |
+
return play_by_play_desc
|
433 |
+
|
434 |
+
def linearize_input(self, entry):
|
435 |
+
output = []
|
436 |
+
output.append(self.get_team_line_attributes(entry, entry["home_line"]["team_name"]))
|
437 |
+
output.append(self.get_team_line_attributes(entry, entry["vis_line"]["team_name"]))
|
438 |
+
output.extend(self.get_player_line(entry))
|
439 |
+
for inning_data in entry['play_by_play']:
|
440 |
+
for side in ["top", "bottom"]:
|
441 |
+
pbyp_desc = self.get_play_by_play_all_entities_inning(inning_data, entry["home_line"]["team_name"],
|
442 |
+
entry["vis_line"]["team_name"], inning_data['inning'],
|
443 |
+
side)
|
444 |
+
if pbyp_desc:
|
445 |
+
output.append(" ".join(pbyp_desc))
|
446 |
+
|
447 |
+
linearized_input = " ".join(output)
|
448 |
+
linearized_input = linearized_input.replace(" ", " ")
|
449 |
+
return linearized_input
|
450 |
+
|
451 |
def _generate_examples(
|
452 |
self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
453 |
):
|
|
|
472 |
"summary_eval": data["summary_eval"],
|
473 |
"gem_id": data["gem_id"],
|
474 |
"target": data["summary_eval"],
|
475 |
+
"references": [data["summary_eval"]],
|
476 |
+
"linearized_input": self.linearize_input(data)
|
477 |
}
|