Datasets:

Languages:
English
Multilinguality:
monolingual
Size Categories:
n<1K
Tags:
License:
analogy_questions / analogy_questions.py
asahi417's picture
Update analogy_questions.py
d607d2b
import json
from itertools import chain
import datasets
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """[Analogy Question](https://aclanthology.org/2021.acl-long.280/)"""
_NAME = "analogy_questions"
_VERSION = "2.0.8"
_CITATION = """
@inproceedings{ushio-etal-2021-bert,
title = "{BERT} is to {NLP} what {A}lex{N}et is to {CV}: Can Pre-Trained Language Models Identify Analogies?",
author = "Ushio, Asahi and
Espinosa Anke, Luis and
Schockaert, Steven and
Camacho-Collados, Jose",
booktitle = "Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers)",
month = aug,
year = "2021",
address = "Online",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2021.acl-long.280",
doi = "10.18653/v1/2021.acl-long.280",
pages = "3609--3624",
abstract = "Analogies play a central role in human commonsense reasoning. The ability to recognize analogies such as {``}eye is to seeing what ear is to hearing{''}, sometimes referred to as analogical proportions, shape how we structure knowledge and understand language. Surprisingly, however, the task of identifying such analogies has not yet received much attention in the language model era. In this paper, we analyze the capabilities of transformer-based language models on this unsupervised task, using benchmarks obtained from educational settings, as well as more commonly used datasets. We find that off-the-shelf language models can identify analogies to a certain extent, but struggle with abstract and complex relations, and results are highly sensitive to model architecture and hyperparameters. Overall the best results were obtained with GPT-2 and RoBERTa, while configurations using BERT were not able to outperform word embedding models. Our results raise important questions for future work about how, and to what extent, pre-trained language models capture knowledge about abstract semantic relations.",
}
"""
_HOME_PAGE = "https://github.com/asahi417/relbert"
_URL = f'https://huggingface.co/datasets/relbert/{_NAME}/raw/main/dataset'
_URLS = {
str(datasets.Split.TEST): {
'bats': [f'{_URL}/bats/test.jsonl'],
'google': [f'{_URL}/google/test.jsonl'],
# 'sat': [f'{_URL}/sat/test.jsonl'],
# 'sat_metaphor': [f'{_URL}/sat_metaphor/test.jsonl'],
# 'sat_full': [f'{_URL}/sat/test.jsonl', f'{_URL}/sat/valid.jsonl'],
'u2': [f'{_URL}/u2/test.jsonl'],
'u4': [f'{_URL}/u4/test.jsonl'],
"t_rex_relational_similarity": [f'{_URL}/t_rex_relational_similarity/test.jsonl'],
"conceptnet_relational_similarity": [f'{_URL}/conceptnet_relational_similarity/test.jsonl'],
"nell_relational_similarity": [f'{_URL}/nell_relational_similarity/test.jsonl'],
'scan': [f'{_URL}/scan/test.jsonl'],
},
str(datasets.Split.VALIDATION): {
'bats': [f'{_URL}/bats/valid.jsonl'],
'google': [f'{_URL}/google/valid.jsonl'],
# 'sat': [f'{_URL}/sat/valid.jsonl'],
'u2': [f'{_URL}/u2/valid.jsonl'],
'u4': [f'{_URL}/u4/valid.jsonl'],
"semeval2012_relational_similarity": [f'{_URL}/semeval2012_relational_similarity/valid.jsonl'],
"t_rex_relational_similarity": [f'{_URL}/t_rex_relational_similarity/valid.jsonl'],
"conceptnet_relational_similarity": [f'{_URL}/conceptnet_relational_similarity/valid.jsonl'],
"nell_relational_similarity": [f'{_URL}/nell_relational_similarity/valid.jsonl'],
'scan': [f'{_URL}/scan/valid.jsonl'],
}
}
_DATASET = sorted(list(set(list(chain(*[list(i.keys()) for i in _URLS.values()])))))
class AnalogyQuestionConfig(datasets.BuilderConfig):
"""BuilderConfig"""
def __init__(self, **kwargs):
"""BuilderConfig.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(AnalogyQuestionConfig, self).__init__(**kwargs)
class AnalogyQuestion(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [AnalogyQuestionConfig(name=i, version=datasets.Version(_VERSION), description=f"Dataset {i}") for i in sorted(_DATASET)]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"stem": datasets.Sequence(datasets.Value("string")),
"answer": datasets.Value("int32"),
"choice": datasets.Sequence(datasets.Sequence(datasets.Value("string"))),
"prefix": datasets.Value("string")
}
),
supervised_keys=None,
homepage=_HOME_PAGE
)
def _split_generators(self, dl_manager):
target_urls = {k: v[self.config.name] for k, v in _URLS.items() if self.config.name in v}
downloaded_file = dl_manager.download_and_extract(target_urls)
return [datasets.SplitGenerator(
name=k, gen_kwargs={"filepaths": downloaded_file[k]}
) for k, v in _URLS.items() if self.config.name in v]
def _generate_examples(self, filepaths):
_key = 0
for filepath in filepaths:
logger.info("generating examples from = %s", filepath)
with open(filepath, encoding="utf-8") as f:
_list = [i for i in f.read().split('\n') if len(i) > 0]
for i in _list:
data = json.loads(i)
yield _key, data
_key += 1