|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""RedFM: a Filtered and Multilingual Relation Extraction Dataset.""" |
|
|
|
|
|
import collections |
|
import json |
|
import os |
|
from contextlib import ExitStack |
|
import logging |
|
import datasets |
|
|
|
|
|
_CITATION = """\ |
|
@InProceedings{redfm2023, |
|
author = {Huguet Cabot, Pere-Lluis |
|
and Tedeschi, Simone |
|
and Ngonga Ngomo, Axel-Cyrille |
|
and Navigli, Roberto}, |
|
title = {RED\textsuperscript{FM}: a Filtered and Multilingual Relation Extraction Dataset}, |
|
booktitle = {Proceedings of the 2023 Conference on Association for Computational Linguistics}, |
|
year = {2023}, |
|
publisher = {Association for Computational Linguistics}, |
|
location = {Toronto, Canada}, |
|
}""" |
|
|
|
_DESCRIPTION = """\ |
|
Relation Extraction (RE) is a task that identifies relationships between entities in a text, enabling the acquisition of relational facts and bridging the gap between natural language and structured knowledge. However, current RE models often rely on small datasets with low coverage of relation types, particularly when working with languages other than English. \\ |
|
In this paper, we address the above issue and provide two new resources that enable the training and evaluation of multilingual RE systems. |
|
First, we present SRED\textsuperscript{FM}, an automatically annotated dataset covering 18 languages, 400 relation types, 13 entity types, totaling more than 40 million triplet instances. Second, we propose RED\textsuperscript{FM}, a smaller, human-revised dataset for seven languages that allows for the evaluation of multilingual RE systems. |
|
To demonstrate the utility of these novel datasets, we experiment with the first end-to-end multilingual RE model, mREBEL, |
|
that extracts triplets, including entity types, in multiple languages. We release our resources and model checkpoints at \href{https://www.github.com/babelscape/rebel}{https://www.github.com/babelscape/rebel}. |
|
""" |
|
|
|
_LANGUAGES = ("ar", "de", "en", "es", "fr", "it", "zh") |
|
|
|
DEFAULT_CONFIG_NAME = "all_languages" |
|
|
|
_URL_train = f"data/train." |
|
_URL_dev = f"data/dev." |
|
_URL_test = f"data/test." |
|
|
|
class RedFMConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for RedFM.""" |
|
|
|
def __init__(self, language: str, languages=None, **kwargs): |
|
"""BuilderConfig for RedFM. |
|
Args: |
|
language: One of ar,de,en,es,fr,it,zh, or all_languages |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(RedFMConfig, self).__init__(**kwargs) |
|
self.language = language |
|
if language != "all_languages": |
|
self.languages = [language] |
|
else: |
|
self.languages = languages if languages is not None else _LANGUAGES |
|
|
|
|
|
class RedFM(datasets.GeneratorBasedBuilder): |
|
"""RedFM: a Filtered and Multilingual Relation Extraction Dataset. Version 1.0.0""" |
|
|
|
VERSION = datasets.Version("1.0.0", "") |
|
BUILDER_CONFIG_CLASS = RedFMConfig |
|
BUILDER_CONFIGS = [ |
|
RedFMConfig( |
|
name=lang, |
|
language=lang, |
|
version=datasets.Version("1.0.0", ""), |
|
description=f"Plain text import of RedFM for the {lang} language", |
|
) |
|
for lang in _LANGUAGES |
|
] + [ |
|
RedFMConfig( |
|
name="all_languages", |
|
language="all_languages", |
|
version=datasets.Version("1.0.0", ""), |
|
description="Plain text import of RedFM for all languages", |
|
) |
|
] |
|
|
|
def _info(self): |
|
if self.config.language == "all_languages": |
|
features = datasets.Features( |
|
{ |
|
"docid": datasets.Value("string"), |
|
"title": datasets.Value("string"), |
|
"uri": datasets.Value("string"), |
|
"lan": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"entities": [{'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')}], |
|
"relations": [{'subject': datasets.Features({'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')}), |
|
'predicate': datasets.ClassLabel(num_classes=32, names=['country', 'place of birth', 'spouse', 'country of citizenship', 'instance of', 'capital', 'child', 'shares border with', 'author', 'director', 'occupation', 'founded by', 'league', 'owned by', 'genre', 'named after', 'follows', 'headquarters location', 'cast member', 'manufacturer', 'located in or next to body of water', 'location', 'part of', 'mouth of the watercourse', 'member of', 'sport', 'characters', 'participant', 'notable work', 'replaces', 'sibling', 'inception']), |
|
'object': datasets.Features({'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')})}], |
|
} |
|
) |
|
else: |
|
features = datasets.Features( |
|
{ |
|
"docid": datasets.Value("string"), |
|
"title": datasets.Value("string"), |
|
"uri": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"entities": [{'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')}], |
|
"relations": [{'subject': datasets.Features({'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')}), |
|
'predicate': datasets.ClassLabel(num_classes=32, names=['country', 'place of birth', 'spouse', 'country of citizenship', 'instance of', 'capital', 'child', 'shares border with', 'author', 'director', 'occupation', 'founded by', 'league', 'owned by', 'genre', 'named after', 'follows', 'headquarters location', 'cast member', 'manufacturer', 'located in or next to body of water', 'location', 'part of', 'mouth of the watercourse', 'member of', 'sport', 'characters', 'participant', 'notable work', 'replaces', 'sibling', 'inception']), |
|
'object': datasets.Features({'uri': datasets.Value(dtype='string'), 'surfaceform': datasets.Value(dtype='string'), 'type': datasets.Value(dtype='string'), 'start': datasets.Value(dtype='int32'), 'end': datasets.Value(dtype='int32')})}], |
|
} |
|
) |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
|
|
|
|
supervised_keys=None, |
|
homepage="https://www.github.com/babelscape/rebel", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
data_dir = dl_manager.download( |
|
{ |
|
"train": [f"{_URL_train}{lang}.jsonl" for lang in self.config.languages if lang not in ["zh", "ar"]], |
|
"dev": [f"{_URL_dev}{lang}.jsonl" for lang in self.config.languages], |
|
"test": [f"{_URL_test}{lang}.jsonl" for lang in self.config.languages], |
|
} |
|
) |
|
if len(data_dir["train"]) == 0: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"filepaths": data_dir["test"], |
|
"data_format": "RedFM", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"filepaths": data_dir["dev"], |
|
"data_format": "RedFM", |
|
}, |
|
), |
|
] |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepaths": data_dir["train"], |
|
"data_format": "RedFM", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"filepaths": data_dir["test"], |
|
"data_format": "RedFM", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"filepaths": data_dir["dev"], |
|
"data_format": "RedFM", |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, data_format, filepaths): |
|
"""This function returns the examples in the raw (text) form.""" |
|
logging.info("generating examples from = %s", filepaths) |
|
if self.config.language == "all_languages": |
|
for filepath in filepaths: |
|
with open(filepath, encoding="utf-8") as f: |
|
for idx, row in enumerate(f): |
|
data = json.loads(row) |
|
entities = [] |
|
for entity in data["entities"]: |
|
entities.append({ |
|
"uri": entity["uri"], |
|
"surfaceform": entity["surfaceform"], |
|
"start": entity["boundaries"][0], |
|
"end": entity["boundaries"][1], |
|
"type": entity["type"], |
|
}) |
|
relations = [] |
|
for relation in data["relations"]: |
|
relations.append({ |
|
"subject": { |
|
"uri": relation["subject"]["uri"], |
|
"surfaceform": relation["subject"]["surfaceform"], |
|
"start": relation["subject"]["boundaries"][0], |
|
"end": relation["subject"]["boundaries"][1], |
|
"type": relation["subject"]["type"], |
|
}, |
|
"predicate": relation["predicate"]["surfaceform"], |
|
"object": { |
|
"uri": relation["object"]["uri"], |
|
"surfaceform": relation["object"]["surfaceform"], |
|
"start": relation["object"]["boundaries"][0], |
|
"end": relation["object"]["boundaries"][1], |
|
"type": relation["object"]["type"], |
|
}, |
|
}) |
|
yield data["docid"]+ '-' + data["lan"], { |
|
"docid": data["docid"], |
|
"title": data["title"], |
|
"uri": data["uri"], |
|
"lan": data["lan"], |
|
"text": data["text"], |
|
"entities": entities, |
|
"relations": relations, |
|
} |
|
else: |
|
for filepath in filepaths: |
|
with open(filepath, encoding="utf-8") as f: |
|
for idx, row in enumerate(f): |
|
data = json.loads(row) |
|
entities = [] |
|
for entity in data["entities"]: |
|
entities.append({ |
|
"uri": entity["uri"], |
|
"surfaceform": entity["surfaceform"], |
|
"start": entity["boundaries"][0], |
|
"end": entity["boundaries"][1], |
|
"type": entity["type"], |
|
}) |
|
relations = [] |
|
for relation in data["relations"]: |
|
relations.append({ |
|
"subject": { |
|
"uri": relation["subject"]["uri"], |
|
"surfaceform": relation["subject"]["surfaceform"], |
|
"start": relation["subject"]["boundaries"][0], |
|
"end": relation["subject"]["boundaries"][1], |
|
"type": relation["subject"]["type"], |
|
}, |
|
"predicate": relation["predicate"]["surfaceform"], |
|
"object": { |
|
"uri": relation["object"]["uri"], |
|
"surfaceform": relation["object"]["surfaceform"], |
|
"start": relation["object"]["boundaries"][0], |
|
"end": relation["object"]["boundaries"][1], |
|
"type": relation["object"]["type"], |
|
}, |
|
}) |
|
yield data["docid"], { |
|
"docid": data["docid"], |
|
"title": data["title"], |
|
"uri": data["uri"], |
|
"text": data["text"], |
|
"entities": entities, |
|
"relations": relations, |
|
} |