# coding=utf-8 # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors. # # 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. # Lint as: python3 """The FTRACE benchmark.""" import json import os from multiprocessing.sharedctypes import Value import datasets _FTRACE_CITATION = """\ """ _FTRACE_DESCRIPTION = """\ Factual Tracing Dataset """ _FTRACE_ABSTRACTS_DESCRIPTION = """\ Abstracts based on TREx dataset. """ _FTRACE_ABSTRACTS_CITATION = """\ """ _FTRACE_QUERIES_DESCRIPTION = """\ Queries based on LAMA dataset. """ _FTRACE_QUERIES_CITATION = """\ """ class FTRACEConfig(datasets.BuilderConfig): """BuilderConfig for FTRACE.""" def __init__( self, features, data_url, citation, url, **kwargs, ): """BuilderConfig for FTRACE. Args: features: `list[string]`, list of the features that will appear in the feature dict. Should not include "label". data_url: `string`, url to download the zip file from. citation: `string`, citation for the data set. url: `string`, url for information about the data set. **kwargs: keyword arguments forwarded to super. """ # Version history: # 0.0.2: Initial version. super(FTRACEConfig, self).__init__( version=datasets.Version("0.0.2"), **kwargs ) self.features = features self.data_url = data_url self.citation = citation self.url = url class FTRACE(datasets.GeneratorBasedBuilder): """The SuperFTRACE benchmark.""" BUILDER_CONFIGS = [ FTRACEConfig( name="abstracts", description=_FTRACE_ABSTRACTS_DESCRIPTION, features=[ "inputs_pretokenized", "targets_pretokenized", "masked_uri", "masked_type", "facts", "id", "example_uris", "page_uri", ], data_url=( "https://people.csail.mit.edu/akyurek/ftrace/abstracts.zip" ), citation=_FTRACE_ABSTRACTS_CITATION, url="", ), FTRACEConfig( name="queries", description=_FTRACE_QUERIES_DESCRIPTION, features=[ "inputs_pretokenized", "targets_pretokenized", "uuid", "obj_uri", "sub_uri", "predicate_id", "sub_surface", "obj_surface", ], data_url="https://people.csail.mit.edu/akyurek/ftrace/queries.zip", citation=_FTRACE_QUERIES_CITATION, url="", ), ] def _info(self): features = { feature: datasets.Value("string") for feature in self.config.features } return datasets.DatasetInfo( description=_FTRACE_DESCRIPTION + self.config.description, features=datasets.Features(features), homepage=self.config.url, citation=self.config.citation + "\n" + _FTRACE_CITATION, ) def _split_generators(self, dl_manager): dl_dir = dl_manager.download_and_extract(self.config.data_url) or "" task_name = _get_task_name_from_data_url(self.config.data_url) dl_dir = os.path.join(dl_dir, task_name) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "data_file": os.path.join(dl_dir, "train.jsonl"), "split": "train", }, ), ] def _generate_examples(self, data_file, split): with open(data_file, encoding="utf-8") as f: for idx, line in enumerate(f): row = json.loads(line) yield idx, row def _get_task_name_from_data_url(data_url): if "queries" in data_url: return "queries" elif "abstracts" in data_url: return "abstracts" return "queries"