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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
# Lint as: python3
|
| 17 |
+
"""The SuperGLUE benchmark."""
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
|
| 24 |
+
_CITATION = """\
|
| 25 |
+
@article{gustafson2006documentation,
|
| 26 |
+
title={Documentation of the Stockholm-Ume{\aa} Corpus},
|
| 27 |
+
author={Gustafson-Capkov{\'a}, Sofia and Hartmann, Britt},
|
| 28 |
+
journal={Stockholm University: Department of Linguistics},
|
| 29 |
+
year={2006}
|
| 30 |
+
}
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
# You can copy an official description
|
| 34 |
+
_DESCRIPTION = """\
|
| 35 |
+
The dataset is a conversion of the venerable SUC 3.0 dataset into the
|
| 36 |
+
huggingface ecosystem. The original dataset does not contain an official
|
| 37 |
+
train-dev-test split, which is introduced here; the tag distribution for the
|
| 38 |
+
NER tags between the three splits is mostly the same.
|
| 39 |
+
|
| 40 |
+
The dataset has three different types of tagsets: manually annotated POS,
|
| 41 |
+
manually annotated NER, and automatically annotated NER. For the
|
| 42 |
+
automatically annotated NER tags, only sentences were chosen, where the
|
| 43 |
+
automatic and manual annotations would match (with their respective
|
| 44 |
+
categories).
|
| 45 |
+
|
| 46 |
+
Additionally we provide remixes of the same data with some or all sentences
|
| 47 |
+
being lowercased.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
_HOMEPAGE = "https://spraakbanken.gu.se/en/resources/suc3"
|
| 51 |
+
|
| 52 |
+
_LICENSE = "CC-BY-4.0"
|
| 53 |
+
|
| 54 |
+
# TODO: Add link to the official dataset URLs here
|
| 55 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 56 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 57 |
+
_URL = "https://huggingface.co/datasets/KBLab/suc3.1/resolve/main/data/"
|
| 58 |
+
_URLS = {
|
| 59 |
+
"original_tags": {
|
| 60 |
+
"cased": "original_tags/cased.tar.gz",
|
| 61 |
+
"lower": "original_tags/lower.tar.gz",
|
| 62 |
+
"lower_mix": "original_tags/lower_mixed.tar.gz"},
|
| 63 |
+
"simple_tags": {
|
| 64 |
+
"cased": "simple_tags/cased.tar.gz",
|
| 65 |
+
"lower": "simple_tags/lower.tar.gz",
|
| 66 |
+
"lower_mix": "simple_tags/lower_mixed.tar.gz"}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
_POS_LABEL_NAMES = {
|
| 70 |
+
'AB', 'DT', 'HA', 'HD', 'HP', 'HS', 'IE', 'IN', 'JJ', 'KN', 'MAD', 'MID',
|
| 71 |
+
'NN', 'PAD', 'PC', 'PL', 'PM', 'PN', 'PP', 'PS', 'RG', 'RO', 'SN', 'UO',
|
| 72 |
+
'VB'
|
| 73 |
+
}
|
| 74 |
+
_NER_LABEL_NAMES_ORIGINAL = {
|
| 75 |
+
'B-animal', 'B-event', 'B-inst', 'B-myth', 'B-other', 'B-person',
|
| 76 |
+
'B-place', 'B-product', 'B-work', 'I-animal', 'I-event', 'I-inst',
|
| 77 |
+
'I-myth', 'I-other', 'I-person', 'I-place', 'I-product', 'I-work', 'O'
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
_NER_LABEL_NAMES_SIMPLE = {
|
| 81 |
+
'B-EVN', 'B-LOC', 'B-MSR', 'B-OBJ', 'B-ORG', 'B-PRS', 'B-TME', 'B-WRK',
|
| 82 |
+
'I-EVN', 'I-LOC', 'I-MSR', 'I-OBJ', 'I-ORG', 'I-PRS', 'I-TME', 'I-WRK', 'O'
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class SucConfig(datasets.BuilderConfig):
|
| 87 |
+
"""BuilderConfig for Suc."""
|
| 88 |
+
def __init__(self,
|
| 89 |
+
ner_label_names,
|
| 90 |
+
description,
|
| 91 |
+
data_url,
|
| 92 |
+
**kwargs):
|
| 93 |
+
"""BuilderConfig for Suc.
|
| 94 |
+
"""
|
| 95 |
+
# Version history:
|
| 96 |
+
# 1.0.2: Fixed non-nondeterminism in ReCoRD.
|
| 97 |
+
# 1.0.1: Change from the pre-release trial version of SuperGLUE (v1.9) to
|
| 98 |
+
# the full release (v2.0).
|
| 99 |
+
# 1.0.0: S3 (new shuffling, sharding and slicing mechanism).
|
| 100 |
+
# 0.0.2: Initial version.
|
| 101 |
+
super(SucConfig,
|
| 102 |
+
self).__init__(version=datasets.Version("1.0.2"), **kwargs)
|
| 103 |
+
self.ner_label_names = ner_label_names
|
| 104 |
+
self.description = description
|
| 105 |
+
self.config.data_url = data_url
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class Suc(datasets.GeneratorBasedBuilder):
|
| 110 |
+
"""The SuperGLUE benchmark."""
|
| 111 |
+
|
| 112 |
+
BUILDER_CONFIGS = [
|
| 113 |
+
SucConfig(
|
| 114 |
+
name="original_cased",
|
| 115 |
+
ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
|
| 116 |
+
data_url=_URLS["original_tags"]["cased"],
|
| 117 |
+
description="manually annotated & cased",
|
| 118 |
+
),
|
| 119 |
+
SucConfig(
|
| 120 |
+
name="original_lower",
|
| 121 |
+
ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
|
| 122 |
+
data_url=_URLS["original_tags"]["lower"],
|
| 123 |
+
description="manually annotated & lower",
|
| 124 |
+
),
|
| 125 |
+
SucConfig(
|
| 126 |
+
name="original_lower_mixed",
|
| 127 |
+
ner_label_names=_NER_LABEL_NAMES_ORIGINAL,
|
| 128 |
+
data_url=_URLS["original_tags"]["lower_mixed"],
|
| 129 |
+
description="manually annotated & lower_mixed",
|
| 130 |
+
),
|
| 131 |
+
SucConfig(
|
| 132 |
+
name="simple_cased",
|
| 133 |
+
ner_label_names=_NER_LABEL_NAMES_SIMPLE,
|
| 134 |
+
data_url=_URLS["simple_tags"]["cased"],
|
| 135 |
+
description="automatically annotated & cased",
|
| 136 |
+
),
|
| 137 |
+
SucConfig(
|
| 138 |
+
name="simple_lower",
|
| 139 |
+
ner_label_names=_NER_LABEL_NAMES_SIMPLE,
|
| 140 |
+
data_url=_URLS["simple_tags"]["lower"],
|
| 141 |
+
description="automatically annotated & lower",
|
| 142 |
+
),
|
| 143 |
+
SucConfig(
|
| 144 |
+
name="simple_lower_mixed",
|
| 145 |
+
ner_label_names=_NER_LABEL_NAMES_SIMPLE,
|
| 146 |
+
data_url=_URLS["simple_tags"]["lower_mixed"],
|
| 147 |
+
description="autimatically annotated & lower_mixed",
|
| 148 |
+
),
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
def _info(self):
|
| 152 |
+
features = {"id": datasets.Value("string"),
|
| 153 |
+
"tokens": datasets.features.Sequence(datasets.Value("string")),
|
| 154 |
+
"pos_tags": datasets.features.Sequence(datasets.features.ClassLabel(_POS_LABEL_NAMES)),
|
| 155 |
+
"ner_tags": datasets.features.Sequence(datasets.features.ClassLabel(self.config.ner_label_names))}
|
| 156 |
+
|
| 157 |
+
return datasets.DatasetInfo(
|
| 158 |
+
description=_DESCRIPTION + self.config.description,
|
| 159 |
+
features=datasets.Features(features),
|
| 160 |
+
homepage=_HOMEPAGE,
|
| 161 |
+
citation=_CITATION,
|
| 162 |
+
supervised_keys=None,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def _split_generators(self, dl_manager):
|
| 166 |
+
dl_dir = dl_manager.download_and_extract(_URL + self.config.data_url)
|
| 167 |
+
dl_dir = os.path.join(dl_dir)
|
| 168 |
+
return [
|
| 169 |
+
datasets.SplitGenerator(
|
| 170 |
+
name=datasets.Split.TRAIN,
|
| 171 |
+
gen_kwargs={
|
| 172 |
+
"data_file": os.path.join(dl_dir, "train.jsonl"),
|
| 173 |
+
},
|
| 174 |
+
),
|
| 175 |
+
datasets.SplitGenerator(
|
| 176 |
+
name=datasets.Split.VALIDATION,
|
| 177 |
+
gen_kwargs={
|
| 178 |
+
"data_file": os.path.join(dl_dir, "dev.jsonl"),
|
| 179 |
+
},
|
| 180 |
+
),
|
| 181 |
+
datasets.SplitGenerator(
|
| 182 |
+
name=datasets.Split.TEST,
|
| 183 |
+
gen_kwargs={
|
| 184 |
+
"data_file": os.path.join(dl_dir, "test.jsonl"),
|
| 185 |
+
},
|
| 186 |
+
),
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
def _generate_examples(self, data_file):
|
| 190 |
+
with open(data_file, encoding="utf-8") as f:
|
| 191 |
+
for line in f:
|
| 192 |
+
row = json.loads(line)
|
| 193 |
+
yield row
|