File size: 10,881 Bytes
41eed00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e55c633
41eed00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75b9028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41eed00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e55c633
41eed00
 
 
e55c633
41eed00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75b9028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41eed00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e55c633
5123818
e55c633
5123818
41eed00
 
 
5123818
41eed00
5123818
41eed00
5123818
75b9028
 
 
536bc16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5123818
 
41eed00
5123818
 
 
e55c633
 
 
 
 
 
 
 
 
5123818
 
 
 
75b9028
 
 
e55c633
5123818
 
41eed00
 
 
 
 
 
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
# 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.
"""LegalGLUE: A Benchmark Dataset for Legal NLP models."""

import csv
import json
import textwrap
import os

import datasets


_DESCRIPTION = """\

Legal General Language Understanding Evaluation (LegalGLUE) benchmark is

a collection of datasets for evaluating model performance across a diverse set of legal NLP tasks

"""

GERMAN_LER = [
    "B-AN",
    "B-EUN",
    "B-GRT",
    "B-GS",
    "B-INN",
    "B-LD",
    "B-LDS",
    "B-LIT",
    "B-MRK",
    "B-ORG",
    "B-PER",
    "B-RR",
    "B-RS",
    "B-ST",
    "B-STR",
    "B-UN",
    "B-VO",
    "B-VS",
    "B-VT",
    "I-AN",
    "I-EUN",
    "I-GRT",
    "I-GS",
    "I-INN",
    "I-LD",
    "I-LDS",
    "I-LIT",
    "I-MRK",
    "I-ORG",
    "I-PER",
    "I-RR",
    "I-RS",
    "I-ST",
    "I-STR",
    "I-UN",
    "I-VO",
    "I-VS",
    "I-VT",
    "O"]

LENER_BR=[
            "O",
            "B-ORGANIZACAO",
            "I-ORGANIZACAO",
            "B-PESSOA",
            "I-PESSOA",
            "B-TEMPO",
            "I-TEMPO",
            "B-LOCAL",
            "I-LOCAL",
            "B-LEGISLACAO",
            "I-LEGISLACAO",
            "B-JURISPRUDENCIA",
            "I-JURISPRUDENCIA",
        ]

class LegalGlueConfig(datasets.BuilderConfig):
    """BuilderConfig for LegalGLUE."""

    def __init__(

        self,

        label_classes,  #the list of classes of the labels

        multi_label,    #boolean, if the task is multi-label

        homepage,       #homepage of the original dataset

        citation,       #citation for the dataset

        data_url,

        data_files,

        **kwargs,

    ):
        super(LegalGlueConfig, self).__init__(version=datasets.Version("1.1.0", ""), **kwargs)
        self.label_classes = label_classes
        self.multi_label = multi_label
        self.homepage = homepage
        self.citation = citation
        self.data_url = data_url
        self.data_files = data_files



