Datasets:
Tasks:
Table to Text
Modalities:
Text
Languages:
English
Size:
10K - 100K
Tags:
data-to-text
License:
ratishsp commited on
Commit ·
c1d9d32
1
Parent(s): 46ea39f
added support to linearize the input
Browse files17 unmodified lines18192021222326 unmodified lines505152535455205 unmodified lines26126226326426526618 unmodified lines28528628728828917 unmodified linesimport csvimport jsonimport osimport datasets26 unmodified lines_URL = "data.tar.bz2"class MlbDataToText(datasets.GeneratorBasedBuilder):"""MLB dataset for data to text generation"""205 unmodified lines),]def _generate_examples(self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`):18 unmodified lines"summary_eval": data["summary_eval"],"gem_id": data["gem_id"],"target": data["summary_eval"],"references": [data["summary_eval"]]}17 unmodified lines1819202122232426 unmodified lines51525354555657585960616263646566676869707172737475767778798081205 unmodified lines28728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245318 unmodified lines47247347447547647717 unmodified linesimport csvimport jsonimport osimport reimport datasets26 unmodified lines_URL = "data.tar.bz2"team_verbalization_map = {"team_errors": "<TEAM_ERRORS>", "team_hits": "<TEAM_HITS>", "team_runs": "<TEAM_RUNS>"}pitcher_verbalization_map = {"p_bb": "<PITCH-BASE-ON-BALLS>", "p_er": "<EARNED-RUN>", "p_era": "<EARNED-RUN-AVG>","p_h": "<PITCH-HITS>", "p_hr": "<PITCH-HOME-RUN>", "p_l": "<PITCH-LOSS>","p_loss": "<PITCH-LOSING-PITCHER>", "p_s": "<PITCH-STRIKES-THROWN>","p_np": "<PITCH-COUNT>", "p_r": "<PITCH-RUNS>", "p_save": "<PITCH-SAVING-PITCHER>","p_so": "<PITCH-STRIKE-OUT>", "p_bf": "<PITCH-BATTERS-FACED>","p_bs": "<PITCH-BLOWN-SAVE>","p_sv": "<PITCH-SAVE>", "p_w": "<PITCH-WIN>", "p_ip1": "<INNINGS-PITCHED-1>","p_ip2": "<INNINGS-PITCHED-2>", "p_win": "<PITCH-WINNING-PITCHER>","p_out": "<PITCH-OUT>"}batter_verbalization_map = {"h": "<HITS>", "r": "<RUNS>", "hr": "<HOME-RUN>", "ab": "<ATBAT>", "avg": "<AVG>","rbi": "<RBI>", "cs": "<CAUGHT-STEAL>", "hbp": "<HIT-BY-PITCH>", "a": "<ASSIST>","bb": "<BASE-ON-BALL>", "e": "<ERROR>", "obp": "<ON-BASE-PCT>", "po": "<PUTOUT>","pos": "<POS>", "sb": "<STOLEN-BASE>", "sf": "<SAC-FLY>", "slg": "<SLUG>","so": "<STRIKEOUT>"}pbyp_verbalization_map = {"o": "<PBYP-OUTS>", "b": "<PBYP-BALLS>", "s": "<PBYP-STRIKES>", "b1": "<PBYP-B1>","b2": "<PBYP-B2>", "b3": "<PBYP-B3>", "batter": "<PBYP-BATTER>","pitcher": "<PBYP-PITCHER>","scorers": "<PBYP-SCORERS>", "event": "<PBYP-EVENT>", "event2": "<PBYP-EVENT2>","fielder_error": "<PBYP-FIELDER-ERROR>", "runs": "<PBYP-RUNS>", "rbi": "<PBYP-RBI>","error_runs": "<PBYP-ERROR-RUNS>", "top": "<TOP>", "bottom": "<BOTTOM>"}player_verbalization_map = dict(pitcher_verbalization_map, **batter_verbalization_map)class MlbDataToText(datasets.GeneratorBasedBuilder):"""MLB dataset for data to text generation"""205 unmodified lines),]def tokenize_initials(self, value):attrib_value = re.sub(r"(\w)\.(\w)\.", r"\g<1>. \g<2>.", value)return attrib_valuedef get_team_line_attributes(self, entry, name):if name == entry["home_line"]["team_name"]:line = entry["home_line"]type = "home"elif name == entry["vis_line"]["team_name"]:line = entry["vis_line"]type = "vis"else:assert Falsecity = line["team_city"]name = line["team_name"]result = line["result"]updated_type = "<" + type.upper() + ">"team_tup = (updated_type, name, city, result)team_line = "%s <TEAM> %s <CITY> %s <TEAM-RESULT> %s"sentence1 = team_line % (team_tup)other_attributes = []attributes = ["team_runs", "team_hits", "team_errors"]for attrib in attributes:template_string = " ".join([team_verbalization_map[attrib], "%s"])other_attributes.append(template_string % line[attrib])other_attributes = " ".join(other_attributes)team_info = sentence1if len(other_attributes) > 0:team_info = " ".join([sentence1, other_attributes])innings = line["innings"]inning_verbalization = []for inning in innings:inning_phrase = "<INN> %s %s" % (inning["inn"], inning["runs"])inning_verbalization.append(inning_phrase)inning_sentence = " ".join(inning_verbalization)team_info = " ".join([team_info, inning_sentence])return team_infodef get_player_line(self, entry):players = []for player in entry["box_score"]:if player["full_name"] == "N/A":continueplayer_line = "<PLAYER> %s <TEAM> %s <POS> %s"player_tup = (self.tokenize_initials(player["full_name"]), player["team"], player["pos"])player_basic_info = player_line % (player_tup)other_attributes = []for attrib in ["r", "h", "hr", "rbi", "e", "ab", "avg", "cs", "hbp", "bb", "sb", "sf", "so", "a", "po","p_ip1", "p_ip2", "p_w", "p_l", "p_h", "p_r", "p_er", "p_bb", "p_so", "p_hr", "p_np", "p_s","p_era", "p_win", "p_loss", "p_save", "p_sv", "p_bf", "p_out", "p_bs"]:if player[attrib] == "N/A":continueif attrib in ['sb', 'sf', 'e', 'po', 'a', 'cs', 'hbp', 'hr', 'so', 'bb', "p_hr", "p_sv","p_bs"] and int(player[attrib]) == 0:continueif attrib in ['avg'] and player[attrib] == ".000":continuetemplate_string = " ".join([player_verbalization_map[attrib], "%s"])other_attributes.append(template_string % (player[attrib]))player_other_attributes = " ".join(other_attributes)if other_attributes:player_info = " ".join([player_basic_info, player_other_attributes])else:player_info = player_basic_infoplayers.append(player_info)return playersdef get_runs_desc(self, inning_play):obs_desc = []for attrib in ["runs", "rbi", "error_runs"]:if attrib in inning_play and inning_play[attrib] != "N/A" and int(inning_play[attrib]) > 0:desc = " ".join([pbyp_verbalization_map[attrib], "%d"])obs_desc.append(desc % (int(inning_play[attrib])))return obs_descdef get_obs_desc(self, inning_play):obs_desc = []for attrib in ["o", "b", "s"]:if attrib in inning_play:desc = " ".join([pbyp_verbalization_map[attrib], "%d"])obs_desc.append(desc % (int(inning_play[attrib])))return obs_descdef get_name_desc(self, attrib, inning_play, obs_desc):if attrib in inning_play:desc = " ".join([pbyp_verbalization_map[attrib], "%s"])attrib_value = self.tokenize_initials(inning_play[attrib])obs_desc.append(desc % (attrib_value))def get_name_desc_entity(self, attrib, entity_name, obs_desc):desc = " ".join([pbyp_verbalization_map[attrib], "%s"])attrib_value = self.tokenize_initials(entity_name)obs_desc.append(desc % (attrib_value))def get_team_scores_desc(self, away, home, inning_play, obs_desc):if "home_team_runs" in inning_play and "away_team_runs" in inning_play and inning_play["home_team_runs"] != "N/A" and inning_play["away_team_runs"] != "N/A":desc = "<TEAM-SCORES> %s %d %s %d" % (home, int(inning_play["home_team_runs"]), away, int(inning_play["away_team_runs"]))obs_desc.append(desc)def get_attrib_value_desc(self, attrib, inning_play, obs_desc):if attrib in inning_play and inning_play[attrib] != "N/A":desc = " ".join([pbyp_verbalization_map[attrib], "%s"])obs_desc.append(desc % (inning_play[attrib]))def get_play_by_play_desc(self, home, away, inning, inning_play, play_index,top_bottom):inning_line = " ".join(["<INNING> %s", pbyp_verbalization_map[top_bottom], "<BATTING> %s <PITCHING> %s <PLAY> %d"])if top_bottom == "top":inning_attrib = (inning, away, home, play_index)else:inning_attrib = (inning, home, away, play_index)inning_desc = inning_line % (inning_attrib)other_attrib_desc = [inning_desc]other_attrib_desc.extend(self.get_runs_desc(inning_play))other_attrib_desc.extend(self.get_obs_desc(inning_play))for attrib in ["batter", "pitcher", "fielder_error"]:if attrib in inning_play and inning_play[attrib] != "N/A":self.get_name_desc(attrib, inning_play, other_attrib_desc)for attrib in ["scorers", "b2", "b3"]:if attrib in inning_play and len(inning_play[attrib]) > 0 and inning_play[attrib][0] != "N/A":for baserunner_instance in inning_play[attrib]:self.get_name_desc_entity(attrib, baserunner_instance, other_attrib_desc)self.get_attrib_value_desc("event", inning_play, other_attrib_desc)self.get_attrib_value_desc("event2", inning_play, other_attrib_desc)self.get_team_scores_desc(away, home, inning_play, other_attrib_desc)return other_attrib_descdef get_play_by_play_all_entities_inning(self, inning_data, home, away, inning, side):play_by_play_desc = []play_index = 1inning_plays = inning_data[side]for inning_play in inning_plays:other_attrib_desc = self.get_play_by_play_desc(home, away, inning, inning_play, play_index, side)other_attrib_desc = " ".join(other_attrib_desc)play_index += 1play_by_play_desc.append(other_attrib_desc)return play_by_play_descdef linearize_input(self, entry):output = []output.append(self.get_team_line_attributes(entry, entry["home_line"]["team_name"]))output.append(self.get_team_line_attributes(entry, entry["vis_line"]["team_name"]))output.extend(self.get_player_line(entry))for inning_data in entry['play_by_play']:for side in ["top", "bottom"]:pbyp_desc = self.get_play_by_play_all_entities_inning(inning_data, entry["home_line"]["team_name"],entry["vis_line"]["team_name"], inning_data['inning'],side)if pbyp_desc:output.append(" ".join(pbyp_desc))linearized_input = " ".join(output)linearized_input = linearized_input.replace(" ", " ")return linearized_inputdef _generate_examples(self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`):18 unmodified lines"summary_eval": data["summary_eval"],"gem_id": data["gem_id"],"target": data["summary_eval"],"references": [data["summary_eval"]],"linearized_input": self.linearize_input(data)}
mlb_data_to_text.py+189-1