File size: 16,836 Bytes
478e8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7224e63
478e8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7224e63
 
 
478e8ad
7224e63
478e8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0509417
0affe6f
478e8ad
0affe6f
0509417
 
0affe6f
0509417
0affe6f
0509417
 
 
478e8ad
0509417
0affe6f
0509417
4c8d264
41631f8
6f3a9ad
0509417
 
 
 
0affe6f
0509417
 
 
6a9bb79
0509417
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0affe6f
0509417
 
 
6a9bb79
0509417
 
 
 
 
 
 
 
 
 
 
0affe6f
0509417
 
 
6a9bb79
0509417
 
 
 
 
 
 
 
 
 
 
 
0affe6f
0509417
 
 
6a9bb79
0509417
 
 
 
0affe6f
0509417
 
 
6a9bb79
0509417
 
478e8ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""


import csv
import json
import os
import datasets

logger = datasets.logging.get_logger(__name__)


_VERSION = "1.0.0"

_DESCRIPTION = """AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""

_HOMEPAGE = "https://huggingface.co/datasets/clairebarale/AsyLex"

_LICENSE = "cc-by-nc-sa-4.0"

_URLS = {
    "raw_documents": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/cases_anonymized_txt_raw.tar.gz",
    "raw_sentences": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/all_sentences_anonymized.tar.xz",
    "all_legal_entities": "https://huggingface.co/datasets/clairebarale/AsyLex/raw/main/main_and_case_cover_all_entities_inferred.csv",
    "casecover_legal_entities": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/case_cover/case_cover_anonymised_extracted_entities.csv",
    "casecover_entities_outcome": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/case_cover/case_cover_entities_and_decision_outcome.csv",
    "determination_sentences": "https://huggingface.co/datasets/clairebarale/AsyLex/blob/main/determination_label_extracted_sentences.csv",
    "outcome_classification": "https://huggingface.co/datasets/clairebarale/AsyLex/tree/main/outcome_train_test/"
}

class AsyLexConfig(datasets.BuilderConfig):
    """BuilderConfig for AsyLex"""
    def __init__(self, url, **kwargs):
        super(AsyLexConfig, self).__init__(**kwargs)
        self.url = url

 
class Asylex(datasets.GeneratorBasedBuilder):
    """AsyLex: A Dataset for Legal Language Processing of Refugee Claims"""
    
    VERSION = datasets.Version(_VERSION)
    
    BUILDER_CONFIG_CLASS = AsyLexConfig
    

    BUILDER_CONFIGS = [
        AsyLexConfig(
            name="raw_documents",
            description = "contains the raw text from all documents, by case, with the corresponding case identifier",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["raw_documents"]
        ),
        AsyLexConfig(
            name="raw_sentences",
            description = "contains the raw text from all retrieved documents, split by sentences, with the corresponding case identifier",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["raw_sentences"]
        ),
        AsyLexConfig(
            name="all_legal_entities",
            description = "contains the structured dataset, all extracted entities (one column per entity type), with the corresponding case identifier",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["all_legal_entities"]
        ),
        AsyLexConfig(
            name="casecover_legal_entities",
            description = "contains the structured dataset derived from the case covers only (one column per entity type), with the corresponding case identifier",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["casecover_legal_entities"]
        ),
        AsyLexConfig(
            name="casecover_entities_outcome",
            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",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["casecover_entities_outcome"]
        ),
        AsyLexConfig(
            name="determination_sentences",
            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",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["determination_sentences"]
        ),
        AsyLexConfig(
            name="outcome_classification",
            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.",
            version=datasets.Version(_VERSION, ""),
            url = _URLS["outcome_classification"]
        ),
    ]

    DEFAULT_CONFIG_NAME = "raw_sentences"  

    def _info(self):
        
        if self.config.name == "raw_documents":  
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.name == "raw_sentences":  
            features = datasets.Features(
                {
                    "decisionID": datasets.Value("int64"),
                    "Text": datasets.Value("string"),
                }
            )
        elif self.config.name == "all_legal_entities":  
                features = datasets.Features(
            {
            "decisionID": datasets.Value("int64"),
            "Text": datasets.Value("string"),
            "GPE": datasets.Value("string"),
            "DATE": datasets.Value("string"),
            "NORP": datasets.Value("string"),
            "ORG": datasets.Value("string"),
            "LAW": datasets.Value("string"),
            "CLAIMANT_EVENTS": datasets.Value("string"),
            "CREDIBILITY": datasets.Value("string"),
            "DETERMINATION": datasets.Value("string"),
            "CLAIMANT_INFO": datasets.Value("string"),
            "PROCEDURE": datasets.Value("string"),
            "DOC_EVIDENCE": datasets.Value("string"),
            "EXPLANATION": datasets.Value("string"),
            "LEGAL_GROUND": datasets.Value("string"),
            "LAW_CASE": datasets.Value("string"),
            "LAW_REPORT": datasets.Value("string"),
            "decision_outcome": datasets.ClassLabel(
                names=['Rejected', 'Granted', 'Uncertain']
            ),
            "extracted_dates": datasets.Value("string"),
            "LOC_HEARING": datasets.Value("string"),
            "TRIBUNAL": datasets.Value("string"),
            "PUBLIC_PRIVATE_HEARING": datasets.Value("string"),
            "INCHAMBER_VIRTUAL_HEARING": datasets.Value("string"),
            "JUDGE": datasets.Value("string"),
            "text_case_cover": datasets.Value("string"),
            "DATE_DECISION": datasets.Value("string"),
            }
        )
       
        elif self.config.name == "casecover_legal_entities":  
            features = datasets.Features(
            {
            "decision_ID": datasets.Value("int64"),
            "extracted_dates": datasets.Value("string"),
            "extracted_gpe": datasets.Value("string"),
            "extracted_org": datasets.Value("string"),
            "public_private_hearing": datasets.Value("string"),
            "in_chamber_virtual": datasets.Value("string"),
            "judge_name": datasets.Value("string"),
            "date_decision": datasets.Value("string"),
            "text_case_cover": datasets.Value("string"),
            }
        )
        elif self.config.name == "casecover_entities_outcome": 
            features = datasets.Features(
            {
            "decision_ID": datasets.Value("int64"),
            "extracted_dates": datasets.Value("string"),
            "LOC_HEARING": datasets.Value("string"),
            "TRIBUNAL": datasets.Value("string"),
            "PUBLIC_PRIVATE_HEARING": datasets.Value("string"),
            "INCHAMBER_VIRTUAL_HEARING": datasets.Value("string"),
            "JUDGE": datasets.Value("string"),
            "text_case_cover": datasets.Value("string"),
            "DATE_DECISION": datasets.Value("string"),
            "decision_outcome": datasets.ClassLabel(
                names=['Rejected', 'Granted', 'Uncertain']),
            }
        )
        elif self.config.name == "determination_sentences":  
            features = datasets.Features(
            {
            "decisionID": datasets.Value("int64"),
            "extracted_sentences_determination": datasets.Value("string"),
            }
        )
        elif self.config.name == "outcome_classification":  
            features = datasets.Features(
            {
            "decisionID": datasets.Value("float64"),
            "decision_outcome": datasets.ClassLabel(
                names=['Rejected', 'Granted', 'Uncertain']),
            }
        )

        data_files = {
            "train": "outcome_train_test/train_dataset_silver.csv",
            "test": "outcome_train_test/test_dataset_gold.csv",
        }
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features, 
            license=_LICENSE,
            supervised_keys=None,
            homepage=_HOMEPAGE,
        )

    def _split_generators(self, dl_manager):
        # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
        # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name

        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
        # 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.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
        
        urls_to_download = _URLS[self.config.name]
        downloaded_files = dl_manager.download_and_extract(urls_to_download)
        
        if self.config.name == "outcome_classification":
            data_dir = dl_manager.download_and_extract(_URLS["outcome_classification"])
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "train_dataset_silver.csv"),
                        "split": "train",
                    },
                ), 
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "test_dataset_gold.csv"),
                        "split": "test"
                    },
                ),
        ]
        else:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": downloaded_files,
                        "split": "train",
                    },
                )
            ]

    # key value examples
    def _generate_examples(self, filepath, split):
    
        logger.info("⏳ Generating examples from = %s", filepath)
        
        if self.config.name == "raw_documents":
            for idx, filename in enumerate(os.listdir(filepath)):
                if filename.endswith(".txt"):
                    with open(os.path.join(filepath, filename), "r", encoding="utf-8") as f:
                        # Read the content of the text file
                        text_content = f.read()
                        yield idx, {"case_files": text_content}
    
        elif self.config.name == "raw_sentences":
            with open(filepath, "r", encoding="utf-8") as f:
                data = csv.DictReader(f, delimiter = ";")
                for idx, row in enumerate(data):
                    yield idx, {
                        "decisionID": row["decisionID"],
                        "Text": row["Text"],
                    }        
    
        elif self.config.name == "all_legal_entities":
            with open(filepath, "r", encoding="utf-8") as f:
                reader = csv.DictReader(f, delimiter=";")
                for idx, row in enumerate(reader):
                    yield idx, {
                        "decisionID": row["decisionID"],
                        "Text": row["Text"],
                        "GPE": row["GPE"],
                        "DATE": row["DATE"],
                        "NORP": row["NORP"],
                        "ORG": row["ORG"],
                        "LAW": row["LAW"],
                        "CLAIMANT_EVENTS": row["CLAIMANT_EVENTS"],
                        "CREDIBILITY": row["CREDIBILITY"],
                        "DETERMINATION": row["DETERMINATION"],
                        "CLAIMANT_INFO": row["CLAIMANT_INFO"],
                        "PROCEDURE": row["PROCEDURE"],
                        "DOC_EVIDENCE": row["DOC_EVIDENCE"],
                        "EXPLANATION": row["EXPLANATION"],
                        "LEGAL_GROUND": row["LEGAL_GROUND"],
                        "LAW_CASE": row["LAW_CASE"],
                        "LAW_REPORT": row["LAW_REPORT"],
                        "decision_outcome": row["decision_outcome"],
                        "extracted_dates": row["extracted_dates"],
                        "LOC_HEARING": row["LOC_HEARING"],
                        "TRIBUNAL": row["TRIBUNAL"],
                        "PUBLIC_PRIVATE_HEARING": row["PUBLIC_PRIVATE_HEARING"],
                        "INCHAMBER_VIRTUAL_HEARING": row["INCHAMBER_VIRTUAL_HEARING"],
                        "JUDGE": row["JUDGE"],
                        "text_case_cover": row["text_case_cover"],
                        "DATE_DECISION": row["DATE_DECISION"],
                    }
    
        elif self.config.name == "casecover_legal_entities":
            with open(filepath, "r", encoding="utf-8") as f:
                reader = csv.DictReader(f, delimiter=",")
                for idx, row in enumerate(reader):
                    yield idx, {
                        "decision_ID": row["decision_ID"],
                        "extracted_dates": row["extracted_dates"],
                        "extracted_gpe": row["extracted_gpe"],
                        "extracted_org": row["extracted_org"],
                        "public_private_hearing": row["public_private_hearing"],
                        "in_chamber_virtual": row["in_chamber_virtual"],
                        "judge_name": row["judge_name"],
                        "date_decision": row["date_decision"],
                        "text_case_cover": row["text_case_cover"],
                    }
    
        elif self.config.name == "casecover_entities_outcome":
            with open(filepath, "r", encoding="utf-8") as f:
                reader = csv.DictReader(f, delimiter=";")
                for idx, row in enumerate(reader):
                    yield idx, {
                        "decision_ID": row["decision_ID"],
                        "extracted_dates": row["extracted_dates"],
                        "LOC_HEARING": row["LOC_HEARING"],
                        "TRIBUNAL": row["TRIBUNAL"],
                        "PUBLIC_PRIVATE_HEARING": row["PUBLIC_PRIVATE_HEARING"],
                        "INCHAMBER_VIRTUAL_HEARING": row["INCHAMBER_VIRTUAL_HEARING"],
                        "JUDGE": row["JUDGE"],
                        "text_case_cover": row["text_case_cover"],
                        "DATE_DECISION": row["DATE_DECISION"],
                        "decision_outcome": row["decision_outcome"],
                    }
    
        elif self.config.name == "determination_sentences":
            with open(filepath, "r", encoding="utf-8") as f:
                reader = csv.DictReader(f, delimiter=";")
                for idx, line in enumerate(reader):
                    yield idx, {
                        "decisionID": line["decisionID"],
                        "extracted_sentences_determination": line["extracted_sentences_determination"],
                    }
        
        elif self.config.name == "outcome_classification":
            with open(filepath, "r", encoding="utf-8") as f:
                reader = csv.DictReader(f, delimiter=";")
                for idx, row in enumerate(reader):
                    yield idx, {
                        "decisionID": row["decisionID"],
                        "decision_outcome": row["decision_outcome"],
                    }