sihaochen commited on
Commit
b442617
1 Parent(s): 73a6987

Create propsegment-retrieval.py

Browse files
Files changed (1) hide show
  1. propsegment-retrieval.py +178 -0
propsegment-retrieval.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @article{chen2023subsentence,
26
+ title={Sub-Sentence Encoder: Contrastive Learning of Propositional Semantic Representations},
27
+ author={Sihao Chen and Hongming Zhang and Tong Chen and Ben Zhou and Wenhao Yu and Dian Yu and Baolin Peng and Hongwei Wang and Dan Roth and Dong Yu},
28
+ journal={arXiv preprint arXiv:2311.04335},
29
+ year={2023},
30
+ URL = {https://arxiv.org/pdf/2311.04335.pdf}
31
+ }
32
+
33
+ @inproceedings{chen2023propsegment,
34
+ title = "{PropSegmEnt}: A Large-Scale Corpus for Proposition-Level Segmentation and Entailment Recognition",
35
+ author = "Chen, Sihao and Buthpitiya, Senaka and Fabrikant, Alex and Roth, Dan and Schuster, Tal",
36
+ booktitle = "Findings of the Association for Computational Linguistics: ACL 2023",
37
+ year = "2023",
38
+ }
39
+ """
40
+
41
+ # TODO: Add description of the dataset here
42
+ # You can copy an official description
43
+ _DESCRIPTION = """\
44
+ This contains the processed dataset for the atomic fact retrieval task of the "PropSegment" dataset.
45
+
46
+ The task features a test set of 8,865 queries propositions.
47
+ Each query proposition corresponds to 1-2 ground truth propositions from another document.
48
+ In total, there are 43,299 target candidate propositions.
49
+ Note that the query propositions are also included in the target set, so during evaluation, the query needs to be removed from the retrieved candidates.
50
+
51
+ Check out more details in our paper -- https://arxiv.org/pdf/2311.04335.pdf.
52
+ """
53
+
54
+ _HOMEPAGE = "https://github.com/schen149/sub-sentence-encoder"
55
+
56
+ _LICENSE = "CC-BY-4.0"
57
+
58
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
59
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
60
+ _URLS = {
61
+ "targets": {
62
+ "test": "propsegment_targets_all.jsonl",
63
+ },
64
+ "queries": {
65
+ "test": "propsegment_queries_all.jsonl",
66
+ }
67
+ }
68
+
69
+ _CONFIG_TO_FILENAME = {
70
+ "targets": "propsegment_targets_all",
71
+ "queries": "propsegment_queries_all"
72
+ }
73
+
74
+ class PropSegmentRetrieval(datasets.GeneratorBasedBuilder):
75
+
76
+ VERSION = datasets.Version("1.0.0")
77
+
78
+ # This is an example of a dataset with multiple configurations.
79
+ # If you don't want/need to define several sub-sets in your dataset,
80
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
81
+
82
+ # If you need to make complex sub-parts in the datasets with configurable options
83
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
84
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
85
+
86
+ # You will be able to load one or the other configurations in the following list with
87
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
88
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
89
+ BUILDER_CONFIGS = [
90
+ datasets.BuilderConfig(name="targets", version=VERSION, description="Query propositions of the atomic fact retrieval task"),
91
+ datasets.BuilderConfig(name="queries", version=VERSION, description="Target candidate propositions of the atomic fact retrieval task"),
92
+ ]
93
+
94
+ DEFAULT_CONFIG_NAME = "queries" # It's not mandatory to have a default configuration. Just use one if it make sense.
95
+
96
+ def _info(self):
97
+ if self.config.name == "queries": # This is the name of the configuration selected in BUILDER_CONFIGS above
98
+ features = datasets.Features(
99
+ {
100
+ "id": datasets.Value("string"),
101
+ "sentence_text": datasets.Value("string"),
102
+ "spans": datasets.Value("string"),
103
+ "labels": datasets.features.Sequence(datasets.Value("string")),
104
+ "tokens": datasets.features.Sequence(
105
+ {"text": datasets.Value("string"), "character_offset_of_token_in_sentence": datasets.Value("int32"),}
106
+ ),
107
+ "token_indices": datasets.features.Sequence(datasets.Value("int32"))
108
+ }
109
+ )
110
+ else:
111
+ features = datasets.Features(
112
+ {
113
+ "id": datasets.Value("string"),
114
+ "sentence_text": datasets.Value("string"),
115
+ "spans": datasets.Value("string"),
116
+ "tokens": datasets.features.Sequence(
117
+ {"text": datasets.Value("string"), "character_offset_of_token_in_sentence": datasets.Value("int32"),}
118
+ ),
119
+ "token_indices": datasets.features.Sequence(datasets.Value("int32"))
120
+ }
121
+ )
122
+ return datasets.DatasetInfo(
123
+ # This is the description that will appear on the datasets page.
124
+ description=_DESCRIPTION,
125
+ # This defines the different columns of the dataset and their types
126
+ features=features, # Here we define them above because they are different between the two configurations
127
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
128
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
129
+ # supervised_keys=("sentence", "label"),
130
+ # Homepage of the dataset for documentation
131
+ homepage=_HOMEPAGE,
132
+ # License for the dataset if available
133
+ license=_LICENSE,
134
+ # Citation for the dataset
135
+ citation=_CITATION,
136
+ )
137
+
138
+ def _split_generators(self, dl_manager):
139
+ config_name = self.config.name
140
+ urls = _URLS[config_name]
141
+
142
+ data_dir = dl_manager.download(urls)
143
+ file_prefix = _CONFIG_TO_FILENAME[config_name]
144
+
145
+ return [
146
+ datasets.SplitGenerator(
147
+ name=datasets.Split.TEST,
148
+ # These kwargs will be passed to _generate_examples
149
+ gen_kwargs={
150
+ "filepath": data_dir["test"],
151
+ "split": "test"
152
+ },
153
+ ),
154
+ ]
155
+
156
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
157
+ def _generate_examples(self, filepath, split):
158
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
159
+ with open(filepath, encoding="utf-8") as f:
160
+ for key, row in enumerate(f):
161
+ data = json.loads(row)
162
+ if self.config.name == "queries":
163
+ yield key, {
164
+ "id": data["id"],
165
+ "sentence_text": data["sentence_text"],
166
+ "spans": data["spans"],
167
+ "label": data["label"],
168
+ "tokens": data["tokens"],
169
+ "token_indices": data["token_indices"],
170
+ }
171
+ else:
172
+ yield key, {
173
+ "id": data["id"],
174
+ "sentence_text": data["sentence_text"],
175
+ "spans": data["spans"],
176
+ "tokens": data["tokens"],
177
+ "token_indices": data["token_indices"],
178
+ }