magpie / magpie.py
gsarti's picture
Update magpie.py
e958fd0
import datasets
from typing import List
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
@inproceedings{haagsma-etal-2020-magpie,
title = "{MAGPIE}: A Large Corpus of Potentially Idiomatic Expressions",
author = "Haagsma, Hessel and
Bos, Johan and
Nissim, Malvina",
booktitle = "Proceedings of the 12th Language Resources and Evaluation Conference",
month = may,
year = "2020",
address = "Marseille, France",
publisher = "European Language Resources Association",
url = "https://aclanthology.org/2020.lrec-1.35",
pages = "279--287",
language = "English",
ISBN = "979-10-95546-34-4",
}
@inproceedings{dankers-etal-2022-transformer,
title = "Can Transformer be Too Compositional? Analysing Idiom Processing in Neural Machine Translation",
author = "Dankers, Verna and
Lucas, Christopher and
Titov, Ivan",
booktitle = "Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
month = may,
year = "2022",
address = "Dublin, Ireland",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2022.acl-long.252",
doi = "10.18653/v1/2022.acl-long.252",
pages = "3608--3626",
}
"""
_DESCRIPTION = """\
The MAGPIE corpus is a large sense-annotated corpus of potentially idiomatic expressions (PIEs), based on the British National Corpus (BNC). Potentially idiomatic expressions are like idiomatic expressions, but the term also covers literal uses of idiomatic expressions, such as 'I leave work at the end of the day.' for the idiom 'at the end of the day'. This version of the dataset reflects the filtered subset used by Dankers et al. (2022) in their investigation on how PIEs are represented by NMT models. Authors use 37k samples annotated as fully figurative or literal, for 1482 idioms that contain nouns, numerals or adjectives that are colours (which they refer to as keywords). Because idioms show syntactic and morphological variability, the focus is mostly put on nouns. PIEs and their context are separated using the original corpus’s word-level annotations.
"""
_HOMEPAGE = "https://github.com/vernadankers/mt_idioms"
_LICENSE = "CC-BY-4.0"
class MagpieConfig(datasets.BuilderConfig):
"""BuilderConfig for MAGPIE."""
def __init__(self, features, data_url, **kwargs):
"""BuilderConfig for MAGPIE.
Args:
features: : `list[string]`, list of the features that will appear in the
feature dict. Should not include "label".
**kwargs: keyword arguments forwarded to super.
"""
super().__init__(**kwargs)
self.features = features
self.data_url = data_url
class Magpie(datasets.GeneratorBasedBuilder):
"""MAGPIE: A Large Corpus of Potentially Idiomatic Expressions"""
BUILDER_CONFIGS = [
MagpieConfig(
name="magpie",
version=datasets.Version("1.0.0"),
features=["sentence", "annotation", "idiom", "usage", "variant", "pos_tags"],
data_url="https://huggingface.co/datasets/gsarti/magpie/resolve/main/magpie.tsv"
)
]
DEFAULT_CONFIG_NAME = "magpie"
def _info(self):
features = {feature: datasets.Value("string") for feature in self.config.features}
features["annotation"] = datasets.features.Sequence(datasets.Value("uint8"))
features["pos_tags"] = datasets.features.Sequence(datasets.Value("string"))
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(features),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE
)
def _split_generators(self, dl_manager):
data_file = dl_manager.download_and_extract(self.config.data_url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": data_file,
"features": self.config.features,
},
),
]
def _generate_examples(self, filepath: str, features: List[str]):
"""Yields examples as (key, example) tuples."""
with open(filepath, encoding="utf8") as f:
for id_, row in enumerate(f):
if id_ == 0:
continue
fields = row.strip().split("\t")
fields[1] = fields[1].strip().split()
fields[-1] = fields[-1].strip().split()
yield id_, {
k:v for k,v in zip(features, fields)
}