vr18 commited on
Commit
7a63386
1 Parent(s): 3f0fee1

Adding loading script

Browse files
Files changed (1) hide show
  1. court_view_generation.py +198 -0
court_view_generation.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """Dataset for the Court View Generation task."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import lzma
21
+ import os
22
+
23
+ import datasets
24
+ try:
25
+ import lzma as xz
26
+ except ImportError:
27
+ import pylzma as xz
28
+
29
+
30
+ # TODO: Add BibTeX citation
31
+ # Find for instance the citation on arxiv or on the dataset repo/website
32
+ _CITATION = """\
33
+ @InProceedings{huggingface:dataset,
34
+ title = {A great new dataset},
35
+ author={huggingface, Inc.
36
+ },
37
+ year={2020}
38
+ }
39
+ """
40
+
41
+ # You can copy an official description
42
+ _DESCRIPTION = """\
43
+ This dataset contains court decision for court view generation task.
44
+ """
45
+
46
+ # TODO: Add a link to an official homepage for the dataset here
47
+ _HOMEPAGE = ""
48
+
49
+ # TODO: Add the licence for the dataset here if you can find it
50
+ _LICENSE = ""
51
+
52
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
53
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
54
+ _URLS = {
55
+ "full": "https://huggingface.co/datasets/rcds/court_view_generation/resolve/main/L1/huggingface",
56
+ "level_2": "https://huggingface.co/datasets/rcds/court_view_generation/resolve/main/L2/huggingface"
57
+ }
58
+
59
+
60
+ class CourtViewGeneration(datasets.GeneratorBasedBuilder):
61
+ """This dataset contains court decision for court view generation task."""
62
+
63
+ VERSION = datasets.Version("1.1.0")
64
+
65
+ # This is an example of a dataset with multiple configurations.
66
+ # If you don't want/need to define several sub-sets in your dataset,
67
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
68
+
69
+ # If you need to make complex sub-parts in the datasets with configurable options
70
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
71
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
72
+
73
+ # You will be able to load one or the other configurations in the following list with
74
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
75
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
76
+ BUILDER_CONFIGS = [
77
+ datasets.BuilderConfig(name="full", version=VERSION, description="This part of my dataset covers the whole dataset"),
78
+ datasets.BuilderConfig(name="level_2", version=VERSION, description="This part of my dataset covers a subset containing only cases with origin data")
79
+ ]
80
+
81
+ DEFAULT_CONFIG_NAME = "full" # It's not mandatory to have a default configuration. Just use one if it make sense.
82
+
83
+ def _info(self):
84
+ if self.config.name == "full": # This is the name of the configuration selected in BUILDER_CONFIGS above
85
+ features = datasets.Features(
86
+ {
87
+ "decision_id": datasets.Value("string"),
88
+ "facts": datasets.Value("string"),
89
+ "considerations": datasets.Value("string"),
90
+ "court": datasets.Value("string"),
91
+ "origin_facts": datasets.Value("string"),
92
+ "origin_considerations": datasets.Value("string")
93
+ # These are the features of your dataset like images, labels ...
94
+ }
95
+ )
96
+ else: # This is an example to show how to have different features for "first_domain" and "second_domain"
97
+ features = datasets.Features(
98
+ {
99
+ "decision_id": datasets.Value("string"),
100
+ "facts": datasets.Value("string"),
101
+ "considerations": datasets.Value("string"),
102
+ "court": datasets.Value("string"),
103
+ "origin_facts": datasets.Value("string"),
104
+ "origin_considerations": datasets.Value("string")
105
+ # These are the features of your dataset like images, labels ...
106
+ }
107
+ )
108
+ return datasets.DatasetInfo(
109
+ # This is the description that will appear on the datasets page.
110
+ description=_DESCRIPTION,
111
+ # This defines the different columns of the dataset and their types
112
+ features=features, # Here we define them above because they are different between the two configurations
113
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
114
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
115
+ # supervised_keys=("sentence", "label"),
116
+ # Homepage of the dataset for documentation
117
+ # homepage=_HOMEPAGE,
118
+ # License for the dataset if available
119
+ # license=_LICENSE,
120
+ # Citation for the dataset
121
+ # citation=_CITATION,
122
+ )
123
+
124
+ def _split_generators(self, dl_manager):
125
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
126
+
127
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
128
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
129
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
130
+ urls = _URLS[self.config.name]
131
+ filepath_train = dl_manager.download(os.path.join(urls, "train.jsonl.xz"))
132
+ filepath_validation = dl_manager.download(os.path.join(urls, "validation.jsonl.xz"))
133
+ filepath_test = dl_manager.download(os.path.join(urls, "test.jsonl.xz"))
134
+
135
+ print("filepath_train", filepath_train)
136
+ # show files in the downloaded and extracted dir
137
+ print(os.listdir(os.path.dirname(filepath_train)))
138
+
139
+ return [
140
+ datasets.SplitGenerator(
141
+ name=datasets.Split.TRAIN,
142
+ # These kwargs will be passed to _generate_examples
143
+ gen_kwargs={
144
+ "filepath": filepath_train,
145
+ "split": "train",
146
+ },
147
+ ),
148
+ datasets.SplitGenerator(
149
+ name=datasets.Split.VALIDATION,
150
+ # These kwargs will be passed to _generate_examples
151
+ gen_kwargs={
152
+ "filepath": filepath_validation,
153
+ "split": "validation",
154
+ },
155
+ ),
156
+ datasets.SplitGenerator(
157
+ name=datasets.Split.TEST,
158
+ # These kwargs will be passed to _generate_examples
159
+ gen_kwargs={
160
+ "filepath": filepath_test,
161
+ "split": "test"
162
+ },
163
+ ),
164
+ ]
165
+
166
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
167
+ def _generate_examples(self, filepath, split):
168
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
169
+ line_counter = 0
170
+ try:
171
+ with xz.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
172
+ for id, line in enumerate(f):
173
+ line_counter += 1
174
+ print(id, len(line), line[:200])
175
+ if line:
176
+ data = json.loads(line)
177
+ if self.config.name == "all":
178
+ yield id, {
179
+ "decision_id": data["decision_id"],
180
+ "facts": data["facts"],
181
+ "considerations": data["considerations"],
182
+ "court": data["court"],
183
+ "origin_facts": data["origin_facts"],
184
+ "origin_considerations": data["origin_considerations"]
185
+ }
186
+ else:
187
+ yield id, {
188
+ "decision_id": data["decision_id"],
189
+ "facts": data["facts"],
190
+ "considerations": data["considerations"],
191
+ "court": data["court"],
192
+ "origin_facts": data["origin_facts"],
193
+ "origin_considerations": data["origin_considerations"]
194
+ }
195
+ except lzma.LZMAError as e:
196
+ print(split, e)
197
+ if line_counter == 0:
198
+ raise e