ghadeermobasher commited on
Commit
8b89640
·
1 Parent(s): 99757ec

Create CRAFT-Chem.py

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