import json import os import datasets _CITATION = """\ @article{Narayan2018DontGM, title={Don't Give Me the Details, Just the Summary! Topic-Aware Convolutional Neural Networks for Extreme Summarization}, author={Shashi Narayan and Shay B. Cohen and Mirella Lapata}, journal={ArXiv}, year={2018}, volume={abs/1808.08745} } """ _DESCRIPTION = """\ This is the XSUM subset of the GEM benchmark. """ _URLs = { "xsum": { "data": "http://bollin.inf.ed.ac.uk/public/direct/XSUM-EMNLP18-Summary-Data-Original.tar.gz", "splits": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_xsum_confidence_0.8.json", "challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/xsum.zip", }, } _XSUM_REMOVE_LINES = set( [ "Share this with\n", "Email\n", "Facebook\n", "Messenger\n", "Twitter\n", "Pinterest\n", "WhatsApp\n", "Linkedin\n", "LinkedIn\n", "Copy this link\n", "These are external links and will open in a new window\n", ] ) class Xsum(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ datasets.BuilderConfig( name=lang, version=datasets.Version("1.0.0"), description="", ) ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features = datasets.Features( { "gem_id": datasets.Value("string"), "gem_parent_id": datasets.Value("string"), "xsum_id": datasets.Value("string"), "document": datasets.Value("string"), "target": datasets.Value("string"), "references": [datasets.Value("string")], } ), supervised_keys=None, homepage="", citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" dl_dir = dl_manager.download_and_extract(_URLs[self.config.name]) challenge_sets = [ ("challenge_train_sample", "train_xsum_RandomSample500.json"), ("challenge_validation_sample", "validation_xsum_RandomSample500.json"), ("challenge_test_backtranslation", "test_xsum_BackTranslation500.json"), ("challenge_test_bfp_02", "test_xsum_ButterFingersPerturbation_p=0.02_500.json"), ("challenge_test_bfp_05", "test_xsum_ButterFingersPerturbation_p=0.05_500.json"), ("challenge_test_nopunc", "test_xsum_WithoutPunctuation500.json"), ("challenge_test_covid", f"en_test_covid19.jsonl"), ] return [ datasets.SplitGenerator( name=challenge_split, gen_kwargs={ "filepath": os.path.join(dl_dir["challenge_set"], "xsum", filename), "split": challenge_split, }, ) for challenge_split, filename in challenge_sets ] def _generate_examples(self, filepath, split, filepaths=None, lang=None): """Yields examples.""" if "challenge" in split: if "covid" in split: with open(filepath, encoding="utf-8") as f: id_ = -1 for line in f: data = json.loads(line) id_ += 1 yield id_, { "gem_id": f"{self.config.name}-{split}-{id_}", "gem_parent_id": f"{self.config.name}-{split}-{id_}", "xsum_id": data["url"], "document": data["text"], "target": data["summary"], "references": [] if split == "train" else [data["summary"]], } else: exples = json.load(open(filepath, encoding="utf-8")) if isinstance(exples, dict): assert len(exples) == 1, "multiple entries found" exples = list(exples.values())[0] for id_, exple in enumerate(exples): exple["gem_parent_id"] = exple["gem_id"] exple["gem_id"] = f"{self.config.name}-{split}-{id_}" yield id_, exple