Datasets:
Create new file
Browse files- sd-character-level-ner.py +171 -0
sd-character-level-ner.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
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 |
+
|
17 |
+
# template from : https://github.com/huggingface/datasets/blob/master/templates/new_dataset_script.py
|
18 |
+
|
19 |
+
from __future__ import absolute_import, division, print_function
|
20 |
+
|
21 |
+
import json
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
_BASE_URL = "https://huggingface.co/datasets/EMBO/sd-character-level-ner/resolve/main/"
|
25 |
+
|
26 |
+
class SourceDataNLP(datasets.GeneratorBasedBuilder):
|
27 |
+
"""SourceDataNLP provides datasets to train NLP tasks in cell and molecular biology."""
|
28 |
+
|
29 |
+
_NER_LABEL_NAMES = [
|
30 |
+
"O",
|
31 |
+
"B-SMALL_MOLECULE",
|
32 |
+
"I-SMALL_MOLECULE",
|
33 |
+
"B-GENEPROD",
|
34 |
+
"I-GENEPROD",
|
35 |
+
"B-SUBCELLULAR",
|
36 |
+
"I-SUBCELLULAR",
|
37 |
+
"B-CELL",
|
38 |
+
"I-CELL",
|
39 |
+
"B-TISSUE",
|
40 |
+
"I-TISSUE",
|
41 |
+
"B-ORGANISM",
|
42 |
+
"I-ORGANISM",
|
43 |
+
"B-EXP_ASSAY",
|
44 |
+
"I-EXP_ASSAY",
|
45 |
+
]
|
46 |
+
_PANEL_START_NAMES = ["O", "B-PANEL_START", "I-PANEL_START"]
|
47 |
+
|
48 |
+
_CITATION = """\
|
49 |
+
@Unpublished{
|
50 |
+
huggingface: dataset,
|
51 |
+
title = {SourceData NLP},
|
52 |
+
authors={Thomas Lemberger & Jorge Abreu-Vicente, EMBO},
|
53 |
+
year={2021}
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
|
57 |
+
_DESCRIPTION = """\
|
58 |
+
This dataset is based on the SourceData database and is intented to facilitate training of NLP tasks in the cell and molecualr biology domain.
|
59 |
+
"""
|
60 |
+
|
61 |
+
_HOMEPAGE = "https://huggingface.co/datasets/EMBO/sd-character-level-ner"
|
62 |
+
|
63 |
+
_LICENSE = "CC-BY 4.0"
|
64 |
+
|
65 |
+
VERSION = datasets.Version("1.0.0")
|
66 |
+
|
67 |
+
_URLS = {
|
68 |
+
"NER": f"{_BASE_URL}sd_character_panels.zip",
|
69 |
+
"PANELIZATION": f"{_BASE_URL}sd_character_panelization.zip",
|
70 |
+
}
|
71 |
+
BUILDER_CONFIGS = [
|
72 |
+
datasets.BuilderConfig(name="NER", version=VERSION, description="Dataset for entity recognition"),
|
73 |
+
datasets.BuilderConfig(
|
74 |
+
name="PANELIZATION",
|
75 |
+
version=VERSION,
|
76 |
+
description="Dataset for figure legend segmentation into panel-specific legends.",
|
77 |
+
),
|
78 |
+
]
|
79 |
+
|
80 |
+
DEFAULT_CONFIG_NAME = "NER"
|
81 |
+
|
82 |
+
def _info(self):
|
83 |
+
if self.config.name == "NER":
|
84 |
+
features = datasets.Features(
|
85 |
+
{
|
86 |
+
"words": datasets.Sequence(feature=datasets.Value("string")),
|
87 |
+
"labels": datasets.Sequence(
|
88 |
+
feature=datasets.ClassLabel(num_classes=len(self._NER_LABEL_NAMES),
|
89 |
+
names=self._NER_LABEL_NAMES)
|
90 |
+
),
|
91 |
+
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
|
92 |
+
}
|
93 |
+
)
|
94 |
+
elif self.config.name == "PANELIZATION":
|
95 |
+
features = datasets.Features(
|
96 |
+
{
|
97 |
+
"words": datasets.Sequence(feature=datasets.Value("string")),
|
98 |
+
"labels": datasets.Sequence(
|
99 |
+
feature=datasets.ClassLabel(num_classes=len(self._PANEL_START_NAMES),
|
100 |
+
names=self._PANEL_START_NAMES)
|
101 |
+
),
|
102 |
+
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
|
103 |
+
}
|
104 |
+
)
|
105 |
+
|
106 |
+
return datasets.DatasetInfo(
|
107 |
+
description=self._DESCRIPTION,
|
108 |
+
features=features,
|
109 |
+
supervised_keys=("words", "label_ids"),
|
110 |
+
homepage=self._HOMEPAGE,
|
111 |
+
license=self._LICENSE,
|
112 |
+
citation=self._CITATION,
|
113 |
+
)
|
114 |
+
|
115 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
116 |
+
"""Returns SplitGenerators.
|
117 |
+
Uses local files if a data_dir is specified. Otherwise downloads the files from their official url."""
|
118 |
+
|
119 |
+
if self.config.name in ["NER"]:
|
120 |
+
url = self._URLS["NER"]
|
121 |
+
data_dir = dl_manager.download_and_extract(url)
|
122 |
+
data_dir += "/sd_character_panels"
|
123 |
+
elif self.config.name == "PANELIZATION":
|
124 |
+
url = self._URLS[self.config.name]
|
125 |
+
data_dir = dl_manager.download_and_extract(url)
|
126 |
+
data_dir += "/sd_character_panelization"
|
127 |
+
else:
|
128 |
+
raise ValueError(f"unkonwn config name: {self.config.name}")
|
129 |
+
|
130 |
+
return [
|
131 |
+
datasets.SplitGenerator(
|
132 |
+
name=datasets.Split.TRAIN,
|
133 |
+
# These kwargs will be passed to _generate_examples
|
134 |
+
gen_kwargs={
|
135 |
+
"filepath": data_dir + "/train.jsonl"},
|
136 |
+
),
|
137 |
+
datasets.SplitGenerator(
|
138 |
+
name=datasets.Split.TEST,
|
139 |
+
gen_kwargs={
|
140 |
+
"filepath": data_dir + "/test.jsonl"},
|
141 |
+
),
|
142 |
+
datasets.SplitGenerator(
|
143 |
+
name=datasets.Split.VALIDATION,
|
144 |
+
gen_kwargs={
|
145 |
+
"filepath": data_dir + "/eval.jsonl"},
|
146 |
+
),
|
147 |
+
]
|
148 |
+
|
149 |
+
def _generate_examples(self, filepath):
|
150 |
+
"""Yields examples. This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
|
151 |
+
It is in charge of opening the given file and yielding (key, example) tuples from the dataset
|
152 |
+
The key is not important, it's more here for legacy reason (legacy from tfds)"""
|
153 |
+
|
154 |
+
with open(filepath, encoding="utf-8") as f:
|
155 |
+
# logger.info("⏳ Generating examples from = %s", filepath)
|
156 |
+
for id_, row in enumerate(f):
|
157 |
+
data = json.loads(row)
|
158 |
+
if self.config.name == "NER":
|
159 |
+
labels = data["label_ids"]["entity_types"]
|
160 |
+
tag_mask = [0 if tag == "O" else 1 for tag in labels]
|
161 |
+
yield id_, {
|
162 |
+
"text": data["text"],
|
163 |
+
"labels": labels
|
164 |
+
}
|
165 |
+
elif self.config.name == "PANELIZATION":
|
166 |
+
labels = data["label_ids"]["panel_start"]
|
167 |
+
tag_mask = [1 if t == "B-PANEL_START" else 0 for t in labels]
|
168 |
+
yield id_, {
|
169 |
+
"text": data["text"],
|
170 |
+
"labels": data["label_ids"]["panel_start"]
|
171 |
+
}
|