Abinaya Mahendiran commited on
Commit
ce90d54
1 Parent(s): 1a9b60a

Added data loader script - xsum

Browse files
Files changed (1) hide show
  1. xsum.py +119 -0
xsum.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+
4
+ import datasets
5
+
6
+ _CITATION = """\
7
+ @article{Narayan2018DontGM,
8
+ title={Don't Give Me the Details, Just the Summary! Topic-Aware Convolutional Neural Networks for Extreme Summarization},
9
+ author={Shashi Narayan and Shay B. Cohen and Mirella Lapata},
10
+ journal={ArXiv},
11
+ year={2018},
12
+ volume={abs/1808.08745}
13
+ }
14
+ """
15
+
16
+ _DESCRIPTION = """\
17
+ This is the XSUM subset of the GEM benchmark.
18
+ """
19
+ _URLs = {
20
+ "xsum": {
21
+ "data": "http://bollin.inf.ed.ac.uk/public/direct/XSUM-EMNLP18-Summary-Data-Original.tar.gz",
22
+ "splits": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_xsum_confidence_0.8.json",
23
+ "challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/xsum.zip",
24
+ },
25
+ }
26
+
27
+ _XSUM_REMOVE_LINES = set(
28
+ [
29
+ "Share this with\n",
30
+ "Email\n",
31
+ "Facebook\n",
32
+ "Messenger\n",
33
+ "Twitter\n",
34
+ "Pinterest\n",
35
+ "WhatsApp\n",
36
+ "Linkedin\n",
37
+ "LinkedIn\n",
38
+ "Copy this link\n",
39
+ "These are external links and will open in a new window\n",
40
+ ]
41
+ )
42
+
43
+ class Xsum(datasets.GeneratorBasedBuilder):
44
+
45
+ BUILDER_CONFIGS = [
46
+ datasets.BuilderConfig(
47
+ name=lang,
48
+ version=datasets.Version("1.0.0"),
49
+ description="",
50
+ )
51
+ ]
52
+
53
+ def _info(self):
54
+ return datasets.DatasetInfo(
55
+ description=_DESCRIPTION,
56
+ features = datasets.Features(
57
+ {
58
+ "gem_id": datasets.Value("string"),
59
+ "gem_parent_id": datasets.Value("string"),
60
+ "xsum_id": datasets.Value("string"),
61
+ "document": datasets.Value("string"),
62
+ "target": datasets.Value("string"),
63
+ "references": [datasets.Value("string")],
64
+ }
65
+ ),
66
+ supervised_keys=None,
67
+ homepage="",
68
+ citation=_CITATION,
69
+ )
70
+
71
+ def _split_generators(self, dl_manager):
72
+ """Returns SplitGenerators."""
73
+ dl_dir = dl_manager.download_and_extract(_URLs[self.config.name])
74
+ challenge_sets = [
75
+ ("challenge_train_sample", "train_xsum_RandomSample500.json"),
76
+ ("challenge_validation_sample", "validation_xsum_RandomSample500.json"),
77
+ ("challenge_test_backtranslation", "test_xsum_BackTranslation500.json"),
78
+ ("challenge_test_bfp_02", "test_xsum_ButterFingersPerturbation_p=0.02_500.json"),
79
+ ("challenge_test_bfp_05", "test_xsum_ButterFingersPerturbation_p=0.05_500.json"),
80
+ ("challenge_test_nopunc", "test_xsum_WithoutPunctuation500.json"),
81
+ ("challenge_test_covid", f"en_test_covid19.jsonl"),
82
+ ]
83
+ return [
84
+ datasets.SplitGenerator(
85
+ name=challenge_split,
86
+ gen_kwargs={
87
+ "filepath": os.path.join(dl_dir["challenge_set"], "xsum", filename),
88
+ "split": challenge_split,
89
+ },
90
+ )
91
+ for challenge_split, filename in challenge_sets
92
+ ]
93
+
94
+ def _generate_examples(self, filepath, split, filepaths=None, lang=None):
95
+ """Yields examples."""
96
+ if "challenge" in split:
97
+ if "covid" in split:
98
+ with open(filepath, encoding="utf-8") as f:
99
+ id_ = -1
100
+ for line in f:
101
+ data = json.loads(line)
102
+ id_ += 1
103
+ yield id_, {
104
+ "gem_id": f"{self.config.name}-{split}-{id_}",
105
+ "gem_parent_id": f"{self.config.name}-{split}-{id_}",
106
+ "xsum_id": data["url"],
107
+ "document": data["text"],
108
+ "target": data["summary"],
109
+ "references": [] if split == "train" else [data["summary"]],
110
+ }
111
+ else:
112
+ exples = json.load(open(filepath, encoding="utf-8"))
113
+ if isinstance(exples, dict):
114
+ assert len(exples) == 1, "multiple entries found"
115
+ exples = list(exples.values())[0]
116
+ for id_, exple in enumerate(exples):
117
+ exple["gem_parent_id"] = exple["gem_id"]
118
+ exple["gem_id"] = f"{self.config.name}-{split}-{id_}"
119
+ yield id_, exple