jmnybl commited on
Commit
cdeab22
1 Parent(s): db6352d

dataloader

Browse files
Files changed (1) hide show
  1. turku_hockey_data2text.py +170 -0
turku_hockey_data2text.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Data Loader for Turku Hockey Data2Text corpus"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # Find for instance the citation on arxiv or on the dataset repo/website
26
+ _CITATION = """\
27
+ @inproceedings{kanerva2019newsgen,
28
+ Title = {Template-free Data-to-Text Generation of Finnish Sports News},
29
+ Author = {Jenna Kanerva and Samuel R{\"o}nnqvist and Riina Kekki and Tapio Salakoski and Filip Ginter},
30
+ booktitle = {Proceedings of the 22nd Nordic Conference on Computational Linguistics (NoDaLiDa’19)},
31
+ year={2019}
32
+ }
33
+ """
34
+
35
+
36
+ # You can copy an official description
37
+ _DESCRIPTION = """\
38
+ The Turku Hockey Data2Text corpus was developed as a benchmark for evaluating template-free, machine learning methods on Finnish news generation in the area of ice hockey reporting. This dataset is a collection of 3,454 ice hockey games, each including game statistics and a news article describing the game. Each game includes manual alignment of events (such as goals or penalties) and sentences describing the specific event in natural language extracted from the news article. The corpus includes 12,827 annotated events. The natural language passages are manually curated not to include any information not derivable from the input data or world knowledge.
39
+ """
40
+
41
+ _HOMEPAGE = "https://github.com/TurkuNLP/Turku-hockey-data2text"
42
+
43
+ _LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)"
44
+
45
+
46
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
47
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
48
+ _URLs = {
49
+ 'train': 'train.json',
50
+ 'validation': 'validation.json',
51
+ 'test': 'test.json'
52
+ }
53
+
54
+
55
+
56
+ class TurkuHockeyData2Text(datasets.GeneratorBasedBuilder):
57
+ """The Turky Hockey Data2Text is a manually curated corpus for Finnish news generation in the area of ice hockey reporting."""
58
+
59
+ VERSION = datasets.Version("1.1.0")
60
+
61
+ # This is an example of a dataset with multiple configurations.
62
+ # If you don't want/need to define several sub-sets in your dataset,
63
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
64
+
65
+ # If you need to make complex sub-parts in the datasets with configurable options
66
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
67
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
68
+
69
+ # You will be able to load one or the other configurations in the following list with
70
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
71
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
72
+ BUILDER_CONFIGS = [
73
+ datasets.BuilderConfig(name="main", version=VERSION, description="Main config"),
74
+ ]
75
+
76
+ DEFAULT_CONFIG_NAME = "main" # It's not mandatory to have a default configuration. Just use one if it make sense.
77
+
78
+ def _info(self):
79
+ # This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
80
+ #if self.config.name == "main": # This is the name of the configuration selected in BUILDER_CONFIGS above
81
+ features = datasets.Features(
82
+ {
83
+ "gem_id": datasets.Value("string"),
84
+ "id": datasets.Value("string"),
85
+ "news_article": datasets.Value("string"),
86
+ "events": datasets.features.Sequence(
87
+ {
88
+ "event_id": datasets.Value("string"),
89
+ "event_type": datasets.Value("string"),
90
+ "text": datasets.Value("string"),
91
+ "home_team": datasets.Value("string"),
92
+ "guest_team": datasets.Value("string"),
93
+ "score": datasets.Value("string"),
94
+ "periods": datasets.features.Sequence(datasets.Value("string")),
95
+ "features": datasets.features.Sequence(datasets.Value("string")),
96
+ "player": datasets.Value("string"),
97
+ "assist": datasets.features.Sequence(datasets.Value("string")),
98
+ "team": datasets.Value("string"),
99
+ "team_name": datasets.Value("string"),
100
+ "time": datasets.Value("string"),
101
+ "penalty_minutes": datasets.Value("string"),
102
+ "saves": datasets.Value("string"),
103
+ }
104
+ ),
105
+ }
106
+ )
107
+ # define other configs
108
+ return datasets.DatasetInfo(
109
+ description=_DESCRIPTION,
110
+ features=features,
111
+ # If there's a common (input, target) tuple from the features,
112
+ # specify them here. They'll be used if as_supervised=True in
113
+ # builder.as_dataset.
114
+ supervised_keys=None,
115
+ homepage=_HOMEPAGE,
116
+ license=_LICENSE,
117
+ citation=_CITATION,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+ """Returns SplitGenerators."""
122
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
123
+
124
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
125
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
126
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
127
+
128
+ my_urls = _URLs
129
+ data_dir = dl_manager.download_and_extract(my_urls)
130
+ return [
131
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir["train"], "split": "train"}),
132
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_dir["validation"], "split": "validation"}),
133
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_dir["test"], "split": "test"})
134
+ ]
135
+
136
+ def _generate_examples(self, filepath, split):
137
+ """ Yields examples as (key, example) tuples. """
138
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
139
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
140
+
141
+ with open(filepath, "rt", encoding="utf-8") as f:
142
+ data = json.load(f)
143
+ for i, example in enumerate(data):
144
+ example["gem_id"] = f"gem-turku_hockey_data2text-{split}-{i}" # fill in gem_id
145
+ example = self._generate_features(example)
146
+ yield i, example
147
+
148
+
149
+ def _generate_features(self, example):
150
+ _events = []
151
+ for e in example["events"]:
152
+ d = {"event_id": e["event_id"],
153
+ "event_type": e["event_type"],
154
+ "text": e["text"] if e["text"] != None else "",
155
+ "home_team": e.get("home_team", ""),
156
+ "guest_team": e.get("guest_team", ""),
157
+ "score": e.get("score", ""),
158
+ "periods": e.get("periods", []),
159
+ "features": e.get("features", []),
160
+ "player": e.get("player", []),
161
+ "assist": e.get("assist", []),
162
+ "team": e.get("team", ""),
163
+ "team_name": e.get("team_name", ""),
164
+ "time": e.get("time", ""),
165
+ "penalty_minutes": e.get("penalty_minutes", ""),
166
+ "saves": e.get("saves", ""),
167
+ }
168
+ _events.append(d)
169
+ example["events"] = _events
170
+ return example