class LegalGLUE(datasets.GeneratorBasedBuilder):
    """LegalGLUE: A Benchmark Dataset for Legal Language Understanding"""

    BUILDER_CONFIGS = [
        LegalGlueConfig(
            name="german_ler",
            description=textwrap.dedent(
                """\

            description"""
            ),
            label_classes=GERMAN_LER,
            multi_label=False,
            data_url="https://raw.githubusercontent.com/elenanereiss/Legal-Entity-Recognition/master/data/dataset_courts.zip",
            data_files=["bag.conll", "bfh.conll", "bgh.conll", "bpatg.conll", "bsg.conll", "bverfg.conll", "bverwg.conll"],
            homepage="https://github.com/elenanereiss/Legal-Entity-Recognition",
            citation=textwrap.dedent("""\

                @inproceedings{leitner2019fine,

                author = {Elena Leitner and Georg Rehm and Julian Moreno-Schneider},

                title = {{Fine-grained Named Entity Recognition in Legal Documents}},

                booktitle = {Semantic Systems. The Power of AI and Knowledge

                  Graphs. Proceedings of the 15th International Conference

                  (SEMANTiCS 2019)},

                year = 2019,

                editor = {Maribel Acosta and Philippe Cudré-Mauroux and Maria

                  Maleshkova and Tassilo Pellegrini and Harald Sack and York

                  Sure-Vetter},

                keywords = {aip},

                publisher = {Springer},

                series = {Lecture Notes in Computer Science},

                number = {11702},

                address = {Karlsruhe, Germany},

                month = 9,

                note = {10/11 September 2019},

                pages = {272--287},

                pdf = {https://link.springer.com/content/pdf/10.1007%2F978-3-030-33220-4_20.pdf}}

            """)
        ),
        # LegalGlueConfig(
        #     name="lener_br",
        #     description=textwrap.dedent(
        #     """\
        #     LeNER-Br is a Portuguese language dataset for named entity recognition
        #     applied to legal documents. LeNER-Br consists entirely of manually annotated
        #     legislation and legal cases texts and contains tags for persons, locations,
        #     time entities, organizations, legislation and legal cases.
        #     To compose the dataset, 66 legal documents from several Brazilian Courts were
        #     collected. Courts of superior and state levels were considered, such as Supremo
        #     Tribunal Federal, Superior Tribunal de Justiça, Tribunal de Justiça de Minas
        #     Gerais and Tribunal de Contas da União. In addition, four legislation documents
        #     were collected, such as "Lei Maria da Penha", giving a total of 70 documents
        #     """
        #     ),
        #     label_classes=LENER_BR,
        #     multi_label=False,
        #     data_url="https://github.com/peluz/lener-br/raw/master/leNER-Br/",
        #     data_files=["train/train.conll", "dev/dev.conll", "test/test.conll"],
        #     homepage="https://cic.unb.br/~teodecampos/LeNER-Br/",
        #     citation=textwrap.dedent("""\
        #         @inproceedings{luz_etal_propor2018,
        #         author = {Pedro H. {Luz de Araujo} and Te\'{o}filo E. {de Campos} and
        #         Renato R. R. {de Oliveira} and Matheus Stauffer and
        #         Samuel Couto and Paulo Bermejo},
        #         title = {{LeNER-Br}: a Dataset for Named Entity Recognition in {Brazilian} Legal Text},
        #         booktitle = {International Conference on the Computational Processing of Portuguese ({PROPOR})},
        #         publisher = {Springer},
        #         series = {Lecture Notes on Computer Science ({LNCS})},
        #         pages = {313--323},
        #         year = {2018},
        #         month = {September 24-26},
        #         address = {Canela, RS, Brazil},
        #         doi = {10.1007/978-3-319-99722-3_32},
        #         url = {https://cic.unb.br/~teodecampos/LeNER-Br/},
        #         }
        #     """)
        # )
    ]

    def _info(self):
        if self.config.name == "german_ler":
            features = {
                "id": datasets.Value("string"),
                "tokens": datasets.Sequence(datasets.Value("string")),
                "ner_tags": datasets.Sequence(
                    datasets.features.ClassLabel(
                        names=self.config.label_classes
                    )
                )
            }

        return datasets.DatasetInfo(
            description=self.config.description,
            features=datasets.Features(features),
            homepage=self.config.homepage,
            citation=self.config.citation,
        )

    def _split_generators(self, dl_manager):
        #archive = dl_manager.download(self.config.data_url)
        if self.config.name == "german_ler":
            archive = dl_manager.download_and_extract(self.config.data_url)
            return [datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": self.config.data_files,
                    "split": "train",
                    "files": [os.path.join(archive,file) for file in self.config.data_files]#dl_manager.iter_archive(archive),
                },
            )]
        #elif self.config_name == "lener_br":


        # else:
        #     return [
        #         datasets.SplitGenerator(
        #             name=datasets.Split.TRAIN,
        #             # These kwargs will be passed to _generate_examples
        #             gen_kwargs={
        #                 "filepath": self.config.data_files,
        #                 "split": "train",
        #                 "files": dl_manager.iter_archive(archive),
        #                 },
        #             ),
        #         datasets.SplitGenerator(
        #             name=datasets.Split.TEST,
        #             # These kwargs will be passed to _generate_examples
        #             gen_kwargs={
        #                 "filepath": self.config.data_files,
        #                 "split": "test",
        #                 "files": dl_manager.iter_archive(archive),
        #             },
        #         ),
        #         datasets.SplitGenerator(
        #             name=datasets.Split.VALIDATION,
        #             # These kwargs will be passed to _generate_examples
        #             gen_kwargs={
        #                 "filepath": self.config.data_files,
        #                 "split": "validation",
        #                 "files": dl_manager.iter_archive(archive),
        #             },
        #         ),
        #     ]

    def _generate_examples(self, filepath, split, files):
        if self.config.name == "german_ler":
            texts, labels = [], []
            for file in files:
                #if path in filepath:
                with open(file, encoding="utf-8") as f:
                    tokens = []
                    tags = []
                    for line in f:
                        if line == "" or line == "\n":
                            if tokens:
                                texts.append(tokens)
                                labels.append(tags)
                                tokens = []
                                tags = []
                        else:
                            token, tag = line.split()
                            tokens.append(token)
                            tags.append(tag.rstrip())
                    if tokens:
                        texts.append(tokens)
                        labels.append(tags)

            for i,token in enumerate(texts):
                tokens = texts[i]
                ner_tags = labels[i]
                yield i, {
                        "id": str(i),
                        "tokens": tokens,
                        "ner_tags": ner_tags,
                        }