ghadeermobasher commited on
Commit
aa154c3
1 Parent(s): 869ee04

Create BC5CDR-Chemical-Disease.py

Browse files
Files changed (1) hide show
  1. BC5CDR-Chemical-Disease.py +117 -0
BC5CDR-Chemical-Disease.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ return datasets.DatasetInfo(
50
+ description=_DESCRIPTION,
51
+ features=datasets.Features(
52
+ {
53
+ "id": datasets.Value("string"),
54
+ "tokens": datasets.Sequence(datasets.Value("string")),
55
+ "ner_tags": datasets.Sequence(
56
+ datasets.features.ClassLabel(
57
+ names=[
58
+ "O",
59
+ "B-Disease",
60
+ "I-Disease",
61
+ "B-Chemical",
62
+ "I-Chemical"
63
+
64
+ ]
65
+ )
66
+ ),
67
+ }
68
+ ),
69
+ supervised_keys=None,
70
+ homepage=_HOMEPAGE,
71
+ citation=_CITATION,
72
+ )
73
+
74
+ def _split_generators(self, dl_manager):
75
+ """Returns SplitGenerators."""
76
+ urls_to_download = {
77
+ "train": f"{_URL}{_TRAINING_FILE}",
78
+ "dev": f"{_URL}{_DEV_FILE}",
79
+ "test": f"{_URL}{_TEST_FILE}",
80
+ }
81
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
82
+
83
+ return [
84
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
85
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
86
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
87
+ ]
88
+
89
+ def _generate_examples(self, filepath):
90
+ logger.info("⏳ Generating examples from = %s", filepath)
91
+ with open(filepath, encoding="utf-8") as f:
92
+ guid = 0
93
+ tokens = []
94
+ ner_tags = []
95
+ for line in f:
96
+ if line == "" or line == "\n":
97
+ if tokens:
98
+ print(tokens)
99
+ yield guid, {
100
+ "id": str(guid),
101
+ "tokens": tokens,
102
+ "ner_tags": ner_tags,
103
+ }
104
+ guid += 1
105
+ tokens = []
106
+ ner_tags = []
107
+ else:
108
+ # tokens are tab separated
109
+ splits = line.split("\t")
110
+ tokens.append(splits[0])
111
+ ner_tags.append(splits[1].rstrip())
112
+ # last example
113
+ yield guid, {
114
+ "id": str(guid),
115
+ "tokens": tokens,
116
+ "ner_tags": ner_tags,
117
+ }