Update morfitt.py
Browse files- morfitt.py +50 -62
morfitt.py
CHANGED
|
@@ -1,42 +1,34 @@
|
|
| 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 |
-
"""MORFITT: A multi-label corpus of French biomedical literature"""
|
| 16 |
-
|
| 17 |
import os
|
| 18 |
import json
|
|
|
|
| 19 |
|
| 20 |
import datasets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
_DESCRIPTION = """\
|
| 23 |
-
|
| 24 |
"""
|
| 25 |
|
| 26 |
-
_HOMEPAGE = "
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
(comming soon)
|
| 34 |
-
"""
|
| 35 |
|
| 36 |
class MORFITT(datasets.GeneratorBasedBuilder):
|
| 37 |
-
"""MORFITT: A multi-label corpus of French biomedical literature"""
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def _info(self):
|
| 42 |
|
|
@@ -44,75 +36,71 @@ class MORFITT(datasets.GeneratorBasedBuilder):
|
|
| 44 |
{
|
| 45 |
"id": datasets.Value("string"),
|
| 46 |
"abstract": datasets.Value("string"),
|
| 47 |
-
"
|
| 48 |
-
names=
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
'microbiology',
|
| 54 |
-
'parasitology',
|
| 55 |
-
'pharmacology',
|
| 56 |
-
'physiology',
|
| 57 |
-
'psychology',
|
| 58 |
-
'surgery',
|
| 59 |
-
'veterinary',
|
| 60 |
-
'virology',
|
| 61 |
-
],
|
| 62 |
-
)),
|
| 63 |
}
|
| 64 |
)
|
| 65 |
-
|
| 66 |
return datasets.DatasetInfo(
|
| 67 |
description=_DESCRIPTION,
|
| 68 |
features=features,
|
|
|
|
| 69 |
homepage=_HOMEPAGE,
|
| 70 |
-
license=_LICENSE,
|
| 71 |
citation=_CITATION,
|
| 72 |
)
|
| 73 |
|
| 74 |
def _split_generators(self, dl_manager):
|
| 75 |
-
"""Returns SplitGenerators."""
|
| 76 |
-
|
| 77 |
-
data_dir = dl_manager.download_and_extract(_URL)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
return [
|
| 80 |
datasets.SplitGenerator(
|
| 81 |
name=datasets.Split.TRAIN,
|
| 82 |
gen_kwargs={
|
| 83 |
-
"
|
|
|
|
| 84 |
},
|
| 85 |
),
|
| 86 |
datasets.SplitGenerator(
|
| 87 |
name=datasets.Split.VALIDATION,
|
| 88 |
gen_kwargs={
|
| 89 |
-
"
|
|
|
|
| 90 |
},
|
| 91 |
),
|
| 92 |
datasets.SplitGenerator(
|
| 93 |
name=datasets.Split.TEST,
|
| 94 |
gen_kwargs={
|
| 95 |
-
"
|
|
|
|
| 96 |
},
|
| 97 |
),
|
| 98 |
]
|
| 99 |
|
| 100 |
-
def _generate_examples(self,
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
content = f_in.read()
|
| 104 |
-
f_in.close()
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
yield
|
| 115 |
-
"id": identifier,
|
| 116 |
-
"abstract": abstract,
|
| 117 |
-
"
|
|
|
|
| 118 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import random
|
| 4 |
|
| 5 |
import datasets
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
_CITATION = """\
|
| 10 |
+
ddd
|
| 11 |
+
"""
|
| 12 |
|
| 13 |
_DESCRIPTION = """\
|
| 14 |
+
ddd
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
_HOMEPAGE = "ddd"
|
| 18 |
|
| 19 |
+
_URL = "https://huggingface.co/datasets/Dr-BERT/MORFITT/resolve/main/data.zip"
|
| 20 |
|
| 21 |
+
_LICENSE = "unknown"
|
| 22 |
|
| 23 |
+
_SPECIALITIES = ['microbiology', 'etiology', 'virology', 'physiology', 'immunology', 'parasitology', 'genetics', 'chemistry', 'veterinary', 'surgery', 'pharmacology', 'psychology']
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class MORFITT(datasets.GeneratorBasedBuilder):
|
|
|
|
| 26 |
|
| 27 |
+
DEFAULT_CONFIG_NAME = "source"
|
| 28 |
+
|
| 29 |
+
BUILDER_CONFIGS = [
|
| 30 |
+
datasets.BuilderConfig(name="source", version="1.0.0", description="The MORFITT corpora"),
|
| 31 |
+
]
|
| 32 |
|
| 33 |
def _info(self):
|
| 34 |
|
|
|
|
| 36 |
{
|
| 37 |
"id": datasets.Value("string"),
|
| 38 |
"abstract": datasets.Value("string"),
|
| 39 |
+
"specialities": datasets.Sequence(
|
| 40 |
+
datasets.features.ClassLabel(names=_SPECIALITIES),
|
| 41 |
+
),
|
| 42 |
+
"specialities_one_hot": datasets.Sequence(
|
| 43 |
+
datasets.Value("float"),
|
| 44 |
+
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
)
|
| 47 |
+
|
| 48 |
return datasets.DatasetInfo(
|
| 49 |
description=_DESCRIPTION,
|
| 50 |
features=features,
|
| 51 |
+
supervised_keys=None,
|
| 52 |
homepage=_HOMEPAGE,
|
| 53 |
+
license=str(_LICENSE),
|
| 54 |
citation=_CITATION,
|
| 55 |
)
|
| 56 |
|
| 57 |
def _split_generators(self, dl_manager):
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
data_dir = dl_manager.download_and_extract(_URL).rstrip("/")
|
| 60 |
+
|
| 61 |
return [
|
| 62 |
datasets.SplitGenerator(
|
| 63 |
name=datasets.Split.TRAIN,
|
| 64 |
gen_kwargs={
|
| 65 |
+
"tsv_file": data_dir + "/train.tsv",
|
| 66 |
+
"split": "train",
|
| 67 |
},
|
| 68 |
),
|
| 69 |
datasets.SplitGenerator(
|
| 70 |
name=datasets.Split.VALIDATION,
|
| 71 |
gen_kwargs={
|
| 72 |
+
"tsv_file": data_dir + "/dev.tsv",
|
| 73 |
+
"split": "validation",
|
| 74 |
},
|
| 75 |
),
|
| 76 |
datasets.SplitGenerator(
|
| 77 |
name=datasets.Split.TEST,
|
| 78 |
gen_kwargs={
|
| 79 |
+
"tsv_file": data_dir + "/test.tsv",
|
| 80 |
+
"split": "test",
|
| 81 |
},
|
| 82 |
),
|
| 83 |
]
|
| 84 |
|
| 85 |
+
def _generate_examples(self, tsv_file, split):
|
| 86 |
+
|
| 87 |
+
# Load TSV file
|
| 88 |
+
df = pd.read_csv(tsv_file, sep="\t")
|
| 89 |
|
| 90 |
+
for index, e in df.iterrows():
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
specialities = e["specialities"].split("|")
|
| 93 |
|
| 94 |
+
# Empty one hot vector
|
| 95 |
+
one_hot = [0.0 for i in _SPECIALITIES]
|
| 96 |
|
| 97 |
+
# Fill up the one hot vector
|
| 98 |
+
for s in specialities:
|
| 99 |
+
one_hot[_SPECIALITIES.index(s)] = 1.0
|
| 100 |
|
| 101 |
+
yield e["identifier"], {
|
| 102 |
+
"id": e["identifier"],
|
| 103 |
+
"abstract": e["abstract"],
|
| 104 |
+
"specialities": specialities,
|
| 105 |
+
"specialities_one_hot": one_hot,
|
| 106 |
}
|