# 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 """COMPS: Conceptual Minimal Pair Sentences for analyzing property knowledge.""" import json import datasets _CITATION = """ @article{misra2022comps, title={COMPS: Conceptual Minimal Pair Sentences for testing Property Knowledge and Inheritance in Pre-trained Language Models}, author={Misra, Kanishka and Rayz, Julia Taylor and Ettinger, Allyson}, journal={arXiv preprint arXiv:2210.01963}, year={2022} } """ _DESCRIPTION = """ COMPS is a dataset of minimal pair sentences in English that enables the testing knowledge of concepts and their properties in language models (LMs). Specifically, it tests the ability of LMs to attribute properties to everyday concepts, and demonstrate reasoning compatible with property inheritance, where subordinate concepts inherit the properties of their superordinate (hypernyms). """ _PROJECT_URL = "https://github.com/kanishkamisra/comps" _DOWNLOAD_URL = "https://raw.githubusercontent.com/kanishkamisra/comps/main/" \ "data/comps/" class CompsConfig(datasets.BuilderConfig): """BuilderConfig for COMPS.""" def __init__(self, name, version=datasets.Version("0.1.0"), **kwargs): """BuilderConfig for COMPS. Args: name (str): subset id **kwargs: keyword arguments forwarded to super. """ description = _DESCRIPTION if name == "base": description += " This subset tests for base property knowledge." elif name == "wugs": description += " This subset tests for property inheritance" \ " without distraction." else: description += " This subset tests for property inheritance with" \ f" distraction ({name.replace('wugs-dist-', '')})." super().__init__(name=name, description=description, version=version, **kwargs) class Comps(datasets.GeneratorBasedBuilder): """Minimal pairs to analyze property knowledge.""" subsets = ("base", "wugs", "wugs_dist", "wugs_dist-before", "wugs_dist-in-between") BUILDER_CONFIGS = [CompsConfig(subset) for subset in subsets] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("int32"), "property": datasets.Value("string"), "acceptable_concept": datasets.Value("string"), "unacceptable_concept": datasets.Value("string"), "prefix_acceptable": datasets.Value("string"), "prefix_unacceptable": datasets.Value("string"), "property_phrase": datasets.Value("string"), "negative_sample_type": datasets.Value("string"), "similarity": datasets.Value("float32"), "distraction_type": datasets.Value("string"), } ), homepage=_PROJECT_URL, citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" download_urls = _DOWNLOAD_URL + f"comps_{self.config.name}.jsonl" downloaded_file = dl_manager.download_and_extract(download_urls) return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_file})] def _generate_examples(self, filepath): """Yields examples.""" with open(filepath, "r", encoding="utf-8") as f: for line in f: line_dict = json.loads(line) if "wugs" in self.config.name: id_ = str(line_dict["base_id"]) + "_" + line_dict["distraction_type"] feats = { "id": line_dict["id"], "property": line_dict["property"], "acceptable_concept": line_dict["acceptable_concept"], "unacceptable_concept": line_dict["unacceptable_concept"], "prefix_acceptable": line_dict["prefix_acceptable"], "prefix_unacceptable": line_dict["prefix_unacceptable"], "property_phrase": line_dict["property_phrase"], "negative_sample_type": line_dict["negative_sample_type"], "similarity": line_dict["similarity"], "distraction_type": line_dict["distraction_type"], } else: id_ = str(line_dict['id']) feats = { **line_dict, "distraction_type": "None" } yield id_, feats