Datasets:
GEM
/

Languages:
English
Multilinguality:
unknown
Size Categories:
unknown
Language Creators:
unknown
Annotations Creators:
none
Source Datasets:
original
Tags:
data-to-text
License:
ratishsp commited on
Commit
689a0b7
1 Parent(s): bf095ae

script to create data in GEM format; the input is in format introduced originally in the ACL 19 paper on data-to-text generation with entity modeling

Browse files
Files changed (1) hide show
  1. create_mlb_data.py +177 -0
create_mlb_data.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Script creates mlb data for gem benchmark
3
+ """
4
+ import argparse
5
+ import os
6
+ import json
7
+ import logging
8
+ logging.basicConfig(level=logging.INFO)
9
+
10
+
11
+ def sort_files_key(x):
12
+ if "train" in x:
13
+ file_index = int(x[5:7].strip(".")) # get the index of the train file
14
+ else:
15
+ file_index = -1 # valid and test
16
+ return file_index
17
+
18
+
19
+ def filter_summaries(summary_entry, seen_output, test_seen_output):
20
+ match_words = {"rain", "rains", "rained", "snow"}
21
+ filter = False
22
+ if len(summary_entry["summary"]) < 100:
23
+ filter = True
24
+ elif 100 < len(summary_entry["summary"]) < 300:
25
+ if len(match_words.intersection(set(summary_entry["summary"]))) > 0:
26
+ filter = True
27
+ elif "_".join(summary_entry["summary"][:50]) in seen_output: # retaining only one instance
28
+ filter = True
29
+ elif "_".join(summary_entry["summary"][:50]) in test_seen_output: # retaining only one instance
30
+ filter = True
31
+ return filter
32
+
33
+
34
+ def replace_Carmona(obj):
35
+ def decode_dict(a_dict):
36
+ for key, value in a_dict.items():
37
+ try:
38
+ if value == "Roberto Hernandez":
39
+ a_dict[key] = value.replace("Roberto Hernandez", "Fausto Carmona")
40
+ except AttributeError:
41
+ pass
42
+ return a_dict
43
+ return json.loads(json.dumps(obj), object_hook=decode_dict)
44
+
45
+
46
+ def process(input_folder, type, output_folder):
47
+ output_name_map = {"train": "train", "valid": "validation", "test": "test"}
48
+ output_jsonl = open(os.path.join(output_folder, output_name_map[type] + ".jsonl"), mode="w", encoding="utf-8")
49
+ file_list = os.listdir(input_folder)
50
+ sorted_file_list = sorted(file_list, key=sort_files_key)
51
+ seen_output = set()
52
+ test_seen_output = set()
53
+ if type == "train":
54
+ for filename in sorted_file_list:
55
+ if "valid" in filename or "test" in filename:
56
+ print("test filename", filename)
57
+ json_file = open(os.path.join(input_folder, filename), mode="r", encoding="utf-8")
58
+ data = json.load(json_file)
59
+ for entry_index, entry in enumerate(data):
60
+ test_seen_output.add("_".join(entry["summary"][:50]))
61
+
62
+ index = 1
63
+ for filename in sorted_file_list:
64
+ if type in filename:
65
+ print("filename", filename)
66
+ json_file = open(os.path.join(input_folder, filename), mode="r", encoding="utf-8")
67
+ data = json.load(json_file)
68
+ for entry_index, entry in enumerate(data):
69
+ logging.debug("instance %s", entry_index)
70
+ if type == "train":
71
+ if filter_summaries(entry, seen_output, test_seen_output):
72
+ continue
73
+ seen_output.add("_".join(entry["summary"][:50]))
74
+
75
+ summary = entry["summary"]
76
+ summ = " ".join(summary)
77
+ if "Fausto Carmona" in summ:
78
+ entry = replace_Carmona(entry)
79
+ gem_id = "GEM-mlb_data_to_text-"+ output_name_map[type] + "-" + str(index)
80
+ index += 1
81
+
82
+ updated_entry = {}
83
+ updated_entry["home_name"] = entry["home_name"]
84
+ updated_entry["vis_name"] = entry["vis_name"]
85
+ updated_entry["home_city"] = entry["home_city"]
86
+ updated_entry["vis_city"] = entry["vis_city"]
87
+ updated_entry["summary"] = entry["summary"]
88
+ updated_entry["summary_eval"] = " ".join(entry["summary"]).replace("*NEWPARAGRAPH* ", "")
89
+ updated_entry["day"] = entry["day"]
90
+ updated_entry["gem_id"] = gem_id
91
+ updated_entry["box_score"] = []
92
+ box_score_keys = entry["box_score"].keys()
93
+ construct_box_score(box_score_keys, entry["box_score"], updated_entry["box_score"])
94
+ assert len(updated_entry["box_score"]) == len(
95
+ entry["box_score"][list(box_score_keys)[-1]]) # checking sizes match
96
+ updated_entry["play_by_play"] = []
97
+ construct_play_by_play(entry["play_by_play"], updated_entry["play_by_play"])
98
+ updated_entry["vis_line"] = {}
99
+ updated_entry["home_line"] = {}
100
+ for attrib in ["team_runs", "result", "team_hits", "team_name", "team_errors", "team_city"]:
101
+ updated_entry["vis_line"][attrib] = entry["vis_line"][attrib]
102
+ updated_entry["home_line"][attrib] = entry["home_line"][attrib]
103
+ updated_entry["vis_line"]["innings"] = []
104
+ construct_inning_scores(entry["vis_line"]["innings"], updated_entry["vis_line"]["innings"])
105
+ updated_entry["home_line"]["innings"] = []
106
+ construct_inning_scores(entry["home_line"]["innings"], updated_entry["home_line"]["innings"])
107
+ json.dump(updated_entry, output_jsonl, ensure_ascii=False)
108
+ output_jsonl.write('\n')
109
+ if entry_index % 50 == 0:
110
+ print("entry_index", entry_index)
111
+ output_jsonl.close()
112
+
113
+
114
+ def construct_box_score(box_score_keys, box_score, box_score_list):
115
+ player_index = 0
116
+ while True:
117
+ box_score_object = {}
118
+ for _box_score_key in box_score_keys:
119
+ if str(player_index) not in box_score[_box_score_key]:
120
+ return
121
+ box_score_object[_box_score_key] = box_score[_box_score_key][str(player_index)]
122
+ box_score_list.append(box_score_object)
123
+ player_index += 1
124
+
125
+
126
+ def construct_play_by_play(play_by_play, play_by_play_list):
127
+ inning_index = 1
128
+ while True:
129
+ if str(inning_index) not in play_by_play:
130
+ return
131
+ play_by_play_object = {}
132
+ for side in ["top", "bottom"]:
133
+ if side in play_by_play[str(inning_index)]:
134
+ play_by_play_object[side] = []
135
+ for play in play_by_play[str(inning_index)][side]:
136
+ play_object = construct_play_object(play)
137
+ play_by_play_object[side].append(play_object)
138
+
139
+ play_by_play_object["inning"] = inning_index
140
+ play_by_play_list.append(play_by_play_object)
141
+ inning_index += 1
142
+
143
+
144
+ def construct_play_object(play):
145
+ play_object = {}
146
+ for attrib in ["runs", "pitcher", "o", "b", "s", "batter", "event", "event2", "home_team_runs", "away_team_runs",
147
+ "rbi", "error_runs", "fielder_error"]:
148
+ play_object[attrib] = "N/A"
149
+ if attrib in play:
150
+ play_object[attrib] = play[attrib]
151
+ for attrib in ["scorers", "b1", "b2", "b3"]:
152
+ play_object[attrib] = ["N/A"]
153
+ if attrib in play:
154
+ play_object[attrib] = play[attrib]
155
+ return play_object
156
+
157
+
158
+ def construct_inning_scores(innings, innings_list):
159
+ inning_index = 1
160
+ while True:
161
+ if "inn" + str(inning_index) not in innings:
162
+ return
163
+ innings_list.append({"inn": inning_index, "runs": innings["inn" + str(inning_index)]})
164
+ inning_index += 1
165
+
166
+
167
+ if __name__ == '__main__':
168
+ parser = argparse.ArgumentParser(description='Script for constructing the mlb dataset in GEM format')
169
+ parser.add_argument('-json_root', type=str,
170
+ help='path of json root; download from '
171
+ 'https://drive.google.com/drive/folders/1G4iIE-02icAU2-5skvLlTEPWDQQj1ss4', default=None)
172
+ parser.add_argument('-output_folder', type=str,
173
+ help='path of output file', default=None)
174
+ parser.add_argument('-dataset_type', type=str,
175
+ help='type of dataset', default=None)
176
+ args = parser.parse_args()
177
+ process(args.json_root, args.dataset_type, args.output_folder)