ramybaly
commited on
Commit
·
7b2f59a
1
Parent(s):
dad822e
added dataset files and script
Browse files- .DS_Store +0 -0
- .gitattributes +8 -0
- id2coarse_tags.json +3 -0
- id2fine_tags.json +3 -0
- nerd.py +210 -0
- prepare_data.py +36 -0
- test.json +3 -0
- test.txt +3 -0
- train.json +3 -0
- train.txt +3 -0
- validation.json +3 -0
- validation.txt +3 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -14,3 +14,11 @@
|
|
| 14 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 15 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 16 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 15 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 16 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
validation.txt filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
test.txt filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
train.txt filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
train.json filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
validation.json filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
id2coarse_tags.json filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
id2fine_tags.json filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
test.json filter=lfs diff=lfs merge=lfs -text
|
id2coarse_tags.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3f8ea0be9d72f6d12a799687307099f1b742055095d8f7424ec46c4781769dfc
|
| 3 |
+
size 136
|
id2fine_tags.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 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 |
+
# FEW-NERD: A Few-shot Named Entity Recognition Dataset
|
| 18 |
+
|
| 19 |
+
import datasets
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
logger = datasets.logging.get_logger(__name__)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@article{ding2021few,
|
| 27 |
+
title={Few-NERD: A Few-Shot Named Entity Recognition Dataset},
|
| 28 |
+
author={Ding, Ning and Xu, Guangwei and Chen, Yulin and Wang, Xiaobin and Han, Xu and Xie, Pengjun and Zheng, Hai-Tao and Liu, Zhiyuan},
|
| 29 |
+
journal={arXiv preprint arXiv:2105.07464},
|
| 30 |
+
year={2021}
|
| 31 |
+
}"""
|
| 32 |
+
|
| 33 |
+
_DESCRIPTION = """\
|
| 34 |
+
Recently, considerable literature has grown up around the theme of few-shot named entity recognition (NER), but little published benchmark
|
| 35 |
+
data specifically focused on the practical and challenging task. Current approaches collect existing supervised NER datasets and reorganize
|
| 36 |
+
them into the few-shot setting for empirical study. These strategies conventionally aim to recognize coarse-grained entity types with few
|
| 37 |
+
examples, while in practice, most unseen entity types are fine-grained. In this paper, we present FEW-NERD, a large-scale human-annotated
|
| 38 |
+
few-shot NER dataset with a hierarchy of 8 coarse-grained and 66 fine-grained entity types. FEW-NERD consists of 188,238 sentences from
|
| 39 |
+
Wikipedia, 4,601,160 words are included and each is annotated as context or a part of a two-level entity type. To the best of our knowledge,
|
| 40 |
+
this is the first few-shot NER dataset and the largest human-crafted NER dataset. We construct benchmark tasks with different emphases to
|
| 41 |
+
comprehensively assess the generalization capability of models. Extensive empirical results and analysis show that FEW-NERD is challenging
|
| 42 |
+
and the problem requires further research. We make Few-NERD public at https://nigding97.github.io/fewnerd/
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class NERDConfig(datasets.BuilderConfig):
|
| 47 |
+
"""BuilderConfig for NERD"""
|
| 48 |
+
|
| 49 |
+
def __init__(self, **kwargs):
|
| 50 |
+
"""BuilderConfig for NERD.
|
| 51 |
+
Args:
|
| 52 |
+
**kwargs: keyword arguments forwarded to super.
|
| 53 |
+
"""
|
| 54 |
+
super(NERDConfig, self).__init__(**kwargs)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class NERD(datasets.GeneratorBasedBuilder):
|
| 58 |
+
"""Conll2012 dataset."""
|
| 59 |
+
|
| 60 |
+
BUILDER_CONFIGS = [
|
| 61 |
+
NERDConfig(name="nerd", version=datasets.Version("1.0.0"), description="NERD dataset"),
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
def _info(self):
|
| 65 |
+
return datasets.DatasetInfo(
|
| 66 |
+
description=_DESCRIPTION,
|
| 67 |
+
features=datasets.Features(
|
| 68 |
+
{
|
| 69 |
+
"id": datasets.Value("string"),
|
| 70 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
| 71 |
+
"pos_tags": datasets.Sequence(
|
| 72 |
+
datasets.features.ClassLabel(
|
| 73 |
+
names=['O', 'art', 'building', 'event', 'location', 'organization', 'other', 'person', 'product']
|
| 74 |
+
)
|
| 75 |
+
),
|
| 76 |
+
"ner_tags": datasets.Sequence(
|
| 77 |
+
datasets.features.ClassLabel(
|
| 78 |
+
names=[
|
| 79 |
+
'O',
|
| 80 |
+
'art-broadcastprogram',
|
| 81 |
+
'art-film',
|
| 82 |
+
'art-music',
|
| 83 |
+
'art-other',
|
| 84 |
+
'art-painting',
|
| 85 |
+
'art-writtenart',
|
| 86 |
+
'building-airport',
|
| 87 |
+
'building-hospital',
|
| 88 |
+
'building-hotel',
|
| 89 |
+
'building-library',
|
| 90 |
+
'building-other',
|
| 91 |
+
'building-restaurant',
|
| 92 |
+
'building-sportsfacility',
|
| 93 |
+
'building-theater',
|
| 94 |
+
'event-attack/battle/war/militaryconflict',
|
| 95 |
+
'event-disaster',
|
| 96 |
+
'event-election',
|
| 97 |
+
'event-other',
|
| 98 |
+
'event-protest',
|
| 99 |
+
'event-sportsevent',
|
| 100 |
+
'location-GPE',
|
| 101 |
+
'location-bodiesofwater',
|
| 102 |
+
'location-island',
|
| 103 |
+
'location-mountain',
|
| 104 |
+
'location-other',
|
| 105 |
+
'location-park',
|
| 106 |
+
'location-road/railway/highway/transit',
|
| 107 |
+
'organization-company',
|
| 108 |
+
'organization-education',
|
| 109 |
+
'organization-government/governmentagency',
|
| 110 |
+
'organization-media/newspaper',
|
| 111 |
+
'organization-other',
|
| 112 |
+
'organization-politicalparty',
|
| 113 |
+
'organization-religion',
|
| 114 |
+
'organization-showorganization',
|
| 115 |
+
'organization-sportsleague',
|
| 116 |
+
'organization-sportsteam',
|
| 117 |
+
'other-astronomything',
|
| 118 |
+
'other-award',
|
| 119 |
+
'other-biologything',
|
| 120 |
+
'other-chemicalthing',
|
| 121 |
+
'other-currency',
|
| 122 |
+
'other-disease',
|
| 123 |
+
'other-educationaldegree',
|
| 124 |
+
'other-god',
|
| 125 |
+
'other-language',
|
| 126 |
+
'other-law',
|
| 127 |
+
'other-livingthing',
|
| 128 |
+
'other-medical',
|
| 129 |
+
'person-actor',
|
| 130 |
+
'person-artist/author',
|
| 131 |
+
'person-athlete',
|
| 132 |
+
'person-director',
|
| 133 |
+
'person-other',
|
| 134 |
+
'person-politician',
|
| 135 |
+
'person-scholar',
|
| 136 |
+
'person-soldier',
|
| 137 |
+
'product-airplane',
|
| 138 |
+
'product-car',
|
| 139 |
+
'product-food',
|
| 140 |
+
'product-game',
|
| 141 |
+
'product-other',
|
| 142 |
+
'product-ship',
|
| 143 |
+
'product-software',
|
| 144 |
+
'product-train',
|
| 145 |
+
'product-weapon'
|
| 146 |
+
]
|
| 147 |
+
)
|
| 148 |
+
),
|
| 149 |
+
}
|
| 150 |
+
),
|
| 151 |
+
supervised_keys=None,
|
| 152 |
+
homepage="https://catalog.ldc.upenn.edu/LDC2013T19",
|
| 153 |
+
citation=_CITATION,
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
def _split_generators(self, dl_manager):
|
| 157 |
+
"""Returns SplitGenerators."""
|
| 158 |
+
urls_to_download = {
|
| 159 |
+
# "train": f"{_URL}{_TRAINING_FILE}",
|
| 160 |
+
# "validation": f"{_URL}{_DEV_FILE}",
|
| 161 |
+
# "test": f"{_URL}{_TEST_FILE}",
|
| 162 |
+
'train': 'train.txt',
|
| 163 |
+
'validation': 'validation.txt',
|
| 164 |
+
'test': 'test.txt',
|
| 165 |
+
}
|
| 166 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
| 167 |
+
|
| 168 |
+
return [
|
| 169 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
| 170 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["validation"]}),
|
| 171 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
|
| 172 |
+
]
|
| 173 |
+
|
| 174 |
+
def _generate_examples(self, filepath):
|
| 175 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
| 176 |
+
|
| 177 |
+
with open(filepath, encoding="utf-8") as f:
|
| 178 |
+
lines = f.readlines()
|
| 179 |
+
|
| 180 |
+
guid = 0
|
| 181 |
+
tokens = []
|
| 182 |
+
ner_tags = []
|
| 183 |
+
ner_tags_fine = []
|
| 184 |
+
|
| 185 |
+
for line in lines:
|
| 186 |
+
if line.startswith("-DOCSTART-") or line.strip() == "" or line == "\n":
|
| 187 |
+
if tokens:
|
| 188 |
+
yield guid, {
|
| 189 |
+
"id": str(guid),
|
| 190 |
+
"tokens": tokens,
|
| 191 |
+
"ner_tags": ner_tags,
|
| 192 |
+
"ner_tags_fine": ner_tags_fine,
|
| 193 |
+
}
|
| 194 |
+
guid += 1
|
| 195 |
+
tokens = []
|
| 196 |
+
ner_tags = []
|
| 197 |
+
ner_tags_fine = []
|
| 198 |
+
else:
|
| 199 |
+
# nerd tokens are tab- separated
|
| 200 |
+
splits = line.split('\t')
|
| 201 |
+
tokens.append(splits[0])
|
| 202 |
+
ner_tags.append(splits[1])
|
| 203 |
+
ner_tags_fine.append(splits[2].rstrip())
|
| 204 |
+
# last example
|
| 205 |
+
yield guid, {
|
| 206 |
+
"id": str(guid),
|
| 207 |
+
"tokens": tokens,
|
| 208 |
+
"ner_tags": ner_tags,
|
| 209 |
+
"ner_tags_fine": ner_tags_fine,
|
| 210 |
+
}
|
prepare_data.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
import json
|
| 3 |
+
import pickle
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def main():
|
| 7 |
+
id2label = {int(i): tag for i, tag in json.load(open('./id2coarse_tags.json', 'r')).items()}
|
| 8 |
+
id2label_fine = {int(i): tag for i, tag in json.load(open('./id2fine_tags.json', 'r')).items()}
|
| 9 |
+
|
| 10 |
+
files = glob.glob('./*json')
|
| 11 |
+
|
| 12 |
+
for file in files:
|
| 13 |
+
if 'tags.json' in file:
|
| 14 |
+
continue
|
| 15 |
+
|
| 16 |
+
with open(file, 'r') as f:
|
| 17 |
+
sentences = f.readlines()
|
| 18 |
+
|
| 19 |
+
with open(file.replace('.json', '.txt'), 'w') as f:
|
| 20 |
+
for sentence in sentences:
|
| 21 |
+
sentence = json.loads(sentence)
|
| 22 |
+
tokens = sentence['tokens']
|
| 23 |
+
ner_tags_ids = sentence['coarse_tags']
|
| 24 |
+
ner_tags_fine_ids = sentence['fine_tags']
|
| 25 |
+
|
| 26 |
+
ner_tags = [id2label[t] for t in ner_tags_ids]
|
| 27 |
+
ner_tags_fine = [id2label_fine[t] for t in ner_tags_fine_ids]
|
| 28 |
+
|
| 29 |
+
for token, tag, tag_fine in zip(tokens, ner_tags, ner_tags_fine):
|
| 30 |
+
f.write(f'{token}\t{tag}\t{tag_fine}\n')
|
| 31 |
+
|
| 32 |
+
f.write('\n')
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
main()
|
test.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9db8564d48dc7ff29e6bc15e8be5aa7b1d95d241a77cead5e1839e7bb0007751
|
| 3 |
+
size 15804621
|
test.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e424b4925a7520e5dba7d6ee57b8ae4c908fa7fad6f6660a784400767074b94d
|
| 3 |
+
size 13203253
|
train.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b740b0c841c1d67289fe0c61f83c52a73d190fd6e60bfbfab78f7c6910d8685
|
| 3 |
+
size 55425936
|
train.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b15f7964a22b17b454ed07dd4b98205a3c60c1963e206a31c9f0d5aee856819
|
| 3 |
+
size 46308665
|
validation.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:805c189cbe7b3918b20727c49da42aa6dbadcf2fe6ba469ad4da0e2605ecde3e
|
| 3 |
+
size 7934187
|
validation.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:daa175f505958b071d99298ee94837dda32771bf92dd830b644ab95fd72073c1
|
| 3 |
+
size 6636339
|