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
|
|
|
|
| 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 |
+
"""Loading script for the biolang dataset for language modeling in biology."""
|
| 20 |
+
|
| 21 |
+
from __future__ import absolute_import, division, print_function
|
| 22 |
+
|
| 23 |
+
import json
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
import datasets
|
| 26 |
+
from common import CACHE
|
| 27 |
+
import shutil
|
| 28 |
+
|
| 29 |
+
_CITATION = """\
|
| 30 |
+
@Unpublished{
|
| 31 |
+
huggingface: dataset,
|
| 32 |
+
title = {biolang},
|
| 33 |
+
authors={Thomas Lemberger, EMBO},
|
| 34 |
+
year={2021}
|
| 35 |
+
}
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
_DESCRIPTION = """\
|
| 39 |
+
This dataset is based on abstracts from the open access section of PubMed Central to train language models for the domain of biology.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
_HOMEPAGE = "https://europepmc.org/downloads/openaccess"
|
| 43 |
+
|
| 44 |
+
_LICENSE = "CC BY 4.0"
|
| 45 |
+
|
| 46 |
+
_URLs = {
|
| 47 |
+
"biolang": "https://huggingface.co/datasets/EMBO/biolang/resolve/main/oapmc_abstracts_figs.zip",
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class BioLang(datasets.GeneratorBasedBuilder):
|
| 52 |
+
"""BioLang: a dataset to train language models in biology."""
|
| 53 |
+
|
| 54 |
+
VERSION = datasets.Version("0.0.1")
|
| 55 |
+
|
| 56 |
+
BUILDER_CONFIGS = [
|
| 57 |
+
datasets.BuilderConfig(name="MLM", version="0.0.1", description="Dataset for general masked language model."),
|
| 58 |
+
datasets.BuilderConfig(name="DET", version="0.0.1", description="Dataset for part-of-speech (determinant) masked language model."),
|
| 59 |
+
datasets.BuilderConfig(name="VERB", version="0.0.1", description="Dataset for part-of-speech (verbs) masked language model."),
|
| 60 |
+
datasets.BuilderConfig(name="SMALL", version="0.0.1", description="Dataset for part-of-speech (determinants, conjunctions, prepositions, pronouns) masked language model."),
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
DEFAULT_CONFIG_NAME = "MLM" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 64 |
+
|
| 65 |
+
def _info(self):
|
| 66 |
+
if self.config.name == "MLM":
|
| 67 |
+
features = datasets.Features(
|
| 68 |
+
{
|
| 69 |
+
"input_ids": datasets.Sequence(feature=datasets.Value("int32")),
|
| 70 |
+
"special_tokens_mask": datasets.Sequence(feature=datasets.Value("int8")),
|
| 71 |
+
}
|
| 72 |
+
)
|
| 73 |
+
elif self.config.name in ["DET", "VERB", "SMALL"]:
|
| 74 |
+
features = datasets.Features({
|
| 75 |
+
"input_ids": datasets.Sequence(feature=datasets.Value("int32")),
|
| 76 |
+
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
return datasets.DatasetInfo(
|
| 80 |
+
description=_DESCRIPTION,
|
| 81 |
+
features=features, # Here we define them above because they are different between the two configurations
|
| 82 |
+
supervised_keys=('input_ids', 'pos_mask'),
|
| 83 |
+
homepage=_HOMEPAGE,
|
| 84 |
+
license=_LICENSE,
|
| 85 |
+
citation=_CITATION,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def _split_generators(self, dl_manager):
|
| 89 |
+
"""Returns SplitGenerators."""
|
| 90 |
+
if self.config.data_dir:
|
| 91 |
+
data_dir = self.config.data_dir
|
| 92 |
+
else:
|
| 93 |
+
url = _URLs["biolang"]
|
| 94 |
+
data_dir = dl_manager.download_and_extract(url)
|
| 95 |
+
data_dir += "/oapmc_abstracts_figs"
|
| 96 |
+
return [
|
| 97 |
+
datasets.SplitGenerator(
|
| 98 |
+
name=datasets.Split.TRAIN,
|
| 99 |
+
gen_kwargs={
|
| 100 |
+
"filepath": data_dir + "/train.jsonl"),
|
| 101 |
+
"split": "train",
|
| 102 |
+
},
|
| 103 |
+
),
|
| 104 |
+
datasets.SplitGenerator(
|
| 105 |
+
name=datasets.Split.TEST,
|
| 106 |
+
gen_kwargs={
|
| 107 |
+
"filepath": data_dir + "/test.jsonl"),
|
| 108 |
+
"split": "test"
|
| 109 |
+
},
|
| 110 |
+
),
|
| 111 |
+
datasets.SplitGenerator(
|
| 112 |
+
name=datasets.Split.VALIDATION,
|
| 113 |
+
gen_kwargs={
|
| 114 |
+
"filepath": data_dir + "/eval.jsonl"),
|
| 115 |
+
"split": "eval",
|
| 116 |
+
},
|
| 117 |
+
),
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
def _generate_examples(self, filepath, split):
|
| 121 |
+
""" Yields examples. """
|
| 122 |
+
with open(filepath, encoding="utf-8") as f:
|
| 123 |
+
for id_, row in enumerate(f):
|
| 124 |
+
data = json.loads(row)
|
| 125 |
+
if self.config.name == "MLM":
|
| 126 |
+
yield id_, {
|
| 127 |
+
"input_ids": data["input_ids"],
|
| 128 |
+
"special_tokens_mask": data['special_tokens_mask']
|
| 129 |
+
}
|
| 130 |
+
elif self.config.name == "DET":
|
| 131 |
+
pos_mask = [0] * len(data['input_ids'])
|
| 132 |
+
for idx, label in enumerate(data['label_ids']):
|
| 133 |
+
if label == 'DET':
|
| 134 |
+
pos_mask[idx] = 1
|
| 135 |
+
yield id_, {
|
| 136 |
+
"input_ids": data['input_ids'],
|
| 137 |
+
"tag_mask": pos_mask,
|
| 138 |
+
}
|
| 139 |
+
elif self.config.name == "VERB":
|
| 140 |
+
pos_mask = [0] * len(data['input_ids'])
|
| 141 |
+
for idx, label in enumerate(data['label_ids']):
|
| 142 |
+
if label == 'VERB':
|
| 143 |
+
pos_mask[idx] = 1
|
| 144 |
+
yield id_, {
|
| 145 |
+
"input_ids": data['input_ids'],
|
| 146 |
+
"tag_mask": pos_mask,
|
| 147 |
+
}
|
| 148 |
+
elif self.config.name == "SMALL":
|
| 149 |
+
pos_mask = [0] * len(data['input_ids'])
|
| 150 |
+
for idx, label in enumerate(data['label_ids']):
|
| 151 |
+
if label in ['DET', 'CCONJ', 'SCONJ', 'ADP', 'PRON']:
|
| 152 |
+
pos_mask[idx] = 1
|
| 153 |
+
yield id_, {
|
| 154 |
+
"input_ids": data['input_ids'],
|
| 155 |
+
"tag_mask": pos_mask,
|
| 156 |
+
}
|