File size: 6,901 Bytes
fa3669e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2d5b47
 
fa3669e
 
e96a026
 
 
fa3669e
e96a026
 
 
fa3669e
 
1a57806
fa3669e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a57806
fa3669e
 
 
 
 
 
 
 
1a57806
fa3669e
 
 
efc2dac
fa3669e
 
 
1a57806
fa3669e
 
 
1a57806
fa3669e
 
 
 
 
1a57806
fa3669e
 
 
 
 
1a57806
e96a026
fa3669e
e96a026
 
fa3669e
1a57806
fa3669e
 
 
 
 
1a57806
fa3669e
 
 
 
 
1a57806
e96a026
fa3669e
e96a026
 
fa3669e
 
 
 
 
 
71fa4c6
 
fa3669e
 
 
 
 
 
 
 
 
 
 
c6ed634
fa3669e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71fa4c6
fa3669e
71fa4c6
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
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# 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.

# Lint as: python3
"""The SuperGLUE benchmark."""

import json
import os

import datasets

_CITATION = """\
@article{gustafson2006documentation,
  title={Documentation of the Stockholm-Ume{\aa} Corpus},
  author={Gustafson-Capkov{\'a}, Sofia and Hartmann, Britt},
  journal={Stockholm University: Department of Linguistics},
  year={2006}
}
"""

# You can copy an official description
_DESCRIPTION = """\
    The dataset is a conversion of the venerable SUC 3.0 dataset into the
    huggingface ecosystem. The original dataset does not contain an official
    train-dev-test split, which is introduced here; the tag distribution for the
    NER tags between the three splits is mostly the same.
    
    The dataset has three different types of tagsets: manually annotated POS,
    manually annotated NER, and automatically annotated NER. For the
    automatically annotated NER tags, only sentences were chosen, where the
    automatic and manual annotations would match (with their respective
    categories).
    
    Additionally we provide remixes of the same data with some or all sentences
    being lowercased.
"""

_HOMEPAGE = "https://spraakbanken.gu.se/en/resources/suc3"

_LICENSE = "CC-BY-4.0"

# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
# _URL = "https://huggingface.co/datasets/KBLab/suc3_1/resolve/main/data/"
_URL = "https://huggingface.co/datasets/KBLab/sucx3_ner/resolve/main/data/"
_URLS = {
    "original_tags": {
        "cased": "original_tags/cased.tar.gz",
        "lower": "original_tags/lower.tar.gz",
        "lower_mix": "original_tags/lower_mix.tar.gz"},
    "simple_tags": {
        "cased": "simple_tags/cased.tar.gz",
        "lower": "simple_tags/lower.tar.gz",
        "lower_mix": "simple_tags/lower_mix.tar.gz"}
}


_POS_LABEL_NAMES = {
    'AB', 'DT', 'HA', 'HD', 'HP', 'HS', 'IE', 'IN', 'JJ', 'KN', 'MAD', 'MID',
    'NN', 'PAD', 'PC', 'PL', 'PM', 'PN', 'PP', 'PS', 'RG', 'RO', 'SN', 'UO',
    'VB'
}
_NER_LABEL_NAMES_ORIGINAL = {
    'B-animal', 'B-event', 'B-inst', 'B-myth', 'B-other', 'B-person',
    'B-place', 'B-product', 'B-work', 'I-animal', 'I-event', 'I-inst',
    'I-myth', 'I-other', 'I-person', 'I-place', 'I-product', 'I-work', 'O'
}

_NER_LABEL_NAMES_SIMPLE = {
    'B-EVN', 'B-LOC', 'B-MSR', 'B-OBJ', 'B-ORG', 'B-PRS', 'B-TME', 'B-WRK',
    'I-EVN', 'I-LOC', 'I-MSR', 'I-OBJ', 'I-ORG', 'I-PRS', 'I-TME', 'I-WRK', 'O'
}


class SUC3Config(datasets.BuilderConfig):
    """BuilderConfig for Suc."""
    def __init__(self,
                 ner_label_names,
                 description,
                 data_url,
                 **kwargs):
        """BuilderConfig for Suc.
        """
        super(SUC3Config,
              self).__init__(version=datasets.Version("1.0.2"), **kwargs)
        self.ner_label_names = ner_label_names
        self.description = description
        self.data_url = data_url



class SUC3(datasets.GeneratorBasedBuilder):
    """The SuperGLUE benchmark."""

    BUILDER_CONFIGS = [
        SUC3Config(
            name="original_cased",
            ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
            data_url=_URLS["original_tags"]["cased"],
            description="manually annotated & cased",
        ),
        SUC3Config(
            name="original_lower",
            ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
            data_url=_URLS["original_tags"]["lower"],
            description="manually annotated & lower",
        ),
        SUC3Config(
            name="original_lower_mix",
            ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
            data_url=_URLS["original_tags"]["lower_mix"],
            description="manually annotated & lower_mix",
        ),
        SUC3Config(
            name="simple_cased",
            ner_label_names=_NER_LABEL_NAMES_SIMPLE,
            data_url=_URLS["simple_tags"]["cased"],
            description="automatically annotated & cased",
        ),
        SUC3Config(
            name="simple_lower",
            ner_label_names=_NER_LABEL_NAMES_SIMPLE,
            data_url=_URLS["simple_tags"]["lower"],
            description="automatically annotated & lower",
        ),
        SUC3Config(
            name="simple_lower_mix",
            ner_label_names=_NER_LABEL_NAMES_SIMPLE,
            data_url=_URLS["simple_tags"]["lower_mix"],
            description="autimatically annotated & lower_mix",
        ),
    ]

    def _info(self):
        features = {"id": datasets.Value("string"),
                    "tokens": datasets.features.Sequence(datasets.Value("string")),
                    "pos_tags": datasets.features.Sequence(datasets.Value("string")),
                    "ner_tags": datasets.features.Sequence(datasets.Value("string"))}

        return datasets.DatasetInfo(
            description=_DESCRIPTION + self.config.description,
            features=datasets.Features(features),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            supervised_keys=None,
        )

    def _split_generators(self, dl_manager):
        dl_dir = dl_manager.download_and_extract(_URL + self.config.data_url)
        dl_dir = os.path.join(dl_dir, self.config.data_url.split("/")[-1].split(".")[0])
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "train.jsonl"),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "dev.jsonl"),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "test.jsonl"),
                },
            ),
        ]

    def _generate_examples(self, data_file):
        with open(data_file, encoding="utf-8") as f:
            for i, line in enumerate(f):
                row = json.loads(line)
                yield str(i), row