add data script and README
Browse files- README.md +7 -3
- bart_cnndm.py +275 -0
README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Modification of the cnn_dailymail dataset in Hugging Face. The main goal is to reproduce the results on BART.
|
| 2 |
+
|
| 3 |
+
References: https://github.com/facebookresearch/fairseq/issues/1401
|
| 4 |
+
|
| 5 |
+
Major changes:
|
| 6 |
+
1. remove the space in " ." in fix_missing_period.
|
| 7 |
+
2. remove "(CNN)" in article.
|
bart_cnndm.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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uggingFace Datasets Authors.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
# Lint as: python3
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
"""CNN/DailyMail Summarization dataset, non-anonymized version for BART model.
|
| 20 |
+
|
| 21 |
+
Major changes:
|
| 22 |
+
1. remove the space in " ." in fix_missing_period.
|
| 23 |
+
2. remove "(CNN)" in article.
|
| 24 |
+
|
| 25 |
+
The goal is to reproduce the results in BART.
|
| 26 |
+
|
| 27 |
+
References: https://github.com/facebookresearch/fairseq/issues/1401
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
import hashlib
|
| 31 |
+
import os
|
| 32 |
+
|
| 33 |
+
import datasets
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
logger = datasets.logging.get_logger(__name__)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
_HOMEPAGE = "https://github.com/abisee/cnn-dailymail"
|
| 40 |
+
|
| 41 |
+
_DESCRIPTION = """\
|
| 42 |
+
CNN/DailyMail non-anonymized summarization dataset.
|
| 43 |
+
There are two features:
|
| 44 |
+
- article: text of news article, used as the document to be summarized
|
| 45 |
+
- highlights: joined text of highlights with <s> and </s> around each
|
| 46 |
+
highlight, which is the target summary
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
# The second citation introduces the source data, while the first
|
| 50 |
+
# introduces the specific form (non-anonymized) we use here.
|
| 51 |
+
_CITATION = """\
|
| 52 |
+
@article{DBLP:journals/corr/SeeLM17,
|
| 53 |
+
author = {Abigail See and
|
| 54 |
+
Peter J. Liu and
|
| 55 |
+
Christopher D. Manning},
|
| 56 |
+
title = {Get To The Point: Summarization with Pointer-Generator Networks},
|
| 57 |
+
journal = {CoRR},
|
| 58 |
+
volume = {abs/1704.04368},
|
| 59 |
+
year = {2017},
|
| 60 |
+
url = {http://arxiv.org/abs/1704.04368},
|
| 61 |
+
archivePrefix = {arXiv},
|
| 62 |
+
eprint = {1704.04368},
|
| 63 |
+
timestamp = {Mon, 13 Aug 2018 16:46:08 +0200},
|
| 64 |
+
biburl = {https://dblp.org/rec/bib/journals/corr/SeeLM17},
|
| 65 |
+
bibsource = {dblp computer science bibliography, https://dblp.org}
|
| 66 |
+
}
|
| 67 |
+
@inproceedings{hermann2015teaching,
|
| 68 |
+
title={Teaching machines to read and comprehend},
|
| 69 |
+
author={Hermann, Karl Moritz and Kocisky, Tomas and Grefenstette, Edward and Espeholt, Lasse and Kay, Will and Suleyman, Mustafa and Blunsom, Phil},
|
| 70 |
+
booktitle={Advances in neural information processing systems},
|
| 71 |
+
pages={1693--1701},
|
| 72 |
+
year={2015}
|
| 73 |
+
}
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
_DL_URLS = {
|
| 77 |
+
"cnn_stories": "https://huggingface.co/datasets/cnn_dailymail/resolve/11343c3752184397d56efc19a8a7cceb68089318/data/cnn_stories.tgz",
|
| 78 |
+
"dm_stories": "https://huggingface.co/datasets/cnn_dailymail/resolve/11343c3752184397d56efc19a8a7cceb68089318/data/dailymail_stories.tgz",
|
| 79 |
+
"train": "https://raw.githubusercontent.com/abisee/cnn-dailymail/master/url_lists/all_train.txt",
|
| 80 |
+
"validation": "https://raw.githubusercontent.com/abisee/cnn-dailymail/master/url_lists/all_val.txt",
|
| 81 |
+
"test": "https://raw.githubusercontent.com/abisee/cnn-dailymail/master/url_lists/all_test.txt",
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
_HIGHLIGHTS = "highlights"
|
| 85 |
+
_ARTICLE = "article"
|
| 86 |
+
|
| 87 |
+
_SUPPORTED_VERSIONS = [
|
| 88 |
+
# Using cased version.
|
| 89 |
+
datasets.Version("3.0.0", "Using cased version."),
|
| 90 |
+
# Same data as 0.0.2
|
| 91 |
+
datasets.Version("1.0.0", ""),
|
| 92 |
+
# Having the model predict newline separators makes it easier to evaluate
|
| 93 |
+
# using summary-level ROUGE.
|
| 94 |
+
datasets.Version("2.0.0", "Separate target sentences with newline."),
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
_DEFAULT_VERSION = datasets.Version("3.0.0", "Using cased version.")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class CnnDailymailConfig(datasets.BuilderConfig):
|
| 102 |
+
"""BuilderConfig for CnnDailymail."""
|
| 103 |
+
|
| 104 |
+
def __init__(self, **kwargs):
|
| 105 |
+
"""BuilderConfig for CnnDailymail.
|
| 106 |
+
Args:
|
| 107 |
+
**kwargs: keyword arguments forwarded to super.
|
| 108 |
+
"""
|
| 109 |
+
super(CnnDailymailConfig, self).__init__(**kwargs)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def _get_url_hashes(path):
|
| 113 |
+
"""Get hashes of urls in file."""
|
| 114 |
+
urls = _read_text_file_path(path)
|
| 115 |
+
|
| 116 |
+
def url_hash(u):
|
| 117 |
+
h = hashlib.sha1()
|
| 118 |
+
try:
|
| 119 |
+
u = u.encode("utf-8")
|
| 120 |
+
except UnicodeDecodeError:
|
| 121 |
+
logger.error("Cannot hash url: %s", u)
|
| 122 |
+
h.update(u)
|
| 123 |
+
return h.hexdigest()
|
| 124 |
+
|
| 125 |
+
return {url_hash(u) for u in urls}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _get_hash_from_path(p):
|
| 129 |
+
"""Extract hash from path."""
|
| 130 |
+
return os.path.splitext(os.path.basename(p))[0]
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
DM_SINGLE_CLOSE_QUOTE = "\u2019" # unicode
|
| 134 |
+
DM_DOUBLE_CLOSE_QUOTE = "\u201d"
|
| 135 |
+
# acceptable ways to end a sentence
|
| 136 |
+
END_TOKENS = [
|
| 137 |
+
".",
|
| 138 |
+
"!",
|
| 139 |
+
"?",
|
| 140 |
+
"...",
|
| 141 |
+
"'",
|
| 142 |
+
"`",
|
| 143 |
+
'"',
|
| 144 |
+
DM_SINGLE_CLOSE_QUOTE,
|
| 145 |
+
DM_DOUBLE_CLOSE_QUOTE,
|
| 146 |
+
")",
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def _read_text_file_path(path):
|
| 151 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 152 |
+
lines = [line.strip() for line in f]
|
| 153 |
+
return lines
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _read_text_file(file):
|
| 157 |
+
return [line.decode("utf-8").strip() for line in file]
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def _get_art_abs(story_file, tfds_version):
|
| 161 |
+
"""Get abstract (highlights) and article from a story file path."""
|
| 162 |
+
# Based on https://github.com/abisee/cnn-dailymail/blob/master/
|
| 163 |
+
# make_datafiles.py
|
| 164 |
+
|
| 165 |
+
lines = _read_text_file(story_file)
|
| 166 |
+
|
| 167 |
+
# The github code lowercase the text and we removed it in 3.0.0.
|
| 168 |
+
|
| 169 |
+
# Put periods on the ends of lines that are missing them
|
| 170 |
+
# (this is a problem in the dataset because many image captions don't end in
|
| 171 |
+
# periods; consequently they end up in the body of the article as run-on
|
| 172 |
+
# sentences)
|
| 173 |
+
def fix_missing_period(line):
|
| 174 |
+
"""Adds a period to a line that is missing a period."""
|
| 175 |
+
if "@highlight" in line:
|
| 176 |
+
return line
|
| 177 |
+
if not line:
|
| 178 |
+
return line
|
| 179 |
+
if line[-1] in END_TOKENS:
|
| 180 |
+
return line
|
| 181 |
+
return line + "."
|
| 182 |
+
|
| 183 |
+
lines = [fix_missing_period(line) for line in lines]
|
| 184 |
+
|
| 185 |
+
# Separate out article and abstract sentences
|
| 186 |
+
article_lines = []
|
| 187 |
+
highlights = []
|
| 188 |
+
next_is_highlight = False
|
| 189 |
+
for line in lines:
|
| 190 |
+
if not line:
|
| 191 |
+
continue # empty line
|
| 192 |
+
elif line.startswith("@highlight"):
|
| 193 |
+
next_is_highlight = True
|
| 194 |
+
elif next_is_highlight:
|
| 195 |
+
highlights.append(line)
|
| 196 |
+
else:
|
| 197 |
+
article_lines.append(line)
|
| 198 |
+
|
| 199 |
+
# Make article into a single string
|
| 200 |
+
article = " ".join(article_lines)
|
| 201 |
+
|
| 202 |
+
if tfds_version >= "2.0.0":
|
| 203 |
+
abstract = "\n".join(highlights)
|
| 204 |
+
else:
|
| 205 |
+
abstract = " ".join(highlights)
|
| 206 |
+
|
| 207 |
+
if article[:5] == "(CNN)":
|
| 208 |
+
article = article[5:]
|
| 209 |
+
|
| 210 |
+
return article, abstract
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
class CnnDailymail(datasets.GeneratorBasedBuilder):
|
| 214 |
+
"""CNN/DailyMail non-anonymized summarization dataset."""
|
| 215 |
+
|
| 216 |
+
BUILDER_CONFIGS = [
|
| 217 |
+
CnnDailymailConfig(name=str(version), description="Plain text", version=version)
|
| 218 |
+
for version in _SUPPORTED_VERSIONS
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
def _info(self):
|
| 222 |
+
return datasets.DatasetInfo(
|
| 223 |
+
description=_DESCRIPTION,
|
| 224 |
+
features=datasets.Features(
|
| 225 |
+
{
|
| 226 |
+
_ARTICLE: datasets.Value("string"),
|
| 227 |
+
_HIGHLIGHTS: datasets.Value("string"),
|
| 228 |
+
"id": datasets.Value("string"),
|
| 229 |
+
}
|
| 230 |
+
),
|
| 231 |
+
supervised_keys=None,
|
| 232 |
+
homepage=_HOMEPAGE,
|
| 233 |
+
citation=_CITATION,
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
def _vocab_text_gen(self, paths):
|
| 237 |
+
for _, ex in self._generate_examples(paths):
|
| 238 |
+
yield " ".join([ex[_ARTICLE], ex[_HIGHLIGHTS]])
|
| 239 |
+
|
| 240 |
+
def _split_generators(self, dl_manager):
|
| 241 |
+
dl_paths = dl_manager.download(_DL_URLS)
|
| 242 |
+
return [
|
| 243 |
+
datasets.SplitGenerator(
|
| 244 |
+
name=split,
|
| 245 |
+
gen_kwargs={
|
| 246 |
+
"urls_file": dl_paths[split],
|
| 247 |
+
"files_per_archive": [
|
| 248 |
+
dl_manager.iter_archive(dl_paths["cnn_stories"]),
|
| 249 |
+
dl_manager.iter_archive(dl_paths["dm_stories"]),
|
| 250 |
+
],
|
| 251 |
+
},
|
| 252 |
+
)
|
| 253 |
+
for split in [
|
| 254 |
+
datasets.Split.TRAIN,
|
| 255 |
+
datasets.Split.VALIDATION,
|
| 256 |
+
datasets.Split.TEST,
|
| 257 |
+
]
|
| 258 |
+
]
|
| 259 |
+
|
| 260 |
+
def _generate_examples(self, urls_file, files_per_archive):
|
| 261 |
+
urls = _get_url_hashes(urls_file)
|
| 262 |
+
idx = 0
|
| 263 |
+
for files in files_per_archive:
|
| 264 |
+
for path, file in files:
|
| 265 |
+
hash_from_path = _get_hash_from_path(path)
|
| 266 |
+
if hash_from_path in urls:
|
| 267 |
+
article, highlights = _get_art_abs(file, self.config.version)
|
| 268 |
+
if not article or not highlights:
|
| 269 |
+
continue
|
| 270 |
+
yield idx, {
|
| 271 |
+
_ARTICLE: article,
|
| 272 |
+
_HIGHLIGHTS: highlights,
|
| 273 |
+
"id": hash_from_path,
|
| 274 |
+
}
|
| 275 |
+
idx += 1
|