Datasets:
Tasks:
Token Classification
Modalities:
Text
Formats:
parquet
Sub-tasks:
named-entity-recognition
Languages:
English
Size:
100K - 1M
Tags:
structure-prediction
License:
Delete loading script
Browse files- few-nerd.py +0 -319
few-nerd.py
DELETED
|
@@ -1,319 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import datasets
|
| 4 |
-
from tqdm.autonotebook import tqdm
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
_CITATION = """
|
| 8 |
-
@inproceedings{ding2021few,
|
| 9 |
-
title={Few-NERD: A Few-Shot Named Entity Recognition Dataset},
|
| 10 |
-
author={Ding, Ning and Xu, Guangwei and Chen, Yulin, and Wang, Xiaobin and Han, Xu and Xie,
|
| 11 |
-
Pengjun and Zheng, Hai-Tao and Liu, Zhiyuan},
|
| 12 |
-
booktitle={ACL-IJCNLP},
|
| 13 |
-
year={2021}
|
| 14 |
-
}
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
_DESCRIPTION = """
|
| 18 |
-
Few-NERD is a large-scale, fine-grained manually annotated named entity recognition dataset,
|
| 19 |
-
which contains 8 coarse-grained types, 66 fine-grained types, 188,200 sentences, 491,711 entities
|
| 20 |
-
and 4,601,223 tokens. Three benchmark tasks are built, one is supervised: Few-NERD (SUP) and the
|
| 21 |
-
other two are few-shot: Few-NERD (INTRA) and Few-NERD (INTER).
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
_LICENSE = "CC BY-SA 4.0"
|
| 25 |
-
|
| 26 |
-
# the original data files (zip of .txt) can be downloaded from tsinghua cloud, but we chose to host them on huggingface.co
|
| 27 |
-
# for better reliability and download speed
|
| 28 |
-
_URL = "https://huggingface.co/datasets/DFKI-SLT/few-nerd/resolve/main/data"
|
| 29 |
-
_URLs = {
|
| 30 |
-
"supervised": f"{_URL}/supervised.zip",
|
| 31 |
-
"intra": f"{_URL}/intra.zip",
|
| 32 |
-
"inter": f"{_URL}/inter.zip"
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
# the label ids, for coarse(NER_TAGS_DICT) and fine(FINE_NER_TAGS_DICT)
|
| 36 |
-
NER_TAGS_DICT = {
|
| 37 |
-
"O": 0,
|
| 38 |
-
"art": 1,
|
| 39 |
-
"building": 2,
|
| 40 |
-
"event": 3,
|
| 41 |
-
"location": 4,
|
| 42 |
-
"organization": 5,
|
| 43 |
-
"other": 6,
|
| 44 |
-
"person": 7,
|
| 45 |
-
"product": 8,
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
FINE_NER_TAGS_DICT = {
|
| 49 |
-
"O": 0,
|
| 50 |
-
"art-broadcastprogram": 1,
|
| 51 |
-
"art-film": 2,
|
| 52 |
-
"art-music": 3,
|
| 53 |
-
"art-other": 4,
|
| 54 |
-
"art-painting": 5,
|
| 55 |
-
"art-writtenart": 6,
|
| 56 |
-
"building-airport": 7,
|
| 57 |
-
"building-hospital": 8,
|
| 58 |
-
"building-hotel": 9,
|
| 59 |
-
"building-library": 10,
|
| 60 |
-
"building-other": 11,
|
| 61 |
-
"building-restaurant": 12,
|
| 62 |
-
"building-sportsfacility": 13,
|
| 63 |
-
"building-theater": 14,
|
| 64 |
-
"event-attack/battle/war/militaryconflict": 15,
|
| 65 |
-
"event-disaster": 16,
|
| 66 |
-
"event-election": 17,
|
| 67 |
-
"event-other": 18,
|
| 68 |
-
"event-protest": 19,
|
| 69 |
-
"event-sportsevent": 20,
|
| 70 |
-
"location-GPE": 21,
|
| 71 |
-
"location-bodiesofwater": 22,
|
| 72 |
-
"location-island": 23,
|
| 73 |
-
"location-mountain": 24,
|
| 74 |
-
"location-other": 25,
|
| 75 |
-
"location-park": 26,
|
| 76 |
-
"location-road/railway/highway/transit": 27,
|
| 77 |
-
"organization-company": 28,
|
| 78 |
-
"organization-education": 29,
|
| 79 |
-
"organization-government/governmentagency": 30,
|
| 80 |
-
"organization-media/newspaper": 31,
|
| 81 |
-
"organization-other": 32,
|
| 82 |
-
"organization-politicalparty": 33,
|
| 83 |
-
"organization-religion": 34,
|
| 84 |
-
"organization-showorganization": 35,
|
| 85 |
-
"organization-sportsleague": 36,
|
| 86 |
-
"organization-sportsteam": 37,
|
| 87 |
-
"other-astronomything": 38,
|
| 88 |
-
"other-award": 39,
|
| 89 |
-
"other-biologything": 40,
|
| 90 |
-
"other-chemicalthing": 41,
|
| 91 |
-
"other-currency": 42,
|
| 92 |
-
"other-disease": 43,
|
| 93 |
-
"other-educationaldegree": 44,
|
| 94 |
-
"other-god": 45,
|
| 95 |
-
"other-language": 46,
|
| 96 |
-
"other-law": 47,
|
| 97 |
-
"other-livingthing": 48,
|
| 98 |
-
"other-medical": 49,
|
| 99 |
-
"person-actor": 50,
|
| 100 |
-
"person-artist/author": 51,
|
| 101 |
-
"person-athlete": 52,
|
| 102 |
-
"person-director": 53,
|
| 103 |
-
"person-other": 54,
|
| 104 |
-
"person-politician": 55,
|
| 105 |
-
"person-scholar": 56,
|
| 106 |
-
"person-soldier": 57,
|
| 107 |
-
"product-airplane": 58,
|
| 108 |
-
"product-car": 59,
|
| 109 |
-
"product-food": 60,
|
| 110 |
-
"product-game": 61,
|
| 111 |
-
"product-other": 62,
|
| 112 |
-
"product-ship": 63,
|
| 113 |
-
"product-software": 64,
|
| 114 |
-
"product-train": 65,
|
| 115 |
-
"product-weapon": 66,
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
class FewNERDConfig(datasets.BuilderConfig):
|
| 120 |
-
"""BuilderConfig for FewNERD"""
|
| 121 |
-
|
| 122 |
-
def __init__(self, **kwargs):
|
| 123 |
-
"""BuilderConfig for FewNERD.
|
| 124 |
-
|
| 125 |
-
Args:
|
| 126 |
-
**kwargs: keyword arguments forwarded to super.
|
| 127 |
-
"""
|
| 128 |
-
super(FewNERDConfig, self).__init__(**kwargs)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
class FewNERD(datasets.GeneratorBasedBuilder):
|
| 132 |
-
BUILDER_CONFIGS = [
|
| 133 |
-
FewNERDConfig(name="supervised", description="Fully supervised setting."),
|
| 134 |
-
FewNERDConfig(
|
| 135 |
-
name="inter",
|
| 136 |
-
description="Few-shot setting. Each file contains all 8 coarse "
|
| 137 |
-
"types but different fine-grained types.",
|
| 138 |
-
),
|
| 139 |
-
FewNERDConfig(
|
| 140 |
-
name="intra", description="Few-shot setting. Randomly split by coarse type."
|
| 141 |
-
),
|
| 142 |
-
]
|
| 143 |
-
|
| 144 |
-
def _info(self):
|
| 145 |
-
return datasets.DatasetInfo(
|
| 146 |
-
description=_DESCRIPTION,
|
| 147 |
-
features=datasets.Features(
|
| 148 |
-
{
|
| 149 |
-
"id": datasets.Value("string"),
|
| 150 |
-
"tokens": datasets.features.Sequence(datasets.Value("string")),
|
| 151 |
-
"ner_tags": datasets.features.Sequence(
|
| 152 |
-
datasets.features.ClassLabel(
|
| 153 |
-
names=[
|
| 154 |
-
"O",
|
| 155 |
-
"art",
|
| 156 |
-
"building",
|
| 157 |
-
"event",
|
| 158 |
-
"location",
|
| 159 |
-
"organization",
|
| 160 |
-
"other",
|
| 161 |
-
"person",
|
| 162 |
-
"product",
|
| 163 |
-
]
|
| 164 |
-
)
|
| 165 |
-
),
|
| 166 |
-
"fine_ner_tags": datasets.Sequence(
|
| 167 |
-
datasets.features.ClassLabel(
|
| 168 |
-
names=[
|
| 169 |
-
"O",
|
| 170 |
-
"art-broadcastprogram",
|
| 171 |
-
"art-film",
|
| 172 |
-
"art-music",
|
| 173 |
-
"art-other",
|
| 174 |
-
"art-painting",
|
| 175 |
-
"art-writtenart",
|
| 176 |
-
"building-airport",
|
| 177 |
-
"building-hospital",
|
| 178 |
-
"building-hotel",
|
| 179 |
-
"building-library",
|
| 180 |
-
"building-other",
|
| 181 |
-
"building-restaurant",
|
| 182 |
-
"building-sportsfacility",
|
| 183 |
-
"building-theater",
|
| 184 |
-
"event-attack/battle/war/militaryconflict",
|
| 185 |
-
"event-disaster",
|
| 186 |
-
"event-election",
|
| 187 |
-
"event-other",
|
| 188 |
-
"event-protest",
|
| 189 |
-
"event-sportsevent",
|
| 190 |
-
"location-GPE",
|
| 191 |
-
"location-bodiesofwater",
|
| 192 |
-
"location-island",
|
| 193 |
-
"location-mountain",
|
| 194 |
-
"location-other",
|
| 195 |
-
"location-park",
|
| 196 |
-
"location-road/railway/highway/transit",
|
| 197 |
-
"organization-company",
|
| 198 |
-
"organization-education",
|
| 199 |
-
"organization-government/governmentagency",
|
| 200 |
-
"organization-media/newspaper",
|
| 201 |
-
"organization-other",
|
| 202 |
-
"organization-politicalparty",
|
| 203 |
-
"organization-religion",
|
| 204 |
-
"organization-showorganization",
|
| 205 |
-
"organization-sportsleague",
|
| 206 |
-
"organization-sportsteam",
|
| 207 |
-
"other-astronomything",
|
| 208 |
-
"other-award",
|
| 209 |
-
"other-biologything",
|
| 210 |
-
"other-chemicalthing",
|
| 211 |
-
"other-currency",
|
| 212 |
-
"other-disease",
|
| 213 |
-
"other-educationaldegree",
|
| 214 |
-
"other-god",
|
| 215 |
-
"other-language",
|
| 216 |
-
"other-law",
|
| 217 |
-
"other-livingthing",
|
| 218 |
-
"other-medical",
|
| 219 |
-
"person-actor",
|
| 220 |
-
"person-artist/author",
|
| 221 |
-
"person-athlete",
|
| 222 |
-
"person-director",
|
| 223 |
-
"person-other",
|
| 224 |
-
"person-politician",
|
| 225 |
-
"person-scholar",
|
| 226 |
-
"person-soldier",
|
| 227 |
-
"product-airplane",
|
| 228 |
-
"product-car",
|
| 229 |
-
"product-food",
|
| 230 |
-
"product-game",
|
| 231 |
-
"product-other",
|
| 232 |
-
"product-ship",
|
| 233 |
-
"product-software",
|
| 234 |
-
"product-train",
|
| 235 |
-
"product-weapon",
|
| 236 |
-
]
|
| 237 |
-
)
|
| 238 |
-
),
|
| 239 |
-
}
|
| 240 |
-
),
|
| 241 |
-
supervised_keys=None,
|
| 242 |
-
homepage="https://ningding97.github.io/fewnerd/",
|
| 243 |
-
citation=_CITATION,
|
| 244 |
-
)
|
| 245 |
-
|
| 246 |
-
def _split_generators(self, dl_manager):
|
| 247 |
-
"""Returns SplitGenerators."""
|
| 248 |
-
url_to_download = dl_manager.download_and_extract(_URLs[self.config.name])
|
| 249 |
-
return [
|
| 250 |
-
datasets.SplitGenerator(
|
| 251 |
-
name=datasets.Split.TRAIN,
|
| 252 |
-
gen_kwargs={
|
| 253 |
-
"filepath": os.path.join(
|
| 254 |
-
url_to_download,
|
| 255 |
-
self.config.name,
|
| 256 |
-
"train.txt",
|
| 257 |
-
)
|
| 258 |
-
},
|
| 259 |
-
),
|
| 260 |
-
datasets.SplitGenerator(
|
| 261 |
-
name=datasets.Split.VALIDATION,
|
| 262 |
-
gen_kwargs={
|
| 263 |
-
"filepath": os.path.join(
|
| 264 |
-
url_to_download, self.config.name, "dev.txt"
|
| 265 |
-
)
|
| 266 |
-
},
|
| 267 |
-
),
|
| 268 |
-
datasets.SplitGenerator(
|
| 269 |
-
name=datasets.Split.TEST,
|
| 270 |
-
gen_kwargs={
|
| 271 |
-
"filepath": os.path.join(
|
| 272 |
-
url_to_download, self.config.name, "test.txt"
|
| 273 |
-
)
|
| 274 |
-
},
|
| 275 |
-
),
|
| 276 |
-
]
|
| 277 |
-
|
| 278 |
-
def _generate_examples(self, filepath=None):
|
| 279 |
-
# check file type
|
| 280 |
-
assert filepath[-4:] == ".txt"
|
| 281 |
-
|
| 282 |
-
num_lines = sum(1 for _ in open(filepath, encoding="utf-8"))
|
| 283 |
-
id = 0
|
| 284 |
-
|
| 285 |
-
with open(filepath, "r", encoding="utf-8") as f:
|
| 286 |
-
tokens, ner_tags, fine_ner_tags = [], [], []
|
| 287 |
-
for line in tqdm(f, total=num_lines):
|
| 288 |
-
line = line.strip().split()
|
| 289 |
-
|
| 290 |
-
if line:
|
| 291 |
-
assert len(line) == 2
|
| 292 |
-
token, fine_ner_tag = line
|
| 293 |
-
ner_tag = fine_ner_tag.split("-")[0]
|
| 294 |
-
|
| 295 |
-
tokens.append(token)
|
| 296 |
-
ner_tags.append(NER_TAGS_DICT[ner_tag])
|
| 297 |
-
fine_ner_tags.append(FINE_NER_TAGS_DICT[fine_ner_tag])
|
| 298 |
-
|
| 299 |
-
elif tokens:
|
| 300 |
-
# organize a record to be written into json
|
| 301 |
-
record = {
|
| 302 |
-
"tokens": tokens,
|
| 303 |
-
"id": str(id),
|
| 304 |
-
"ner_tags": ner_tags,
|
| 305 |
-
"fine_ner_tags": fine_ner_tags,
|
| 306 |
-
}
|
| 307 |
-
tokens, ner_tags, fine_ner_tags = [], [], []
|
| 308 |
-
id += 1
|
| 309 |
-
yield record["id"], record
|
| 310 |
-
|
| 311 |
-
# take the last sentence
|
| 312 |
-
if tokens:
|
| 313 |
-
record = {
|
| 314 |
-
"tokens": tokens,
|
| 315 |
-
"id": str(id),
|
| 316 |
-
"ner_tags": ner_tags,
|
| 317 |
-
"fine_ner_tags": fine_ner_tags,
|
| 318 |
-
}
|
| 319 |
-
yield record["id"], record
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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