File size: 7,394 Bytes
3cf237e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
"""Fine-grained Named Entity Recognition in Legal Documents"""

from __future__ import absolute_import, division, print_function

import datasets

_CITATION = """\
@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}
}
"""

_DESCRIPTION = """\
We describe a dataset developed for Named Entity Recognition in German federal court decisions. 
It consists of approx. 67,000 sentences with over 2 million tokens. 
The resource contains 54,000 manually annotated entities, mapped to 19 fine-grained semantic classes: 
person, judge, lawyer, country, city, street, landscape, organization, company, institution, court, brand, law, 
ordinance, European legal norm, regulation, contract, court decision, and legal literature. 
The legal documents were, furthermore, automatically annotated with more than 35,000 TimeML-based time expressions. 
The dataset, which is available under a CC-BY 4.0 license in the CoNNL-2002 format, 
was developed for training an NER service for German legal documents in the EU project Lynx.
"""

_URL = "https://raw.githubusercontent.com/elenanereiss/Legal-Entity-Recognition/master/data/ler.conll"


class Ler(datasets.GeneratorBasedBuilder):
    """
    We describe a dataset developed for Named Entity Recognition in German federal court decisions.
    It consists of approx. 67,000 sentences with over 2 million tokens.
    The resource contains 54,000 manually annotated entities, mapped to 19 fine-grained semantic classes:
    person, judge, lawyer, country, city, street, landscape, organization, company, institution, court, brand, law,
    ordinance, European legal norm, regulation, contract, court decision, and legal literature.
    The legal documents were, furthermore, automatically annotated with more than 35,000 TimeML-based time expressions.
    The dataset, which is available under a CC-BY 4.0 license in the CoNNL-2002 format,
    was developed for training an NER service for German legal documents in the EU project Lynx.
    """
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "ner_tags": datasets.Sequence(
                        datasets.ClassLabel(
                            names=[
                                "O",
                                "B-PER",
                                "I-PER",
                                "B-RR",
                                "I-RR",
                                "B-AN",
                                "I-AN",
                                "B-LD",
                                "I-LD",
                                "B-ST",
                                "I-ST",
                                "B-STR",
                                "I-STR",
                                "B-LDS",
                                "I-LDS",
                                "B-ORG",
                                "I-ORG",
                                "B-UN",
                                "I-UN",
                                "B-INN",
                                "I-INN",
                                "B-GRT",
                                "I-GRT",
                                "B-MRK",
                                "I-MRK",
                                "B-GS",
                                "I-GS",
                                "B-VO",
                                "I-VO",
                                "B-EUN",
                                "I-EUN",
                                "B-VS",
                                "I-VS",
                                "B-VT",
                                "I-VT",
                                "B-RS",
                                "I-RS",
                                "B-LIT",
                                "I-LIT",
                            ]
                        )
                    ),
                }
            ),
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=datasets.info.SupervisedKeysData(input="tokens", output="ner_tags"),
            # Homepage of the dataset for documentation
            homepage="https://github.com/elenanereiss/Legal-Entity-Recognition",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # dl_manager is a datasets.download.DownloadManager that can be used to
        # download and extract URLs
        dl_file = dl_manager.download(_URL)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"filepath": dl_file},
            ),
        ]

    def _generate_examples(self, filepath):
        """ Yields examples. """
        with open(filepath, "r", encoding="utf-8") as f:
            guid = 0
            tokens = []
            ner_tags = []
            for line in f:
                if line == "" or line == "\n":
                    if tokens:
                        yield guid, {"id": guid, "tokens": tokens, "ner_tags": ner_tags}
                        guid += 1
                        tokens = []
                        ner_tags = []
                else:
                    # conll2002 tokens are space separated
                    splits = line.split(" ")
                    tokens.append(splits[0])
                    ner_tags.append(splits[1].rstrip())