schen149 commited on
Commit
ab61ac4
1 Parent(s): 852273b

adding dataset loader file

Browse files
Files changed (1) hide show
  1. propsegment.py +181 -0
propsegment.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """PropSegmEnt: A Large-Scale Corpus for Proposition-Level Segmentation and Entailment Recognition."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+ _CITATION = """\
25
+ @inproceedings{chen2023propsegment,
26
+ title = "{PropSegmEnt}: A Large-Scale Corpus for Proposition-Level Segmentation and Entailment Recognition",
27
+ author = "Chen, Sihao and Buthpitiya, Senaka and Fabrikant, Alex and Roth, Dan and Schuster, Tal",
28
+ booktitle = "Findings of the Association for Computational Linguistics: ACL 2023",
29
+ year = "2023",
30
+ }
31
+ """
32
+
33
+ # TODO: Add description of the dataset here
34
+ # You can copy an official description
35
+ _DESCRIPTION = """\
36
+ This is a reproduced (i.e. after web-crawling) and processed version of the "PropSegment" dataset from Google Research.
37
+
38
+ Since the News portion of the dataset is released only via urls, we reconstruct the dataset by crawling. Overall, ~96%
39
+ of the dataset can be reproduced, and the rest ~4% either have url no longer valid, or sentences that have been edited
40
+ (i.e. cannot be aligned with the orignial dataset).
41
+
42
+ PropSegment (Proposition-level Segmentation and Entailment) is a large-scale, human annotated dataset for segmenting
43
+ English text into propositions, and recognizing proposition-level entailment relations --- whether a different, related
44
+ document entails each proposition, contradicts it, or neither.
45
+
46
+ The original dataset features >45k human annotated propositions, i.e. individual semantic units within sentences, as
47
+ well as >35k entailment labels between propositions and documents.
48
+ """
49
+
50
+ _HOMEPAGE = "https://github.com/google-research-datasets/PropSegmEnt"
51
+
52
+ _LICENSE = "CC-BY-4.0"
53
+
54
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
55
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
56
+ _URL = "https://raw.githubusercontent.com/schen149/PropSegmEnt/main/"
57
+ _URLS = {
58
+ "segmentation": {
59
+ "train": _URL + "proposition_segmentation.train.jsonl",
60
+ "dev": _URL + "proposition_segmentation.dev.jsonl",
61
+ "test": _URL + "proposition_segmentation.test.jsonl",
62
+ },
63
+ "nli": {
64
+ "train": _URL + "propnli.train.jsonl",
65
+ "dev": _URL + "propnli.dev.jsonl",
66
+ "test": _URL + "propnli.test.jsonl",
67
+ }
68
+ }
69
+
70
+ _CONFIG_TO_FILENAME = {
71
+ "segmentation": "proposition_segmentation",
72
+ "nli": "propnli"
73
+ }
74
+
75
+ class PropSegment(datasets.GeneratorBasedBuilder):
76
+
77
+ VERSION = datasets.Version("1.0.0")
78
+
79
+ # This is an example of a dataset with multiple configurations.
80
+ # If you don't want/need to define several sub-sets in your dataset,
81
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
82
+
83
+ # If you need to make complex sub-parts in the datasets with configurable options
84
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
85
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
86
+
87
+ # You will be able to load one or the other configurations in the following list with
88
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
89
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
90
+ BUILDER_CONFIGS = [
91
+ datasets.BuilderConfig(name="segmentation", version=VERSION, description="This part of my dataset covers a first domain"),
92
+ datasets.BuilderConfig(name="nli", version=VERSION, description="This part of my dataset covers a second domain"),
93
+ ]
94
+
95
+ DEFAULT_CONFIG_NAME = "segmentation" # It's not mandatory to have a default configuration. Just use one if it make sense.
96
+
97
+ def _info(self):
98
+ if self.config.name == "segmentation": # This is the name of the configuration selected in BUILDER_CONFIGS above
99
+ features = datasets.Features(
100
+ {
101
+ "sentence": datasets.Value("string"),
102
+ "propositions": datasets.Value("string"),
103
+ }
104
+ )
105
+ else:
106
+ features = datasets.Features(
107
+ {
108
+ "hypothesis": datasets.Value("string"),
109
+ "premise": datasets.Value("string"),
110
+ "label": datasets.Value("string")
111
+ }
112
+ )
113
+ return datasets.DatasetInfo(
114
+ # This is the description that will appear on the datasets page.
115
+ description=_DESCRIPTION,
116
+ # This defines the different columns of the dataset and their types
117
+ features=features, # Here we define them above because they are different between the two configurations
118
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
119
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
120
+ # supervised_keys=("sentence", "label"),
121
+ # Homepage of the dataset for documentation
122
+ homepage=_HOMEPAGE,
123
+ # License for the dataset if available
124
+ license=_LICENSE,
125
+ # Citation for the dataset
126
+ citation=_CITATION,
127
+ )
128
+
129
+ def _split_generators(self, dl_manager):
130
+ config_name = self.config.name
131
+ urls = _URLS[config_name]
132
+ data_dir = dl_manager.download_and_extract(urls)
133
+ file_prefix = _CONFIG_TO_FILENAME[config_name]
134
+
135
+ return [
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.TRAIN,
138
+ # These kwargs will be passed to _generate_examples
139
+ gen_kwargs={
140
+ "filepath": os.path.join(
141
+ data_dir, "{}.train.jsonl".format(file_prefix)),
142
+ "split": "train",
143
+ },
144
+ ),
145
+ datasets.SplitGenerator(
146
+ name=datasets.Split.VALIDATION,
147
+ # These kwargs will be passed to _generate_examples
148
+ gen_kwargs={
149
+ "filepath": os.path.join(
150
+ data_dir, "{}.dev.jsonl".format(file_prefix)),
151
+ "split": "dev",
152
+ },
153
+ ),
154
+ datasets.SplitGenerator(
155
+ name=datasets.Split.TEST,
156
+ # These kwargs will be passed to _generate_examples
157
+ gen_kwargs={
158
+ "filepath": os.path.join(
159
+ data_dir, "{}.test.jsonl".format(file_prefix)),
160
+ "split": "test"
161
+ },
162
+ ),
163
+ ]
164
+
165
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
166
+ def _generate_examples(self, filepath, split):
167
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
168
+ with open(filepath, encoding="utf-8") as f:
169
+ for key, row in enumerate(f):
170
+ data = json.loads(row)
171
+ if self.config.name == "segmentation":
172
+ yield key, {
173
+ "sentence": data["sentence"],
174
+ "propositions": data["propositions"],
175
+ }
176
+ else:
177
+ yield key, {
178
+ "hypothesis": data["hypothesis"],
179
+ "premise": data["premise"],
180
+ "label": data["label"],
181
+ }