Upload MORFITT.py
Browse files- MORFITT.py +106 -0
MORFITT.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 35 |
+
features = datasets.Features(
|
| 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 |
+
}
|