jmnybl commited on
Commit
5d652a2
1 Parent(s): 2be6a42

include event generation mode with input/target/references fields

Browse files
Files changed (1) hide show
  1. turku_hockey_data2text.py +103 -33
turku_hockey_data2text.py CHANGED
@@ -53,6 +53,12 @@ _URLs = {
53
  }
54
 
55
 
 
 
 
 
 
 
56
 
57
  class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
58
  """The Turky Hockey Data2Text is a manually curated corpus for Finnish news generation in the area of ice hockey reporting."""
@@ -71,41 +77,56 @@ class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
71
  # data = datasets.load_dataset('my_dataset', 'first_domain')
72
  # data = datasets.load_dataset('my_dataset', 'second_domain')
73
  BUILDER_CONFIGS = [
74
- datasets.BuilderConfig(name="main", version=VERSION, description="Main config"),
 
75
  ]
76
 
77
- DEFAULT_CONFIG_NAME = "main" # It's not mandatory to have a default configuration. Just use one if it make sense.
78
 
79
  def _info(self):
80
  # This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
81
- #if self.config.name == "main": # This is the name of the configuration selected in BUILDER_CONFIGS above
82
- features = datasets.Features(
83
- {
84
- "gem_id": datasets.Value("string"),
85
- "id": datasets.Value("string"),
86
- "news_article": datasets.Value("string"),
87
- "events": datasets.features.Sequence(
88
- {
89
  "event_id": datasets.Value("string"),
90
- "event_type": datasets.Value("string"),
91
- "text": datasets.Value("string"),
92
- "home_team": datasets.Value("string"),
93
- "guest_team": datasets.Value("string"),
94
- "score": datasets.Value("string"),
95
- "periods": datasets.features.Sequence(datasets.Value("string")),
96
- "features": datasets.features.Sequence(datasets.Value("string")),
97
- "player": datasets.Value("string"),
98
- "assist": datasets.features.Sequence(datasets.Value("string")),
99
- "team": datasets.Value("string"),
100
- "team_name": datasets.Value("string"),
101
- "time": datasets.Value("string"),
102
- "penalty_minutes": datasets.Value("string"),
103
- "saves": datasets.Value("string"),
104
- "multi_reference": datasets.Value("bool")
105
- }
106
- ),
107
- }
108
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  # define other configs
110
  return datasets.DatasetInfo(
111
  description=_DESCRIPTION,
@@ -142,10 +163,22 @@ class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
142
 
143
  with open(filepath, "rt", encoding="utf-8") as f:
144
  data = json.load(f)
145
- for i, example in enumerate(data):
146
- example["gem_id"] = f"gem-turku_hockey_data2text-{split}-{i}" # fill in gem_id
147
- example = self._generate_features(example)
148
- yield i, example
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
 
151
  def _generate_features(self, example):
@@ -191,3 +224,40 @@ class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
191
  if events[i]["event_id"] in multi_events:
192
  return True
193
  return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  }
54
 
55
 
56
+ # relevant keys in input representation for different event types (text and event_id skipped as not being relevant for the input)
57
+ relevant_keys = {"game result": ["event_type", "home_team", "guest_team", "score", "periods", "features"],\
58
+ "goal": ["event_type", "score", "features", "player", "assist", "team", "team_name", "time"],\
59
+ "penalty": ["event_type", "player", "team", "team_name", "time", "penalty_minutes"],\
60
+ "saves": ["event_type", "player", "team", "team_name", "saves"]}
61
+
62
 
63
  class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
64
  """The Turky Hockey Data2Text is a manually curated corpus for Finnish news generation in the area of ice hockey reporting."""
 
77
  # data = datasets.load_dataset('my_dataset', 'first_domain')
78
  # data = datasets.load_dataset('my_dataset', 'second_domain')
79
  BUILDER_CONFIGS = [
80
+ datasets.BuilderConfig(name="event-generation", version=VERSION, description="This loads the dataset in its event generation mode, where an output description is generated for each game event separately. The full game information is not availbale."),
81
+ datasets.BuilderConfig(name="game-generation", version=VERSION, description="This loads the dataset in its game generation mode, where the full game information is provided, but the actual input–output pairs must be created by the user."),
82
  ]
83
 
84
+ DEFAULT_CONFIG_NAME = "event-generation" # It's not mandatory to have a default configuration. Just use one if it make sense.
85
 
86
  def _info(self):
87
  # This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
88
+ if self.config.name == "event-generation": # This is the name of the configuration selected in BUILDER_CONFIGS above
89
+ features = datasets.Features(
90
+ {
91
+
92
+ "gem_id": datasets.Value("string"),
93
+ "game_id": datasets.Value("string"),
 
 
94
  "event_id": datasets.Value("string"),
95
+ "input": datasets.Value("string"),
96
+ "target": datasets.Value("string"),
97
+ "references": [datasets.Value("string")]
98
+ }
99
+ )
100
+
101
+ else: # game-generation
102
+ features = datasets.Features(
103
+ {
104
+ "gem_id": datasets.Value("string"),
105
+ "id": datasets.Value("string"),
106
+ "news_article": datasets.Value("string"),
107
+ "events": datasets.features.Sequence(
108
+ {
109
+ "event_id": datasets.Value("string"),
110
+ "event_type": datasets.Value("string"),
111
+ "text": datasets.Value("string"),
112
+ "home_team": datasets.Value("string"),
113
+ "guest_team": datasets.Value("string"),
114
+ "score": datasets.Value("string"),
115
+ "periods": datasets.features.Sequence(datasets.Value("string")),
116
+ "features": datasets.features.Sequence(datasets.Value("string")),
117
+ "player": datasets.Value("string"),
118
+ "assist": datasets.features.Sequence(datasets.Value("string")),
119
+ "team": datasets.Value("string"),
120
+ "team_name": datasets.Value("string"),
121
+ "time": datasets.Value("string"),
122
+ "penalty_minutes": datasets.Value("string"),
123
+ "saves": datasets.Value("string"),
124
+ "multi_reference": datasets.Value("bool")
125
+ }
126
+ ),
127
+ }
128
+ )
129
+
130
  # define other configs
131
  return datasets.DatasetInfo(
132
  description=_DESCRIPTION,
 
163
 
164
  with open(filepath, "rt", encoding="utf-8") as f:
165
  data = json.load(f)
166
+ idx = 0
167
+ for i, example in enumerate(data): # one game
168
+ if self.config.name == "game-generation":
169
+ example["gem_id"] = f"gem-turku_hockey_data2text-{split}-{idx}" # fill in gem_id
170
+ idx += 1
171
+ example = self._generate_features(example)
172
+ yield idx, example
173
+ else:
174
+ game_data = self._generate_features(example)
175
+ events = self.create_event_data(game_data["events"])
176
+ for e in events:
177
+ e["gem_id"] = f"gem-turku_hockey_data2text-{split}-{idx}" # fill in gem_id
178
+ idx += 1
179
+ e["game_id"] = game_data["id"]
180
+ yield idx, e
181
+
182
 
183
 
184
  def _generate_features(self, example):
 
224
  if events[i]["event_id"] in multi_events:
225
  return True
226
  return False
227
+
228
+
229
+
230
+
231
+ def create_event_data(self, events):
232
+ simplified_events = []
233
+ for i, event in enumerate(events):
234
+ e_idx, event_input, event_output = self.event2string(i, events)
235
+ if not event_input: # skip empty annotations and multi-reference events
236
+ continue
237
+ simplified_events.append({"gem_id": "", "game_id": "", "event_id": e_idx, "input": event_input, "target": event_output, "references": [event_output]})
238
+ return simplified_events
239
+
240
+ def event2string(self, i, events):
241
+ """Featurize i:th event into string input.
242
+ Example:
243
+ input: "event_type: saves [SEP] player: Jani Hurme [SEP] team: home [SEP] team_name: TPS [SEP] saves: 25"
244
+ output: "TPS:n maalissa Jani Hurme ehti 25 kiekon tielle."
245
+ """
246
+ if events[i]["text"] == "": # skip, event is not annotated
247
+ return None, None, None
248
+ if events[i]["multi_reference"] == True: # skip multireference events in simple representation
249
+ return None, None, None
250
+ event_type = events[i]["event_type"] # use only relevant features for this event type
251
+
252
+ e_data = []
253
+ for key in relevant_keys[event_type]:
254
+ if isinstance(events[i][key], str) or isinstance(events[i][key], float):
255
+ s = f"{key} = {events[i][key]}"
256
+ e_data.append(s)
257
+ if isinstance(events[i][key], list) and len(events[i][key]) > 0:
258
+ s = f'{key} = {" , ".join(events[i][key])}'
259
+ e_data.append(s)
260
+ event_input = " [SEP] ".join(e_data)
261
+
262
+ e_idx = events[i]["event_id"]
263
+ return e_idx, event_input, events[i]["text"]