File size: 16,377 Bytes
15680f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


'''Ulysses-Ner dataset'''

import datasets
import os

from pathlib import Path

# Também dá para criar uma citação para o C corpus
_CITATION_PL_CORPUS = """

@InProceedings{10.1007\/978-3-030-98305-5_1,

author="Albuquerque, Hidelberg O.

and Costa, Rosimeire

and Silvestre, Gabriel

and Souza, Ellen

and da Silva, N{\\'a}dia F. F.

and Vit{\\'o}rio, Douglas

and Moriyama, Gyovana

and Martins, Lucas

and Soezima, Luiza

and Nunes, Augusto

and Siqueira, Felipe

and Tarrega, Jo{\~a}o P.

and Beinotti, Joao V.

and Dias, Marcio

and Silva, Matheus

and Gardini, Miguel

and Silva, Vinicius

and de Carvalho, Andr{\\'e} C. P. L. F.

and Oliveira, Adriano L. I.",

editor="Pinheiro, Vl{\\'a}dia

and Gamallo, Pablo

and Amaro, Raquel

and Scarton, Carolina

and Batista, Fernando

and Silva, Diego

and Magro, Catarina

and Pinto, Hugo",

title="UlyssesNER-Br: A Corpus of Brazilian Legislative Documents for Named Entity Recognition",

booktitle="Computational Processing of the Portuguese Language",

year="2022",

publisher="Springer International Publishing",

address="Cham",

pages="3--14",

abstract="The amount of legislative documents produced within the past decade has risen dramatically, making it difficult for law practitioners to consult and update legislation. Named Entity Recognition (NER) systems have the untapped potential to extract information from legal documents, which can improve information retrieval and decision-making processes. We introduce the UlyssesNER-Br, a corpus of Brazilian Legislative Documents for NER with quality baselines. The presented corpus consists of bills and legislative consultations from Brazilian Chamber of Deputies. We implemented Conditional Random Field (CRF) and Hidden Markov Model (HMM) models, and the promising F1-score of 80.8{\%} in the analysis by categories and 81.04{\%} in the analysis by types, was achieved with the CRF model. The entities with the best average F1-score results were ``FUNDlei'' and ``DATA'', and the ones with the worst results were ``EVENTO'' and ``PESSOAgrupoind''. The corpus was also evaluated using a BiLSTM-CRF and Glove architectures provided by the pioneering state-of-the-art paper, achieving F1-score of 76.89{\%} in the analysis by categories and 59.67{\%} in the analysis by types.",

isbn="978-3-030-98305-5"

}

"""

_DESCRIPTION = """

The amount of legislative documents produced within the past decade has risen dramatically, making it difficult for law practitioners to consult and update legislation.

Named Entity Recognition (NER) systems have the untapped potential to extract information from legal documents, which can improve information retrieval and decision-making processes.

We introduce the UlyssesNER-Br, a corpus of Brazilian Legislative Documents for NER with quality baselines.

The presented corpus consists of bills and legislative consultations from Brazilian Chamber of Deputies.

We implemented Conditional Random Field (CRF) and Hidden Markov Model (HMM) models, and the promising F1-score of 80.8% in the analysis by categories and 81.04 in the analysis by types, was achieved with the CRF model. The entities with the best average F1-score results were “FUNDlei” and “DATA”, and the ones with the worst results were “EVENTO” and “PESSOAgrupoind”. The corpus was also evaluated using a BiLSTM-CRF and Glove architectures provided by the pioneering state-of-the-art paper, achieving F1-score of 76.89% in the analysis by categories and 59.67% in the analysis by types.



Keywords: Annotation Schema · Named Entity Recognition · Legal Information Retrieval.

"""

_URL = 'https://github.com/ulysses-camara/ulysses-ner-br'
_HOMEPAGE = 'https://github.com/ulysses-camara'
_LICENSE = ''

