File size: 6,652 Bytes
727ca82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c78f938
727ca82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

"""LexFiles: English Multinational Legal Corpora"""

import datasets
import json
import os

logger = datasets.logging.get_logger(__name__)

_DESCRIPTION = """LexFiles: English Multinational Legal Corpora"""

_HOMEPAGE = "https://huggingface.co/lexlms"

_VERSION = "1.0.0"


class LexFilesConfig(datasets.BuilderConfig):
    """BuilderConfig for LexFiles Corpora"""
    def __init__(self, url, **kwargs):
        super(LexFilesConfig, self).__init__(**kwargs)
        self.url = url


class LexFiles(datasets.GeneratorBasedBuilder):
    """
    The LexFiles
    """

    VERSION = datasets.Version(_VERSION)
    BUILDER_CONFIG_CLASS = LexFilesConfig
    BUILDER_CONFIGS = [
        LexFilesConfig(
            name="eu-legislation",
            description="EU Legislation",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/eurlex.zip'
        ),
        LexFilesConfig(
            name="eu-court-cases",
            description="EU Court cases",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/eurlex.zip'
        ),
        LexFilesConfig(
            name="legal-c4",
            description="Legal webpages extracted from C4",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles4/resolve/main/legal_c4.zip'
        ),
        LexFilesConfig(
            name="ecthr-cases",
            description="ECtHR cases",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/ecthr_cases.zip'
        ),
        LexFilesConfig(
            name="uk-legislation",
            description="UK Legislation",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/uk_legislation.zip'
        ),
        LexFilesConfig(
            name="uk-court-cases",
            description="UK Court cases",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/uk_courts_cases.zip'
        ),
        LexFilesConfig(
            name="indian-court-cases",
            description="Indian Court cases",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/indian_courts_cases.zip'
        ),
        LexFilesConfig(
            name="us-contracts",
            description="US Contracts",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/us_contracts.zip'
        ),
        LexFilesConfig(
            name="us-court-cases",
            description="US Court cases",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/courtlistener.zip'
        ),
        LexFilesConfig(
            name="us-legislation",
            description="US Legislation",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/us_legislation.zip'
        ),
        LexFilesConfig(
            name="canadian-legislation",
            description="Canadian Legislation",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/canadian_legislation.zip'
        ),
        LexFilesConfig(
            name="canadian-court-cases",
            description="Canadian Legislation",
            version=datasets.Version(_VERSION, ""),
            url='https://huggingface.co/datasets/lexlms/lexfiles/resolve/main/canadian_court_cases.zip'
        ),
    ]

    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):
        data_dir = dl_manager.download_and_extract(self.config.url)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": os.path.join(data_dir, "train.jsonl"), "split": 'train'},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": os.path.join(data_dir, "validation.jsonl"), "split": 'validation'},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": os.path.join(data_dir, "test.jsonl"), "split": 'test'},
            ),
        ]

    def _generate_examples(self, filepath, split):
        """
        Reads line by line samples and generates examples.
        :param filepath: Path to jsonl files with line by line examples.
        """
        logger.info("⏳ Generating examples from = %s", filepath)
        if self.config.name in ['eu-court-cases', 'eu-legislation']:
            with open(filepath, encoding="utf-8") as f:
                for idx, row in enumerate(f):
                    data = json.loads(row)
                    if (self.config.name == 'eu-court-cases' and data['sector'] == "6") or \
                            (self.config.name == 'eu-legislation' and data['sector'] != "6"):
                        yield data["id"], {
                            "text": data["text"]
                        }
        else:
            with open(filepath, encoding="utf-8") as f:
                for idx, row in enumerate(f):
                    data = json.loads(row)
                    yield f'{self.config.name}-{split}-{idx}', {
                        "text": data["text"]
                    }