chintagunta85 commited on
Commit
5003871
1 Parent(s): fc206af

Upload linnaeus.py

Browse files
Files changed (1) hide show
  1. linnaeus.py +143 -0
linnaeus.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 HuggingFace Datasets Authors.
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
+
16
+ # Lint as: python3
17
+ """LINNAEUS: A species name identification system for biomedical literature"""
18
+
19
+ import os
20
+
21
+ import datasets
22
+
23
+
24
+ logger = datasets.logging.get_logger(__name__)
25
+
26
+
27
+ _CITATION = """\
28
+ @article{gerner2010linnaeus,
29
+ title={LINNAEUS: a species name identification system for biomedical literature},
30
+ author={Gerner, Martin and Nenadic, Goran and Bergman, Casey M},
31
+ journal={BMC bioinformatics},
32
+ volume={11},
33
+ number={1},
34
+ pages={85},
35
+ year={2010},
36
+ publisher={Springer}
37
+ }
38
+ """
39
+
40
+ _DESCRIPTION = """\
41
+ A novel corpus of full-text documents manually annotated for species mentions.
42
+ """
43
+
44
+ _HOMEPAGE = "http://linnaeus.sourceforge.net/"
45
+ _URL = "https://drive.google.com/u/0/uc?id=1OletxmPYNkz2ltOr9pyT0b0iBtUWxslh&export=download"
46
+ _BIOBERT_NER_DATASET_DIRECTORY = "linnaeus"
47
+ _TRAINING_FILE = "train.tsv"
48
+ _DEV_FILE = "devel.tsv"
49
+ _TEST_FILE = "test.tsv"
50
+
51
+
52
+ class LinnaeusConfig(datasets.BuilderConfig):
53
+ """BuilderConfig for Linnaeus"""
54
+
55
+ def __init__(self, **kwargs):
56
+ """BuilderConfig for Linnaeus.
57
+ Args:
58
+ **kwargs: keyword arguments forwarded to super.
59
+ """
60
+ super(LinnaeusConfig, self).__init__(**kwargs)
61
+
62
+
63
+ class Linnaeus(datasets.GeneratorBasedBuilder):
64
+ """Linnaeus dataset."""
65
+
66
+ BUILDER_CONFIGS = [
67
+ LinnaeusConfig(name="linnaeus", version=datasets.Version("1.0.0"), description="Linnaeus dataset"),
68
+ ]
69
+
70
+ def _info(self):
71
+ custom_names = ['O','B-GENE','I-GENE','B-CHEMICAL','I-CHEMICAL','B-DISEASE','I-DISEASE',
72
+ 'B-DNA', 'I-DNA', 'B-RNA', 'I-RNA', 'B-CELL_LINE', 'I-CELL_LINE', 'B-CELL_TYPE', 'I-CELL_TYPE',
73
+ 'B-PROTEIN', 'I-PROTEIN', 'B-SPECIES', 'I-SPECIES']
74
+ return datasets.DatasetInfo(
75
+ description=_DESCRIPTION,
76
+ features=datasets.Features(
77
+ {
78
+ "id": datasets.Value("string"),
79
+ "tokens": datasets.Sequence(datasets.Value("string")),
80
+ "ner_tags": datasets.Sequence(
81
+ datasets.features.ClassLabel(
82
+ names=custom_names
83
+ )
84
+ ),
85
+ }
86
+ ),
87
+ supervised_keys=None,
88
+ homepage=_HOMEPAGE,
89
+ citation=_CITATION,
90
+ )
91
+
92
+ def _split_generators(self, dl_manager):
93
+ """Returns SplitGenerators."""
94
+ urls_to_download = {
95
+ "biobert_ner_datasets": _URL,
96
+ }
97
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
98
+ dataset_directory = os.path.join(downloaded_files["biobert_ner_datasets"], _BIOBERT_NER_DATASET_DIRECTORY)
99
+ return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(dataset_directory, _TRAINING_FILE)}
102
+ ),
103
+ datasets.SplitGenerator(
104
+ name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(dataset_directory, _DEV_FILE)}
105
+ ),
106
+ datasets.SplitGenerator(
107
+ name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(dataset_directory, _TEST_FILE)}
108
+ ),
109
+ ]
110
+
111
+ def _generate_examples(self, filepath):
112
+ logger.info("⏳ Generating examples from = %s", filepath)
113
+ with open(filepath, encoding="utf-8") as f:
114
+ guid = 0
115
+ tokens = []
116
+ ner_tags = []
117
+ for line in f:
118
+ if line == "" or line == "\n":
119
+ if tokens:
120
+ yield guid, {
121
+ "id": str(guid),
122
+ "tokens": tokens,
123
+ "ner_tags": ner_tags,
124
+ }
125
+ guid += 1
126
+ tokens = []
127
+ ner_tags = []
128
+ else:
129
+ # tokens are tab separated
130
+ splits = line.split("\t")
131
+ tokens.append(splits[0])
132
+ if(splits[1].rstrip()=="B"):
133
+ ner_tags.append("B-SPECIES")
134
+ elif(splits[1].rstrip()=="I"):
135
+ ner_tags.append("I-SPECIES")
136
+ else:
137
+ ner_tags.append(splits[1].rstrip())
138
+ # last example
139
+ yield guid, {
140
+ "id": str(guid),
141
+ "tokens": tokens,
142
+ "ner_tags": ner_tags,
143
+ }