adithya7 commited on
Commit ·
98aff4c
1
Parent(s): 4376b3e
add dataset script
Browse files- dataset_infos.json +0 -0
- xlel_wd.py +315 -0
dataset_infos.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
xlel_wd.py
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
| 15 |
+
"""TODO: Add a description here."""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
_CITATION = """\
|
| 23 |
+
@article{pratapa-etal-2022-multilingual,
|
| 24 |
+
title = {Multilingual Event Linking to Wikidata},
|
| 25 |
+
author = {Pratapa, Adithya and Gupta, Rishubh and Mitamura, Teruko},
|
| 26 |
+
publisher = {arXiv},
|
| 27 |
+
year = {2022},
|
| 28 |
+
url = {https://arxiv.org/abs/2204.06535},
|
| 29 |
+
}
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
_DESCRIPTION = """\
|
| 33 |
+
XLEL-WD is a multilingual event linking dataset. \
|
| 34 |
+
This dataset contains mention references from multilingual Wikipedia/Wikinews articles to event items in Wikidata. \
|
| 35 |
+
The text descriptions for Wikidata events are compiled from Wikipedia articles.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
_HOMEPAGE = "https://github.com/adithya7/xlel-wd"
|
| 39 |
+
|
| 40 |
+
_LICENSE = "CC-BY-4.0"
|
| 41 |
+
|
| 42 |
+
# TODO: Add link to the official dataset URLs here
|
| 43 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 44 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 45 |
+
_URLS = {
|
| 46 |
+
"wikipedia-zero-shot": {
|
| 47 |
+
"train": "wikipedia.train.jsonl",
|
| 48 |
+
"dev": "wikipedia-zero-shot.dev.jsonl",
|
| 49 |
+
"test": "wikipedia-zero-shot.test.jsonl",
|
| 50 |
+
},
|
| 51 |
+
"wikinews-zero-shot": {"test": "wikinews-zero-shot.test.jsonl"},
|
| 52 |
+
"wikinews-cross-domain": {"test": "wikinews-cross-domain.test.jsonl"},
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
_WIKIPEDIA_ZERO_SHOT_LANGS = [
|
| 56 |
+
"af",
|
| 57 |
+
"ar",
|
| 58 |
+
"be",
|
| 59 |
+
"bg",
|
| 60 |
+
"bn",
|
| 61 |
+
"ca",
|
| 62 |
+
"cs",
|
| 63 |
+
"da",
|
| 64 |
+
"de",
|
| 65 |
+
"el",
|
| 66 |
+
"en",
|
| 67 |
+
"es",
|
| 68 |
+
"fa",
|
| 69 |
+
"fi",
|
| 70 |
+
"fr",
|
| 71 |
+
"he",
|
| 72 |
+
"hi",
|
| 73 |
+
"hu",
|
| 74 |
+
"id",
|
| 75 |
+
"it",
|
| 76 |
+
"ja",
|
| 77 |
+
"ko",
|
| 78 |
+
"ml",
|
| 79 |
+
"mr",
|
| 80 |
+
"ms",
|
| 81 |
+
"nl",
|
| 82 |
+
"no",
|
| 83 |
+
"pl",
|
| 84 |
+
"pt",
|
| 85 |
+
"ro",
|
| 86 |
+
"ru",
|
| 87 |
+
"si",
|
| 88 |
+
"sk",
|
| 89 |
+
"sl",
|
| 90 |
+
"sr",
|
| 91 |
+
"sv",
|
| 92 |
+
"sw",
|
| 93 |
+
"ta",
|
| 94 |
+
"te",
|
| 95 |
+
"th",
|
| 96 |
+
"tr",
|
| 97 |
+
"uk",
|
| 98 |
+
"vi",
|
| 99 |
+
"zh",
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
_WIKINEWS_CROSS_DOMAIN_LANGS = [
|
| 103 |
+
"ar",
|
| 104 |
+
"bg",
|
| 105 |
+
"ca",
|
| 106 |
+
"cs",
|
| 107 |
+
"de",
|
| 108 |
+
"el",
|
| 109 |
+
"en",
|
| 110 |
+
"es",
|
| 111 |
+
"fi",
|
| 112 |
+
"fr",
|
| 113 |
+
"he",
|
| 114 |
+
"hu",
|
| 115 |
+
"it",
|
| 116 |
+
"ja",
|
| 117 |
+
"ko",
|
| 118 |
+
"nl",
|
| 119 |
+
"no",
|
| 120 |
+
"pl",
|
| 121 |
+
"pt",
|
| 122 |
+
"ro",
|
| 123 |
+
"ru",
|
| 124 |
+
"sr",
|
| 125 |
+
"sv",
|
| 126 |
+
"ta",
|
| 127 |
+
"tr",
|
| 128 |
+
"uk",
|
| 129 |
+
"zh",
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
_WIKINEWS_ZERO_SHOT_LANGS = [
|
| 133 |
+
"ar",
|
| 134 |
+
"cs",
|
| 135 |
+
"de",
|
| 136 |
+
"en",
|
| 137 |
+
"es",
|
| 138 |
+
"fi",
|
| 139 |
+
"fr",
|
| 140 |
+
"it",
|
| 141 |
+
"ja",
|
| 142 |
+
"ko",
|
| 143 |
+
"nl",
|
| 144 |
+
"no",
|
| 145 |
+
"pl",
|
| 146 |
+
"pt",
|
| 147 |
+
"ru",
|
| 148 |
+
"sr",
|
| 149 |
+
"sv",
|
| 150 |
+
"ta",
|
| 151 |
+
"tr",
|
| 152 |
+
"uk",
|
| 153 |
+
"zh",
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
+
_TASK_NAMES = []
|
| 157 |
+
_TASK_DESCRIPTIONS = []
|
| 158 |
+
|
| 159 |
+
# wikipedia based tasks
|
| 160 |
+
_TASK_NAMES += ["wikipedia-zero-shot"]
|
| 161 |
+
_TASK_DESCRIPTIONS += [
|
| 162 |
+
"This task requires linking mentions from multilingual wiki to Wikidata events (zero-shot evaluation)"
|
| 163 |
+
]
|
| 164 |
+
|
| 165 |
+
for lang in _WIKIPEDIA_ZERO_SHOT_LANGS:
|
| 166 |
+
_TASK_NAMES += [f"wikipedia-zero-shot.{lang}"]
|
| 167 |
+
_TASK_DESCRIPTIONS += [
|
| 168 |
+
f"This task requires linking mentions from {lang}wiki to Wikidata events (zero-shot evaluation)."
|
| 169 |
+
]
|
| 170 |
+
|
| 171 |
+
# wikinews based tasks (zero-shot)
|
| 172 |
+
_TASK_NAMES += ["wikinews-zero-shot"]
|
| 173 |
+
_TASK_DESCRIPTIONS += [
|
| 174 |
+
"This task requires linking mentions from multilingual wikinews to Wikidata events (zero-shot evaluation)."
|
| 175 |
+
]
|
| 176 |
+
for lang in _WIKINEWS_ZERO_SHOT_LANGS:
|
| 177 |
+
_TASK_NAMES += [f"wikinews-zero-shot.{lang}"]
|
| 178 |
+
_TASK_DESCRIPTIONS += [
|
| 179 |
+
f"This task requires linking mentions from {lang}wikinews to Wikidata events (zero-shot evaluation)."
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
# wikinews based tasks (cross-domain)
|
| 183 |
+
_TASK_NAMES += ["wikinews-cross-domain"]
|
| 184 |
+
_TASK_DESCRIPTIONS += [
|
| 185 |
+
"This task requires linking mentions from multilingual wikinews to Wikidata events (cross-domain evaluation)."
|
| 186 |
+
]
|
| 187 |
+
for lang in _WIKINEWS_CROSS_DOMAIN_LANGS:
|
| 188 |
+
_TASK_NAMES += [f"wikinews-cross-domain.{lang}"]
|
| 189 |
+
_TASK_DESCRIPTIONS += [
|
| 190 |
+
f"This task requires linking mentions from {lang}wikinews to Wikidata events (cross-domain evaluation)."
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class XlelWdConfig(datasets.BuilderConfig):
|
| 195 |
+
"""BuilderConfig for XLEL-WD"""
|
| 196 |
+
|
| 197 |
+
def __init__(self, features, citation, url, **kwargs) -> None:
|
| 198 |
+
super(XlelWdConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 199 |
+
self.features = features
|
| 200 |
+
self.citation = citation
|
| 201 |
+
self.url = url
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class XlelWd(datasets.GeneratorBasedBuilder):
|
| 205 |
+
"""A dataset for multilingual linking of event mentions to Wikidata."""
|
| 206 |
+
|
| 207 |
+
VERSION = datasets.Version("1.0.0")
|
| 208 |
+
# the features different slightly for Wikipedia and Wikinews
|
| 209 |
+
# Wikinews dataset also contains the article title and publication date
|
| 210 |
+
BUILDER_CONFIGS = [
|
| 211 |
+
XlelWdConfig(
|
| 212 |
+
name=name,
|
| 213 |
+
description=desc,
|
| 214 |
+
features=["mention", "context_left", "context_right", "context_lang"],
|
| 215 |
+
citation=_CITATION,
|
| 216 |
+
url=_URLS[name.split(".")[0]],
|
| 217 |
+
)
|
| 218 |
+
if name.startswith("wikipedia")
|
| 219 |
+
else XlelWdConfig(
|
| 220 |
+
name=name,
|
| 221 |
+
description=desc,
|
| 222 |
+
features=[
|
| 223 |
+
"mention",
|
| 224 |
+
"context_left",
|
| 225 |
+
"context_right",
|
| 226 |
+
"context_lang",
|
| 227 |
+
"context_title",
|
| 228 |
+
"context_date",
|
| 229 |
+
],
|
| 230 |
+
citation=_CITATION,
|
| 231 |
+
url=_URLS[name.split(".")[0]],
|
| 232 |
+
)
|
| 233 |
+
for name, desc in zip(_TASK_NAMES, _TASK_DESCRIPTIONS)
|
| 234 |
+
]
|
| 235 |
+
|
| 236 |
+
def _info(self):
|
| 237 |
+
|
| 238 |
+
features = {
|
| 239 |
+
feature: datasets.Value("string") for feature in self.config.features
|
| 240 |
+
}
|
| 241 |
+
features["label_id"] = datasets.Value("string")
|
| 242 |
+
return datasets.DatasetInfo(
|
| 243 |
+
description=_DESCRIPTION + self.config.description,
|
| 244 |
+
features=datasets.Features(features),
|
| 245 |
+
homepage=_HOMEPAGE,
|
| 246 |
+
license=_LICENSE,
|
| 247 |
+
citation=self.config.citation,
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
def _split_generators(self, dl_manager):
|
| 251 |
+
|
| 252 |
+
urls = _URLS[self.config.name.split(".")[0]]
|
| 253 |
+
downloaded_files = dl_manager.download_and_extract(urls)
|
| 254 |
+
if self.config.name.startswith("wikipedia"):
|
| 255 |
+
return [
|
| 256 |
+
datasets.SplitGenerator(
|
| 257 |
+
name=datasets.Split.TRAIN,
|
| 258 |
+
gen_kwargs={
|
| 259 |
+
"filepath": downloaded_files["train"],
|
| 260 |
+
"split": "train",
|
| 261 |
+
},
|
| 262 |
+
),
|
| 263 |
+
datasets.SplitGenerator(
|
| 264 |
+
name=datasets.Split.VALIDATION,
|
| 265 |
+
gen_kwargs={
|
| 266 |
+
"filepath": downloaded_files["dev"],
|
| 267 |
+
"split": "dev",
|
| 268 |
+
},
|
| 269 |
+
),
|
| 270 |
+
datasets.SplitGenerator(
|
| 271 |
+
name=datasets.Split.TEST,
|
| 272 |
+
gen_kwargs={
|
| 273 |
+
"filepath": downloaded_files["test"],
|
| 274 |
+
"split": "test",
|
| 275 |
+
},
|
| 276 |
+
),
|
| 277 |
+
]
|
| 278 |
+
elif self.config.name.startswith("wikinews"):
|
| 279 |
+
return [
|
| 280 |
+
datasets.SplitGenerator(
|
| 281 |
+
name=datasets.Split.TEST,
|
| 282 |
+
gen_kwargs={
|
| 283 |
+
"filepath": downloaded_files["test"],
|
| 284 |
+
"split": "test",
|
| 285 |
+
},
|
| 286 |
+
),
|
| 287 |
+
]
|
| 288 |
+
|
| 289 |
+
def _generate_examples(self, filepath, split):
|
| 290 |
+
|
| 291 |
+
task_domain, *task_langs = self.config.name.split(".")
|
| 292 |
+
with open(filepath, encoding="utf-8") as f:
|
| 293 |
+
for key, row in enumerate(f):
|
| 294 |
+
data = json.loads(row)
|
| 295 |
+
# generate mention references for the specified language
|
| 296 |
+
# if no language is specific in the config, return all
|
| 297 |
+
if len(task_langs) == 0 or task_langs[0] == data["context_lang"]:
|
| 298 |
+
if task_domain.startswith("wikipedia"):
|
| 299 |
+
yield key, {
|
| 300 |
+
"mention": data["mention"],
|
| 301 |
+
"context_left": data["context_left"],
|
| 302 |
+
"context_right": data["context_right"],
|
| 303 |
+
"context_lang": data["context_lang"],
|
| 304 |
+
"label_id": "" if split == "test" else data["label_id"],
|
| 305 |
+
}
|
| 306 |
+
elif task_domain.startswith("wikinews"):
|
| 307 |
+
yield key, {
|
| 308 |
+
"mention": data["mention"],
|
| 309 |
+
"context_left": data["context_left"],
|
| 310 |
+
"context_right": data["context_right"],
|
| 311 |
+
"context_lang": data["context_lang"],
|
| 312 |
+
"context_title": data["context_title"],
|
| 313 |
+
"context_date": data["context_date"],
|
| 314 |
+
"label_id": "" if split == "test" else data["label_id"],
|
| 315 |
+
}
|