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