File size: 6,496 Bytes
2f687f5 b5380dd 2f687f5 |
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 |
# coding=utf-8
# 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.
"""The mLAMA Dataset"""
import json
import os
import datasets
_CITATION = """
@article{kassner2021multilingual,
author = {Nora Kassner and
Philipp Dufter and
Hinrich Sch{\"{u}}tze},
title = {Multilingual {LAMA:} Investigating Knowledge in Multilingual Pretrained
Language Models},
journal = {CoRR},
volume = {abs/2102.00894},
year = {2021},
url = {https://arxiv.org/abs/2102.00894},
archivePrefix = {arXiv},
eprint = {2102.00894},
timestamp = {Tue, 09 Feb 2021 13:35:56 +0100},
biburl = {https://dblp.org/rec/journals/corr/abs-2102-00894.bib},
bibsource = {dblp computer science bibliography, https://dblp.org},
note = {to appear in EACL2021}
}
"""
_DESCRIPTION = """mLAMA: a multilingual version of the LAMA benchmark (T-REx and GoogleRE) covering 53 languages."""
_HOMEPAGE = "http://cistern.cis.lmu.de/mlama/"
_LICENSE = "The Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0). https://creativecommons.org/licenses/by-nc-sa/4.0/"
_URL = "http://cistern.cis.lmu.de/mlama/mlama1.1.zip"
_LANGUAGES = (
"af",
"ar",
"az",
"be",
"bg",
"bn",
"ca",
"ceb",
"cs",
"cy",
"da",
"de",
"el",
"en",
"es",
"et",
"eu",
"fa",
"fi",
"fr",
"ga",
"gl",
"he",
"hi",
"hr",
"hu",
"hy",
"id",
"it",
"ja",
"ka",
"ko",
"la",
"lt",
"lv",
"ms",
"nl",
"pl",
"pt",
"ro",
"ru",
"sk",
"sl",
"sq",
"sr",
"sv",
"ta",
"th",
"tr",
"uk",
"ur",
"vi",
"zh",
)
_RELATIONS = (
"place_of_birth",
"place_of_death",
"P1001",
"P101",
"P103",
"P106",
"P108",
"P127",
"P1303",
"P131",
"P136",
"P1376",
"P138",
"P140",
"P1412",
"P159",
"P17",
"P176",
"P178",
"P19",
"P190",
"P20",
"P264",
"P27",
"P276",
"P279",
"P30",
"P31",
"P36",
"P361",
"P364",
"P37",
"P39",
"P407",
"P413",
"P449",
"P463",
"P47",
"P495",
"P527",
"P530",
"P740",
"P937",
)
class MLamaConfig(datasets.BuilderConfig):
"""BuilderConfig for mLAMA."""
def __init__(self, languages=None, relations=None, **kwargs):
"""BuilderConfig for mLAMA.
Args:
languages: A subset of af,ar,az,be,bg,bn,ca,ceb,cs,cy,da,de,el,en,es,et,eu,fa,fi,fr,ga,gl,he,hi,hr,hu,hy,id,it,ja,ka,ko,la,lt,lv,ms,nl,pl,pt,ro,ru,sk,sl,sq,sr,sv,ta,th,tr,uk,ur,vi,zh
relations: A subset of place_of_birth,place_of_death,P1001,P101,P103,P106,P108,P127,P1303,P131,P136,P1376,P138,P140,P1412,P159,P17,P176,P178,P19,P190,P20,P264,P27,P276,P279,P30,P31,P36,P361,P364,P37,P39,P407,P413,P449,P463,P47,P495,P527,P530,P740,P937
**kwargs: keyword arguments forwarded to super.
"""
super(MLamaConfig, self).__init__(**kwargs)
self.languages = languages if languages is not None else _LANGUAGES
self.relations = relations if relations is not None else _RELATIONS
class MLama(datasets.GeneratorBasedBuilder):
"""multilingual LAMA Dataset (mLAMA)"""
VERSION = datasets.Version("1.1.0")
BUILDER_CONFIG_CLASS = MLamaConfig
BUILDER_CONFIGS = [
MLamaConfig(
name="all",
languages=None,
relations=None,
version=datasets.Version("1.1.0"),
description="Import of mLAMA for all languages and all relations.",
)
]
def _info(self):
features = datasets.Features(
{
"uuid": datasets.Value("string"),
"lineid": datasets.Value("uint32"),
"obj_uri": datasets.Value("string"),
"obj_label": datasets.Value("string"),
"sub_uri": datasets.Value("string"),
"sub_label": datasets.Value("string"),
"template": datasets.Value("string"),
"language": datasets.Value("string"),
"predicate_id": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
data_dir = dl_manager.download_and_extract(_URL)
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": os.path.join(data_dir, "mlama1.1"),
"split": "test",
},
),
]
def _generate_examples(self, filepath, split):
"""Yields examples from the mLAMA dataset."""
id_ = -1
for language in self.config.languages:
# load templates
templates = {}
with open(os.path.join(filepath, language, "templates.jsonl"), encoding="utf-8") as fp:
for line in fp:
line = json.loads(line)
templates[line["relation"]] = line["template"]
for relation in self.config.relations:
# load triples
with open(os.path.join(filepath, language, f"{relation}.jsonl"), encoding="utf-8") as fp:
for line in fp:
triple = json.loads(line)
triple["language"] = language
triple["predicate_id"] = relation
triple["template"] = templates.get(relation, "")
id_ += 1
yield id_, triple
|