ulysses-ner-br / ulysses_ner_br.py
giliardgodoi's picture
Upload ulysses_ner_br.py
15680f8 verified
raw
history blame
16.4 kB
'''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])