chintagunta85
commited on
Commit
•
2b9ed52
1
Parent(s):
0d876a9
Upload BC5CDR.py
Browse files
BC5CDR.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
|
3 |
+
|
4 |
+
logger = datasets.logging.get_logger(__name__)
|
5 |
+
|
6 |
+
|
7 |
+
_CITATION = """\
|
8 |
+
@article{krallinger2015chemdner,
|
9 |
+
title={The CHEMDNER corpus of chemicals and drugs and its annotation principles},
|
10 |
+
author={Krallinger, Martin and Rabal, Obdulia and Leitner, Florian and Vazquez, Miguel and Salgado, David and Lu, Zhiyong and Leaman, Robert and Lu, Yanan and Ji, Donghong and Lowe, Daniel M and others},
|
11 |
+
journal={Journal of cheminformatics},
|
12 |
+
volume={7},
|
13 |
+
number={1},
|
14 |
+
pages={1--17},
|
15 |
+
year={2015},
|
16 |
+
publisher={BioMed Central}
|
17 |
+
}
|
18 |
+
"""
|
19 |
+
|
20 |
+
_DESCRIPTION = """\
|
21 |
+
"""
|
22 |
+
|
23 |
+
_HOMEPAGE = ""
|
24 |
+
_URL = "https://github.com/cambridgeltl/MTL-Bioinformatics-2016/raw/master/data/BC5CDR-IOB/"
|
25 |
+
_TRAINING_FILE = "train.tsv"
|
26 |
+
_DEV_FILE = "devel.tsv"
|
27 |
+
_TEST_FILE = "test.tsv"
|
28 |
+
|
29 |
+
|
30 |
+
class BC4CHEMDConfig(datasets.BuilderConfig):
|
31 |
+
"""BuilderConfig for BC4CHEMD"""
|
32 |
+
|
33 |
+
def __init__(self, **kwargs):
|
34 |
+
"""BuilderConfig for BC4CHEMD.
|
35 |
+
Args:
|
36 |
+
**kwargs: keyword arguments forwarded to super.
|
37 |
+
"""
|
38 |
+
super(BC4CHEMDConfig, self).__init__(**kwargs)
|
39 |
+
|
40 |
+
|
41 |
+
class BC4CHEMD(datasets.GeneratorBasedBuilder):
|
42 |
+
""" BC4CHEMD dataset."""
|
43 |
+
|
44 |
+
BUILDER_CONFIGS = [
|
45 |
+
BC4CHEMDConfig(name="BC5CDR-Disease", version=datasets.Version("1.0.0"), description=" BC5CDR-Disease dataset"),
|
46 |
+
]
|
47 |
+
|
48 |
+
def _info(self):
|
49 |
+
|
50 |
+
custom_names = ['O','B-GENE','I-GENE','B-CHEMICAL','I-CHEMICAL','B-DISEASE','I-DISEASE',
|
51 |
+
'B-DNA', 'I-DNA', 'B-RNA', 'I-RNA', 'B-CELL_LINE', 'I-CELL_LINE', 'B-CELL_TYPE', 'I-CELL_TYPE',
|
52 |
+
'B-PROTEIN', 'I-PROTEIN', 'B-SPECIES', 'I-SPECIES']
|
53 |
+
|
54 |
+
return datasets.DatasetInfo(
|
55 |
+
description=_DESCRIPTION,
|
56 |
+
features=datasets.Features(
|
57 |
+
{
|
58 |
+
"id": datasets.Value("string"),
|
59 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
60 |
+
"ner_tags": datasets.Sequence(
|
61 |
+
datasets.features.ClassLabel(
|
62 |
+
names=custom_names
|
63 |
+
)
|
64 |
+
),
|
65 |
+
}
|
66 |
+
),
|
67 |
+
supervised_keys=None,
|
68 |
+
homepage=_HOMEPAGE,
|
69 |
+
citation=_CITATION,
|
70 |
+
)
|
71 |
+
|
72 |
+
def _split_generators(self, dl_manager):
|
73 |
+
"""Returns SplitGenerators."""
|
74 |
+
urls_to_download = {
|
75 |
+
"train": f"{_URL}{_TRAINING_FILE}",
|
76 |
+
"dev": f"{_URL}{_DEV_FILE}",
|
77 |
+
"test": f"{_URL}{_TEST_FILE}",
|
78 |
+
}
|
79 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
80 |
+
|
81 |
+
return [
|
82 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
83 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
84 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
|
85 |
+
]
|
86 |
+
|
87 |
+
def _generate_examples(self, filepath):
|
88 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
89 |
+
with open(filepath, encoding="utf-8") as f:
|
90 |
+
guid = 0
|
91 |
+
tokens = []
|
92 |
+
ner_tags = []
|
93 |
+
for line in f:
|
94 |
+
if line == "" or line == "\n":
|
95 |
+
if tokens:
|
96 |
+
print(tokens)
|
97 |
+
yield guid, {
|
98 |
+
"id": str(guid),
|
99 |
+
"tokens": tokens,
|
100 |
+
"ner_tags": ner_tags,
|
101 |
+
}
|
102 |
+
guid += 1
|
103 |
+
tokens = []
|
104 |
+
ner_tags = []
|
105 |
+
else:
|
106 |
+
# tokens are tab separated
|
107 |
+
splits = line.split("\t")
|
108 |
+
tokens.append(splits[0])
|
109 |
+
if(splits[1].rstrip()=="B-Chemical"):
|
110 |
+
ner_tags.append("B-CHEMICAL")
|
111 |
+
elif(splits[1].rstrip()=="I-Chemical"):
|
112 |
+
ner_tags.append("I-CHEMICAL")
|
113 |
+
elif(splits[1].rstrip()=="B-Disease"):
|
114 |
+
ner_tags.append("B-DISEASE")
|
115 |
+
elif(splits[1].rstrip()=="I-Disease"):
|
116 |
+
ner_tags.append("I-DISEASE")
|
117 |
+
else:
|
118 |
+
ner_tags.append("O")
|
119 |
+
# ner_tags.append(splits[1].rstrip())
|
120 |
+
# last example
|
121 |
+
yield guid, {
|
122 |
+
"id": str(guid),
|
123 |
+
"tokens": tokens,
|
124 |
+
"ner_tags": ner_tags,
|
125 |
+
}
|