_URLS = {
    'pl_corpus_categorias' : {
        'train' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_categorias/train.txt',
        'valid' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_categorias/valid.txt',
        'test'  : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_categorias/test.txt',
    },
    'pl_corpus_tipos' : {
        'train' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_tipos/train.txt',
        'valid' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_tipos/valid.txt',
        'test'  : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/PL_corpus_conll/pl_corpus_tipos/test.txt',
    },
    'c_corpus_categorias' : {
        'train' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_categorias/train.txt',
        'valid' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_categorias/valid.txt',
        'test'  : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_categorias/test.txt',
    },
    'c_corpus_tipos' : {
        'train' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_tipos/train.txt',
        'valid' : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_tipos/valid.txt',
        'test'  : 'https://raw.githubusercontent.com/ulysses-camara/ulysses-ner-br/main/annotated-corpora/C_corpus_conll/c_corpus_tipos/test.txt',
    },
}

# https://github.com/huggingface/datasets/blob/main/templates/new_dataset_script.py
# https://huggingface.co/docs/datasets/v3.0.0/dataset_script
# https://huggingface.co/docs/datasets/v3.0.0/share
# https://huggingface.co/docs/datasets/v3.0.0/repository_structure
# https://huggingface.co/docs/hub/repositories-pull-requests-discussions#pull-requests-advanced-usage
# https://github.com/ulysses-camara/ulysses-ner-br/tree/main/annotated-corpora
# pl_corpus_categorias
# pl_corpus_tipos
# c_corpus_categorias
# c_corpus_tipos


class UlyssesNerBrConfig(datasets.BuilderConfig):
    '''Builder Config for UlyssesNER-Br'''

    def __init__(self, features, data_url, citation, url, version, label_classes=("False", "True"), **kwargs):
        """BuilderConfig for UlyssesNER-Br.



        Args:

        features: *list[string]*, list of the features that will appear in the

            feature dict. Should not include "label".

        data_url: *string*, url to download the zip file from.

        citation: *string*, citation for the data set.

        url: *string*, url for information about the data set.

        label_classes: *list[string]*, the list of classes for the label if the

            label is present as a string. Non-string labels will be cast to either

            'False' or 'True'.

        **kwargs: keyword arguments forwarded to super.

        """

        # Version history:
        # 1.0.2: Fixed non-nondeterminism in ReCoRD.
        # 1.0.1: Change from the pre-release trial version of SuperGLUE (v1.9) to
        #        the full release (v2.0).
        # 1.0.0: S3 (new shuffling, sharding and slicing mechanism).
        # 0.0.2: Initial version.
        super().__init__(version=version, **kwargs)
        self.features = features
        self.label_classes = label_classes
        self.data_url = data_url
        self.citation = citation
        self.url = url


