Datasets:
rcds
/

occlusion_swiss_judgment_prediction / occlusion_swiss_judgment_prediction.py
ninabaum's picture
Update occlusion_swiss_judgment_prediction.py
34a8ac7
raw
history blame
9.07 kB
# 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.
"""Dataset for the Occlusion task."""
import json
import lzma
import os
import datasets
try:
import lzma as xz
except ImportError:
import pylzma as xz
# TODO: Add final BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@misc{baumgartner_nina_occlusion_2022,
title = {From Occlusion to Transparancy – An Occlusion-Based Explainability Approach for Legal Judgment Prediction in Switzerland},
shorttitle = {From Occlusion to Transparancy},
abstract = {Natural Language Processing ({NLP}) models have been used for more and more complex tasks such as Legal Judgment Prediction ({LJP}). A {LJP} model predicts the outcome of a legal case by utilizing its facts. This increasing deployment of Artificial Intelligence ({AI}) in high-stakes domains such as law and the involvement of sensitive data has increased the need for understanding such systems. We propose a multilingual occlusion-based explainability approach for {LJP} in Switzerland and conduct a study on the bias using Lower Court Insertion ({LCI}). We evaluate our results using different explainability metrics introduced in this thesis and by comparing them to high-quality Legal Expert Annotations using Inter Annotator Agreement. Our findings show that the model has a varying understanding of the semantic meaning and context of the facts section, and struggles to distinguish between legally relevant and irrelevant sentences. We also found that the insertion of a different lower court can have an effect on the prediction, but observed no distinct effects based on legal areas, cantons, or regions. However, we did identify a language disparity with Italian performing worse than the other languages due to representation inequality in the training data, which could lead to potential biases in the prediction in multilingual regions of Switzerland. Our results highlight the challenges and limitations of using {NLP} in the judicial field and the importance of addressing concerns about fairness, transparency, and potential bias in the development and use of {NLP} systems. The use of explainable artificial intelligence ({XAI}) techniques, such as occlusion and {LCI}, can help provide insight into the decision-making processes of {NLP} systems and identify areas for improvement. Finally, we identify areas for future research and development in this field in order to address the remaining limitations and challenges.},
author = {{Baumgartner, Nina}},
year = {2022},
langid = {english}
}
"""
# You can copy an official description
_DESCRIPTION = """\
This dataset contains an implementation of occlusion for the SwissJudgmentPrediction task.
"""
_LICENSE = "cc-by-sa-4.0"
_URLS = {
"full": "https://huggingface.co/datasets/rcds/occlusion_swiss_judgment_prediction"
}
class OcclusionSwissJudgmentPrediction(datasets.GeneratorBasedBuilder):
"""This dataset contains court decision for the occlusion task"""
VERSION = datasets.Version("1.1.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="full", description="This part covers the whole dataset"),
]
DEFAULT_CONFIG_NAME = "full" # It's not mandatory to have a default configuration. Just use one if it make sense.
def _info(self):
if self.config.name == "full": # This is the name of the configuration selected in BUILDER_CONFIGS above
features = datasets.Features(
{
"id": datasets.Value("int32"),
"year": datasets.Value("int32"),
"label": datasets.Value("string"),
"language": datasets.Value("string"),
"region": datasets.Value("string"),
"canton": datasets.Value("string"),
"legal_area": datasets.Value("string"),
"explainability_label": datasets.Value("string"),
"occluded_text": datasets.Value("string"),
"text": datasets.Value("string")
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
# supervised_keys=("sentence", "label"),
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# 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 = _URLS[self.config.name]
filepath_test_1 = dl_manager.download(os.path.join(urls, "data/test_occ_1.jsonl.xz"))
filepath_test_2 = dl_manager.download(os.path.join(urls, "data/test_occ_1.jsonl.xz"))
filepath_test_3 = dl_manager.download(os.path.join(urls, "data/test_occ_1.jsonl.xz"))
filepath_test_4 = dl_manager.download(os.path.join(urls, "data/test_occ_1.jsonl.xz"))
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": filepath_test_1,
"split": "test_1"
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": filepath_test_2,
"split": "test_2"
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": filepath_test_3,
"split": "test_3"
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": filepath_test_4,
"split": "test_4"
},
)
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, filepath, split):
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
line_counter = 0
try:
with xz.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
for id, line in enumerate(f):
line_counter += 1
if line:
data = json.loads(line)
if self.config.name == "full":
yield id, {
"id": data["id"],
"year": data["year"],
"label": data["label"],
"language": data["language"],
"region": data["region"],
"canton": data["canton"],
"legal_area": data["legal_area"],
"explainability_label": data["explainability_label"],
"occluded_text": data["occluded_text"],
"text": data["text"]
}
except lzma.LZMAError as e:
print(split, e)
if line_counter == 0:
raise e