clairebarale commited on
Commit
478e8ad
1 Parent(s): 7be07aa

Upload asylex.py

Browse files
Files changed (1) hide show
  1. asylex.py +362 -0
asylex.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+ import datasets
22
+
23
+ logger = datasets.logging.get_logger(__name__)
24
+
25
+
26
+ _VERSION = datasets.Version("1.1.0")
27
+
28
+ _DESCRIPTION = """AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""
29
+
30
+ _HOMEPAGE = "https://huggingface.co/datasets/clairebarale/AsyLex"
31
+
32
+ _LICENSE = "cc-by-nc-sa-4.0"
33
+
34
+ # TODO: Add link to the official dataset URLs here
35
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
36
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
37
+ _URLS = {
38
+ "raw_documents": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/cases_anonymized_txt_raw.tar.gz",
39
+ "raw_sentences": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/all_sentences_anonymized.tar.xz",
40
+ "all_legal_entities": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/main_and_case_cover_all_entities_inferred.csv",
41
+ "casecover_legal_entities": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/case_cover/case_cover_anonymised_extracted_entities.csv",
42
+ "casecover_entities_outcome": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/case_cover/case_cover_entities_and_decision_outcome.csv",
43
+ "determination_sentences": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/determination_label_extracted_sentences.csv",
44
+ "outcome_classification": "https://huggingface.co/datasets/clairebarale/AsyLex/tree/main/outcome_train_test/"
45
+ }
46
+
47
+ class AsyLexConfig(datasets.BuilderConfig):
48
+ """BuilderConfig for AsyLex"""
49
+ def __init__(self, url, **kwargs):
50
+ super(AsyLexConfig, self).__init__(**kwargs)
51
+ self.url = url
52
+
53
+
54
+ class Asylex(datasets.GeneratorBasedBuilder):
55
+ """AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""
56
+
57
+ VERSION = datasets.Version(_VERSION)
58
+
59
+ BUILDER_CONFIG_CLASS = AsyLexConfig
60
+
61
+
62
+ BUILDER_CONFIGS = [
63
+ AsyLexConfig(
64
+ name="raw_documents",
65
+ description = "contains the raw text from all documents, by case, with the corresponding case identifier",
66
+ version=datasets.Version(_VERSION, ""),
67
+ url = _URLS["raw_documents"]
68
+ ),
69
+ AsyLexConfig(
70
+ name="raw_sentences",
71
+ description = "contains the raw text from all retrieved documents, split by sentences, with the corresponding case identifier",
72
+ version=datasets.Version(_VERSION, ""),
73
+ url = _URLS["raw_sentences"]
74
+ ),
75
+ AsyLexConfig(
76
+ name="all_legal_entities",
77
+ description = "contains the structured dataset, all extracted entities (one column per entity type), with the corresponding case identifier",
78
+ version=datasets.Version(_VERSION, ""),
79
+ url = _URLS["all_legal_entities"]
80
+ ),
81
+ AsyLexConfig(
82
+ name="casecover_legal_entities",
83
+ description = "contains the structured dataset derived from the case covers only (one column per entity type), with the corresponding case identifier",
84
+ version=datasets.Version(_VERSION, ""),
85
+ url = _URLS["casecover_legal_entities"]
86
+ ),
87
+ AsyLexConfig(
88
+ name="casecover_entities_outcome",
89
+ description = "contains the structured dataset derived from the case covers only (one column per entity type), with the corresponding case identifier, with the addition of the decision outcome of the case",
90
+ version=datasets.Version(_VERSION, ""),
91
+ url = _URLS["casecover_entities_outcome"]
92
+ ),
93
+ AsyLexConfig(
94
+ name="determination_sentences",
95
+ description = "contains all sentences that have been extracted with the Entity Type determination. All sentences included here should therefore directly state the outcome of the decision, with the correspinding case identifier",
96
+ version=datasets.Version(_VERSION, ""),
97
+ url = _URLS["determination_sentences"]
98
+ ),
99
+ AsyLexConfig(
100
+ name="outcome_classification",
101
+ description = "folder containing a train and test set for the task of outcome classificiation. Each set includes the case identifier and the decision outcome (0,1,2). The test set only contains gold-standard manually labeled data.",
102
+ version=datasets.Version(_VERSION, ""),
103
+ url = _URLS["outcome_classification"]
104
+ ),
105
+ ]
106
+
107
+ DEFAULT_CONFIG_NAME = "raw_sentences"
108
+
109
+ def _info(self):
110
+
111
+ if self.config.name == "raw_documents":
112
+ features = datasets.Features(
113
+ {
114
+ "text": datasets.Value("string"),
115
+ }
116
+ )
117
+ elif self.config.name == "raw_sentences":
118
+ features = datasets.Features(
119
+ {
120
+ "decisionID": datasets.Value("int64"),
121
+ "Text": datasets.Value("string"),
122
+ }
123
+ )
124
+ elif self.config.name == "all_legal_entities":
125
+ features = datasets.Features(
126
+ {
127
+ "decisionID": datasets.Value("int64"),
128
+ "Text": datasets.Value("string"),
129
+ "GPE": datasets.Value("string"),
130
+ "DATE": datasets.Value("string"),
131
+ "NORP": datasets.Value("string"),
132
+ "ORG": datasets.Value("string"),
133
+ "LAW": datasets.Value("string"),
134
+ "CLAIMANT_EVENTS": datasets.Value("string"),
135
+ "CREDIBILITY": datasets.Value("string"),
136
+ "DETERMINATION": datasets.Value("string"),
137
+ "CLAIMANT_INFO": datasets.Value("string"),
138
+ "PROCEDURE": datasets.Value("string"),
139
+ "DOC_EVIDENCE": datasets.Value("string"),
140
+ "EXPLANATION": datasets.Value("string"),
141
+ "LEGAL_GROUND": datasets.Value("string"),
142
+ "LAW_CASE": datasets.Value("string"),
143
+ "LAW_REPORT": datasets.Value("string"),
144
+ "decision_outcome": datasets.ClassLabel(
145
+ names=['Rejected', 'Granted', 'Uncertain']
146
+ ),
147
+ "extracted_dates": datasets.Value("string"),
148
+ "LOC_HEARING": datasets.Value("string"),
149
+ "TRIBUNAL": datasets.Value("string"),
150
+ "PUBLIC_PRIVATE_HEARING": datasets.Value("string"),
151
+ "INCHAMBER_VIRTUAL_HEARING": datasets.Value("string"),
152
+ "JUDGE": datasets.Value("string"),
153
+ "text_case_cover": datasets.Value("string"),
154
+ "DATE_DECISION": datasets.Value("string"),
155
+ }
156
+ )
157
+
158
+ elif self.config.name == "casecover_legal_entities":
159
+ features = datasets.Features(
160
+ {
161
+ "decision_ID": datasets.Value("int64"),
162
+ "extracted_dates": datasets.Value("string"),
163
+ "extracted_gpe": datasets.Value("string"),
164
+ "extracted_org": datasets.Value("string"),
165
+ "public_private_hearing": datasets.Value("string"),
166
+ "in_chamber_virtual": datasets.Value("string"),
167
+ "judge_name": datasets.Value("string"),
168
+ "date_decision": datasets.Value("string"),
169
+ "text_case_cover": datasets.Value("string"),
170
+ }
171
+ )
172
+ elif self.config.name == "casecover_entities_outcome":
173
+ features = datasets.Features(
174
+ {
175
+ "decision_ID": datasets.Value("int64"),
176
+ "extracted_dates": datasets.Value("string"),
177
+ "LOC_HEARING": datasets.Value("string"),
178
+ "TRIBUNAL": datasets.Value("string"),
179
+ "PUBLIC_PRIVATE_HEARING": datasets.Value("string"),
180
+ "INCHAMBER_VIRTUAL_HEARING": datasets.Value("string"),
181
+ "JUDGE": datasets.Value("string"),
182
+ "text_case_cover": datasets.Value("string"),
183
+ "DATE_DECISION": datasets.Value("string"),
184
+ "decision_outcome": datasets.ClassLabel(
185
+ names=['Rejected', 'Granted', 'Uncertain']),
186
+ }
187
+ )
188
+ elif self.config.name == "determination_sentences":
189
+ features = datasets.Features(
190
+ {
191
+ "decisionID": datasets.Value("int64"),
192
+ "extracted_sentences_determination": datasets.Value("string"),
193
+ }
194
+ )
195
+ elif self.config.name == "outcome_classification":
196
+ features = datasets.Features(
197
+ {
198
+ "decisionID": datasets.Value("float64"),
199
+ "decision_outcome": datasets.ClassLabel(
200
+ names=['Rejected', 'Granted', 'Uncertain']),
201
+ }
202
+ )
203
+
204
+ data_files = {
205
+ "train": "outcome_train_test/train_dataset_silver.csv",
206
+ "test": "outcome_train_test/test_dataset_gold.csv",
207
+ }
208
+ return datasets.DatasetInfo(
209
+ description=_DESCRIPTION,
210
+ features=features,
211
+ license=_LICENSE,
212
+ supervised_keys=None,
213
+ homepage=_HOMEPAGE,
214
+ )
215
+
216
+ def _split_generators(self, dl_manager):
217
+ # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
218
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
219
+
220
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
221
+ # 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.
222
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
223
+
224
+ urls_to_download = _URLS[self.config.name]
225
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
226
+
227
+ if self.config.name == "outcome_classification":
228
+ data_dir = dl_manager.download_and_extract(_URLS["outcome_classification"])
229
+ return [
230
+ datasets.SplitGenerator(
231
+ name=datasets.Split.TRAIN,
232
+ gen_kwargs={
233
+ "filepath": os.path.join(data_dir, "train_dataset_silver.csv"),
234
+ "split": "train",
235
+ },
236
+ ),
237
+ datasets.SplitGenerator(
238
+ name=datasets.Split.TEST,
239
+ # These kwargs will be passed to _generate_examples
240
+ gen_kwargs={
241
+ "filepath": os.path.join(data_dir, "test_dataset_gold.csv"),
242
+ "split": "test"
243
+ },
244
+ ),
245
+ ]
246
+ else:
247
+ return [
248
+ datasets.SplitGenerator(
249
+ name=datasets.Split.TRAIN,
250
+ gen_kwargs={
251
+ "filepath": downloaded_files,
252
+ "split": "train",
253
+ },
254
+ )
255
+ ]
256
+
257
+ # key value examples
258
+ def generate_examples(self, file_path, split):
259
+
260
+ logger.info("⏳ Generating examples from = %s", file_path)
261
+
262
+ if self.config.name == "raw_documents":
263
+ for idx, filename in enumerate(os.listdir(file_path)):
264
+ if filename.endswith(".txt"):
265
+ with open(os.path.join(file_path, filename), "r", encoding="utf-8") as f:
266
+ # Read the content of the text file
267
+ text_content = f.read()
268
+ yield idx, {"case_files": text_content}
269
+
270
+ elif self.config.name == "raw_sentences":
271
+ with open(file_path, "r", encoding="utf-8") as f:
272
+ data = csv.DictReader(f, delimiter = ";")
273
+ for idx, row in enumerate(data):
274
+ yield idx, {
275
+ "decisionID": int(row["decisionID"]),
276
+ "Text": row["Text"],
277
+ }
278
+
279
+ elif self.config.name == "all_legal_entities":
280
+ with open(file_path, "r", encoding="utf-8") as f:
281
+ reader = csv.DictReader(f, delimiter=";")
282
+ for idx, row in enumerate(reader):
283
+ yield idx, {
284
+ "decisionID": int(row["decisionID"]),
285
+ "Text": row["Text"],
286
+ "GPE": row["GPE"],
287
+ "DATE": row["DATE"],
288
+ "NORP": row["NORP"],
289
+ "ORG": row["ORG"],
290
+ "LAW": row["LAW"],
291
+ "CLAIMANT_EVENTS": row["CLAIMANT_EVENTS"],
292
+ "CREDIBILITY": row["CREDIBILITY"],
293
+ "DETERMINATION": row["DETERMINATION"],
294
+ "CLAIMANT_INFO": row["CLAIMANT_INFO"],
295
+ "PROCEDURE": row["PROCEDURE"],
296
+ "DOC_EVIDENCE": row["DOC_EVIDENCE"],
297
+ "EXPLANATION": row["EXPLANATION"],
298
+ "LEGAL_GROUND": row["LEGAL_GROUND"],
299
+ "LAW_CASE": row["LAW_CASE"],
300
+ "LAW_REPORT": row["LAW_REPORT"],
301
+ "decision_outcome": row["decision_outcome"],
302
+ "extracted_dates": row["extracted_dates"],
303
+ "LOC_HEARING": row["LOC_HEARING"],
304
+ "TRIBUNAL": row["TRIBUNAL"],
305
+ "PUBLIC_PRIVATE_HEARING": row["PUBLIC_PRIVATE_HEARING"],
306
+ "INCHAMBER_VIRTUAL_HEARING": row["INCHAMBER_VIRTUAL_HEARING"],
307
+ "JUDGE": row["JUDGE"],
308
+ "text_case_cover": row["text_case_cover"],
309
+ "DATE_DECISION": row["DATE_DECISION"],
310
+ }
311
+
312
+ elif self.config.name == "casecover_legal_entities":
313
+ with open(file_path, "r", encoding="utf-8") as f:
314
+ reader = csv.DictReader(f, delimiter=",")
315
+ for idx, row in enumerate(reader):
316
+ yield idx, {
317
+ "decision_ID": int(row["decision_ID"]),
318
+ "extracted_dates": row["extracted_dates"],
319
+ "extracted_gpe": row["extracted_gpe"],
320
+ "extracted_org": row["extracted_org"],
321
+ "public_private_hearing": row["public_private_hearing"],
322
+ "in_chamber_virtual": row["in_chamber_virtual"],
323
+ "judge_name": row["judge_name"],
324
+ "date_decision": row["date_decision"],
325
+ "text_case_cover": row["text_case_cover"],
326
+ }
327
+
328
+ elif self.config.name == "casecover_entities_outcome":
329
+ with open(file_path, "r", encoding="utf-8") as f:
330
+ reader = csv.DictReader(f, delimiter=";")
331
+ for idx, row in enumerate(reader):
332
+ yield idx, {
333
+ "decision_ID": int(row["decision_ID"]),
334
+ "extracted_dates": row["extracted_dates"],
335
+ "LOC_HEARING": row["LOC_HEARING"],
336
+ "TRIBUNAL": row["TRIBUNAL"],
337
+ "PUBLIC_PRIVATE_HEARING": row["PUBLIC_PRIVATE_HEARING"],
338
+ "INCHAMBER_VIRTUAL_HEARING": row["INCHAMBER_VIRTUAL_HEARING"],
339
+ "JUDGE": row["JUDGE"],
340
+ "text_case_cover": row["text_case_cover"],
341
+ "DATE_DECISION": row["DATE_DECISION"],
342
+ "decision_outcome": row["decision_outcome"],
343
+ }
344
+
345
+ elif self.config.name == "determination_sentences":
346
+ with open(file_path, "r", encoding="utf-8") as f:
347
+ reader = csv.DictReader(f, delimiter=";")
348
+ for idx, line in enumerate(reader):
349
+ yield idx, {
350
+ "decisionID": int(line["decisionID"]),
351
+ "extracted_sentences_determination": line["extracted_sentences_determination"],
352
+ }
353
+
354
+ elif self.config.name == "outcome_classification":
355
+ with open(file_path, "r", encoding="utf-8") as f:
356
+ reader = csv.DictReader(f, delimiter=";")
357
+ for idx, row in enumerate(reader):
358
+ yield idx, {
359
+ "decisionID": float(row["decisionID"]),
360
+ "decision_outcome": row["decision_outcome"],
361
+ }
362
+