|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Reddit TIFU dataset using tifu or tldr from subreddit tifu.""" |
|
|
|
|
|
import json |
|
|
|
import datasets |
|
|
|
|
|
_CITATION = """ |
|
@misc{kim2018abstractive, |
|
title={Abstractive Summarization of Reddit Posts with Multi-level Memory Networks}, |
|
author={Byeongchang Kim and Hyunwoo Kim and Gunhee Kim}, |
|
year={2018}, |
|
eprint={1811.00783}, |
|
archivePrefix={arXiv}, |
|
primaryClass={cs.CL} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """ |
|
Reddit dataset, where TIFU denotes the name of subbreddit /r/tifu. |
|
As defined in the publication, styel "short" uses title as summary and |
|
"long" uses tldr as summary. |
|
|
|
Features includes: |
|
- document: post text without tldr. |
|
- tldr: tldr line. |
|
- title: trimmed title without tldr. |
|
- ups: upvotes. |
|
- score: score. |
|
- num_comments: number of comments. |
|
- upvote_ratio: upvote ratio. |
|
""" |
|
|
|
_URL = "data/tifu_all_tokenized_and_filtered.json.gz" |
|
|
|
_DOCUMENT = "documents" |
|
_TITLE = "title" |
|
_TLDR = "tldr" |
|
_ADDITIONAL_FEATURES = ["ups", "num_comments", "score", "upvote_ratio"] |
|
|
|
|
|
class RedditTifuConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for RedditTifu.""" |
|
|
|
def __init__(self, summary_key=None, **kwargs): |
|
"""BuilderConfig for RedditTifu. |
|
|
|
Args: |
|
summary_key: key string of summary in downloaded json file. |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
|
|
super(RedditTifuConfig, self).__init__(version=datasets.Version("1.1.0"), **kwargs) |
|
self.summary_key = summary_key |
|
|
|
|
|
class RedditTifu(datasets.GeneratorBasedBuilder): |
|
"""Reddit TIFU Dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
RedditTifuConfig( |
|
name="short", |
|
summary_key=_TITLE, |
|
description="Using title as summary.", |
|
), |
|
RedditTifuConfig( |
|
name="long", |
|
summary_key=_TLDR, |
|
description="Using TLDR as summary.", |
|
), |
|
] |
|
|
|
def _info(self): |
|
features = { |
|
"ups": datasets.Value("float32"), |
|
"num_comments": datasets.Value("float32"), |
|
"upvote_ratio": datasets.Value("float32"), |
|
"score": datasets.Value("float32"), |
|
} |
|
features.update({k: datasets.Value("string") for k in [_DOCUMENT, _TLDR, _TITLE]}) |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features(features), |
|
supervised_keys=(_DOCUMENT, self.config.summary_key), |
|
homepage="https://github.com/ctr4si/MMN", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
dl_path = dl_manager.download_and_extract(_URL) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"path": dl_path}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, path=None): |
|
"""Yields examples.""" |
|
with open(path, "rb") as f: |
|
for i, line in enumerate(f): |
|
|
|
|
|
|
|
|
|
|
|
d = json.loads(line) |
|
r = { |
|
_DOCUMENT: d["selftext_without_tldr"].strip(), |
|
_TITLE: d["trimmed_title"].strip(), |
|
_TLDR: (d["tldr"] or "").strip(), |
|
} |
|
r.update({k: d[k] for k in _ADDITIONAL_FEATURES}) |
|
|
|
if r[_DOCUMENT] and r[self.config.summary_key]: |
|
yield i, r |
|
|