|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""XSum dataset.""" |
|
|
|
|
|
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 = """ |
|
Extreme Summarization (XSum) Dataset. |
|
There are three features: |
|
- document: Input news article. |
|
- summary: One sentence summary of the article. |
|
- id: BBC ID of the article. |
|
""" |
|
|
|
|
|
_URL_DATA = "data/data1.tar.gz" |
|
_URL_SPLITS = ( |
|
"https://raw.githubusercontent.com/EdinburghNLP/XSum/master/XSum-Dataset/XSum-TRAINING-DEV-TEST-SPLIT-90-5-5.json" |
|
) |
|
|
|
_DOCUMENT = "document" |
|
_SUMMARY = "summary" |
|
_ID = "id" |
|
|
|
_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): |
|
"""Extreme Summarization (XSum) Dataset.""" |
|
|
|
|
|
VERSION = datasets.Version("1.2.0") |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
_DOCUMENT: datasets.Value("string"), |
|
_SUMMARY: datasets.Value("string"), |
|
_ID: datasets.Value("string"), |
|
} |
|
), |
|
supervised_keys=(_DOCUMENT, _SUMMARY), |
|
homepage="https://github.com/EdinburghNLP/XSum/tree/master/XSum-Dataset", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
files_to_download = {"data": _URL_DATA, "splits": _URL_SPLITS} |
|
downloaded_files = dl_manager.download(files_to_download) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"split_path": downloaded_files["splits"], |
|
"split_name": "train", |
|
"data_dir": "data", |
|
"files": dl_manager.iter_archive(downloaded_files["data"]), |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"split_path": downloaded_files["splits"], |
|
"split_name": "validation", |
|
"data_dir": "data", |
|
"files": dl_manager.iter_archive(downloaded_files["data"]), |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"split_path": downloaded_files["splits"], |
|
"split_name": "test", |
|
"data_dir": "data", |
|
"files": dl_manager.iter_archive(downloaded_files["data"]), |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, split_path, split_name, data_dir, files): |
|
"""Yields examples.""" |
|
|
|
with open(split_path, "r", encoding="utf-8") as f: |
|
split_ids = json.load(f) |
|
split_ids = {k: set(v) for k, v in split_ids.items()} |
|
|
|
for path, f in files: |
|
if not split_ids[split_name]: |
|
break |
|
elif path.startswith(data_dir) and path.endswith(".summarykz"): |
|
i = os.path.basename(path).split(".")[0] |
|
if i in split_ids[split_name]: |
|
split_ids[split_name].remove(i) |
|
text = "".join( |
|
[ |
|
line.decode("utf-8") |
|
for line in f.readlines() |
|
if line.decode("utf-8") not in _REMOVE_LINES and line.strip() |
|
] |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
segs = text.split("[SN]") |
|
print(segs[6].strip()) |
|
yield i, {_DOCUMENT: segs[8].strip(), _SUMMARY: segs[6].strip(), _ID: i} |