File size: 5,706 Bytes
20ed7b1 24ca5f7 20ed7b1 516fc89 635cff7 20ed7b1 d607d2b c18e48a d607d2b 20ed7b1 24ca5f7 3740d3f f2da732 c18e48a 20ed7b1 d607d2b 20ed7b1 24ca5f7 a77bc32 3740d3f f2da732 c18e48a 20ed7b1 24ca5f7 20ed7b1 322d5f7 20ed7b1 2f7824a 20ed7b1 |
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 |
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
|