class UlyssesNerBr(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    # BUILDER_CONFIG_CLASS = UlyssesNerBrConfig
    BUILDER_CONFIG_CLASS = datasets.BuilderConfig

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name='pl_corpus_categorias',
            version=VERSION,
            description=''
        ),
        datasets.BuilderConfig(
            name='pl_corpus_tipos',
            version=VERSION,
            description=''
        ),
        datasets.BuilderConfig(
            name='c_corpus_categorias',
            version=VERSION,
            description=''
        ),
        datasets.BuilderConfig(
            name='c_corpus_tipos',
            version=VERSION,
            description=''
        )
    ]

    DEFAULT_CONFIG_NAME = 'pl_corpus_categorias'

    def _info(self):

        if self.config.name == 'pl_corpus_categorias' \
            or self.config.name == 'c_corpus_categorias' :
            features = datasets.Features(
                {
                    'id' : datasets.Value('string'),
                    'tokens' : datasets.Sequence(datasets.Value('string')),
                    'ner_tags' : datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                'O',
                                'B-DATA',
                                'I-DATA',
                                'B-PESSOA',
                                'I-PESSOA',
                                'B-ORGANIZACAO',
                                'I-ORGANIZACAO',
                                'B-FUNDAMENTO',
                                'I-FUNDAMENTO',
                                'B-LOCAL',
                                'I-LOCAL',
                                'B-PRODUTODELEI',
                                'I-PRODUTODELEI',
                                'B-EVENTO',
                                'I-EVENTO',
                            ]
                        )
                    ),
                }
            )
        elif self.config.name == 'pl_corpus_tipos':
            features = datasets.Features(
                {
                    'id' : datasets.Value('string'),
                    'tokens' : datasets.Sequence(datasets.Value('string')),
                    'ner_tags' : datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                'O',
                                'B-DATA',
                                'I-DATA',
                                'B-PESSOAgrupocargo',
                                'I-PESSOAgrupocargo',
                                'B-PESSOAindividual',
                                'I-PESSOAindividual',
                                'B-PESSOAcargo',
                                'I-PESSOAcargo',
                                'B-ORGgovernamental',
                                'I-ORGgovernamental',
                                'B-ORGnaogovernamental',
                                'I-ORGnaogovernamental',
                                'B-ORGpartido',
                                'I-ORGpartido',
                                'B-FUNDlei',
                                'I-FUNDlei',
                                'B-FUNDprojetodelei',
                                'I-FUNDprojetodelei',
                                'B-FUNDapelido',
                                'I-FUNDapelido',
                                'B-LOCALconcreto',
                                'I-LOCALconcreto',
                                'B-LOCALvirtual',
                                'I-LOCALvirtual',
                                'B-PRODUTOprograma',
                                'I-PRODUTOprograma',
                                'B-PRODUTOsistema',
                                'I-PRODUTOsistema',
                                'B-PRODUTOoutros',
                                'I-PRODUTOoutros',
                                'B-EVENTO',
                                'I-EVENTO',
                            ]
                        )
                    ),
                }
            )
        elif self.config.name == 'c_corpus_tipos' :
            features = datasets.Features(
                {
                    'id' : datasets.Value('string'),
                    'tokens' : datasets.Sequence(datasets.Value('string')),
                    'ner_tags' : datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                'O',
                                'B-DATA',
                                'I-DATA',
                                'B-EVENTO',
                                'I-EVENTO',
                                'B-FUNDapelido',
                                'I-FUNDapelido',
                                'B-FUNDlei',
                                'I-FUNDlei',
                                'B-FUNDprojetodelei',
                                'I-FUNDprojetodelei',
                                'B-LOCALconcreto',
                                'I-LOCALconcreto',
                                'B-LOCALvirtual',
                                'I-LOCALvirtual',
                                'B-ORGgovernamental',
                                'I-ORGgovernamental',
                                'B-ORGnaogovernamental',
                                'I-ORGnaogovernamental',
                                'B-ORGpartido',
                                'I-ORGpartido',
                                'B-PESSOAcargo',
                                'I-PESSOAcargo',
                                'B-PESSOAgrupocargo',
                                'I-PESSOAgrupocargo',
                                'B-PESSOAgrupoind',
                                'I-PESSOAgrupoind',
                                'B-PESSOAindividual',
                                'I-PESSOAindividual',
                                'B-PRODUTOoutros',
                                'I-PRODUTOoutros',
                                'B-PRODUTOprograma',
                                'I-PRODUTOprograma',
                                'B-PRODUTOsistema',
                                'I-PRODUTOsistema',
                            ]
                        )
                    ),
                }
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION_PL_CORPUS
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager | datasets.StreamingDownloadManager):
        '''

        '''
        urls = _URLS[self.config.name]

        data_fir = dl_manager.download_and_extract(urls)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    'filepath' : data_fir['train'],
                    'split' : 'train',
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    'filepath' : data_fir['valid'],
                    'split' : 'validation',
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    'filepath' : data_fir['test'],
                    'split' : 'test',
                }
            ),
        ]

    def _generate_examples(self, filepath, split):

        guid = 0
        sentence = list()
        label = list()
        with open(filepath, encoding='utf-8') as file :

            for line in file:
                splited = line.strip()
                if line == '' or line == '\n':
                    if sentence:
                        yield guid, {
                            'id' : str(guid),
                            'tokens' : sentence,
                            'ner_tags' : label,
                        }
                        guid += 1
                        sentence = list()
                        label = list()
                else:
                    splited = line.split(' ')
                    sentence.append(splited[0])
                    label.append(splited[1])