|
import json |
|
from typing import List |
|
|
|
import datasets |
|
|
|
_VERSION = "1.0.0" |
|
|
|
_CITATION = """\ |
|
@inproceedings{decao2021autoregressive, |
|
author = {Nicola {De Cao} and |
|
Gautier Izacard and |
|
Sebastian Riedel and |
|
Fabio Petroni}, |
|
title = {Autoregressive Entity Retrieval}, |
|
booktitle = {9th International Conference on Learning Representations, {ICLR} 2021, |
|
Virtual Event, Austria, May 3-7, 2021}, |
|
publisher = {OpenReview.net}, |
|
year = {2021}, |
|
url = {https://openreview.net/forum?id=5k8F6UU39V}, |
|
}""" |
|
|
|
|
|
class EntityDisambiguationConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for EntityDisambiguation.""" |
|
|
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for EntityDisambiguation. |
|
|
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(EntityDisambiguationConfig, self).__init__(**kwargs) |
|
|
|
self.features = datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"input": datasets.Value("string"), |
|
"meta": { |
|
"left_context": datasets.Value("string"), |
|
"mention": datasets.Value("string"), |
|
"right_context": datasets.Value("string"), |
|
}, |
|
"candidates": datasets.features.Sequence(datasets.Value("string")), |
|
"answer": datasets.Value("string") |
|
} |
|
) |
|
|
|
|
|
class EntityDisambiguation(datasets.GeneratorBasedBuilder): |
|
"""Entity Disambiguation dataset.""" |
|
|
|
VERSION = datasets.Version(_VERSION) |
|
|
|
BUILDER_CONFIGS = [ |
|
EntityDisambiguationConfig(name="ace2004", version=VERSION, description="ACE2004 dataset"), |
|
EntityDisambiguationConfig(name="aida", version=VERSION, description="AIDA dataset"), |
|
EntityDisambiguationConfig(name="aquaint", version=VERSION, description="AQUAINT dataset"), |
|
EntityDisambiguationConfig(name="blink", version=VERSION, description="BLINK dataset"), |
|
EntityDisambiguationConfig(name="clueweb", version=VERSION, description="CWEB dataset"), |
|
EntityDisambiguationConfig(name="msnbc", version=VERSION, description="MSNBC dataset"), |
|
EntityDisambiguationConfig(name="wiki", version=VERSION, description="WIKI dataset"), |
|
] |
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: |
|
"""Returns SplitGenerators.""" |
|
|
|
if self.config.name == "blink": |
|
available_splits = ["train", "dev"] |
|
elif self.config.name == "aida": |
|
available_splits = ["train", "dev", "test"] |
|
else: |
|
available_splits = ["test"] |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=split, |
|
gen_kwargs={ |
|
"filepath": dl_manager.download_and_extract( |
|
f"http://dl.fbaipublicfiles.com/{'KILT' if self.config.name.lower() == 'blink' else 'GENRE'}" |
|
f"/{self.config.name.lower()}-{split}-kilt.jsonl"), |
|
"split": split, |
|
}, |
|
) |
|
for split in available_splits |
|
] |
|
|
|
def _info(self) -> datasets.DatasetInfo: |
|
return datasets.DatasetInfo(description="Entity Disambiguation dataset", features=self.config.features, |
|
citation=_CITATION) |
|
|
|
def _generate_examples(self, filepath: str, split: str): |
|
with open(filepath, encoding="utf-8") as f: |
|
for line in f: |
|
row = json.loads(line) |
|
row["answer"] = row["output"][0]["answer"] |
|
|
|
del row["output"] |
|
|
|
yield row["id"], row |
|
|