File size: 4,912 Bytes
baa1d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9a6f01
 
baa1d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f095b0
baa1d7c
 
 
 
4f095b0
baa1d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9a6f01
baa1d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52cc20b
 
 
 
 
 
 
 
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
# coding=utf-8
# 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.

"""The Legal Pile (Preprocessed)"""


import datasets
import json

try:
    import lzma as xz
except ImportError:
    import pylzma as xz

logger = datasets.logging.get_logger(__name__)

_DESCRIPTION = """The Legal Pile (Preprocessed)"""

_HOMEPAGE = "https://iliaschalkidis.github.io/"

_VERSION = "1.0.0"


class LegalPileConfig(datasets.BuilderConfig):
    """BuilderConfig for Legal Pile Corpora"""
    def __init__(self, name, n_files, **kwargs):
        super(LegalPileConfig, self).__init__(**kwargs)
        self.name = name
        self.filepath = f'https://huggingface.co/datasets/lexlms/the_legal_pile_preprocessed/' \
                        f'resolve/main/data_files/{self.name}.jsonl.tar.xz'
        self.n_files = n_files


class LegalPile(datasets.GeneratorBasedBuilder):
    """
    The Legal Pile
    """

    VERSION = datasets.Version(_VERSION)
    BUILDER_CONFIG_CLASS = LegalPileConfig
    BUILDER_CONFIGS = [
        LegalPileConfig(
            name=f"eu-legislation",
            n_files=1,
            description="EU Legislation",
        ),
        LegalPileConfig(
            name=f"eu-court-cases",
            n_files=1,
            description="EU Court cases",
        ),
        LegalPileConfig(
            name=f"ecthr-cases",
            n_files=1,
            description="ECtHR cases",
        ),
        LegalPileConfig(
            name=f"uk-legislation",
            n_files=1,
            description="UK Legislation",
        ),
        LegalPileConfig(
            name=f"uk-court-cases",
            n_files=1,
            description="UK Court cases",
        ),
        LegalPileConfig(
            name=f"indian-court-cases",
            n_files=1,
            description="Indian Court cases",
        ),
        LegalPileConfig(
            name=f"canadian-legislation",
            n_files=1,
            description="Canadian Legislation",
        ),
        LegalPileConfig(
            name=f"canadian-court-cases",
            n_files=1,
            description="Canadian Legislation",
        ),
        LegalPileConfig(
            name=f"us-legislation",
            n_files=1,
            description="US Legislation",
        ),
        LegalPileConfig(
            name=f"us-contracts",
            n_files=7,
            description="US Contracts",
        ),
        LegalPileConfig(
            name=f"us-court-cases",
            n_files=16,
            description="US Court cases",
        )
    ]

    def _info(self):
        features = datasets.Features(
            {
                "text": datasets.Value("string")
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
        )

    def _split_generators(self, dl_manager):
        train_data_dir = dl_manager.download([self.config.filepath.replace('.jsonl.tar.xz', f'_train_{idx+1}.jsonl.tar.xz')
                                              for idx in range(self.config.n_files)])
        # test_data_dir = dl_manager.download([self.config.filepath.replace('.jsonl.xz', 'test.jsonl.xz')])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepaths": train_data_dir, "split": 'train'},
            ),
            # datasets.SplitGenerator(
            #     name=datasets.Split.TEST,
            #     gen_kwargs={"filepaths": test_data_dir, "split": 'test'},
            # )
        ]

    def _generate_examples(self, filepaths, split):
        """
        Reads line by line samples and generates examples.
        :param filepath: Path to jsonl files with line by line examples.
        """
        id_ = 0
        for filepath in filepaths:
            logger.info("⏳ Generating examples from = %s", filepath)
            with xz.open(open(filepath, "rb"), "rt", encoding='utf-8') as f:
                for row in f:
                    try:
                         data = json.loads(row)
                         yield id_, {
                             "text": data["text"]
                         }
                         id_ += 1
                    except:
                         continue