Commit
·
b5d94a4
1
Parent(s):
2eb86fa
Upload bionlp2.py
Browse files- bionlp2.py +119 -0
bionlp2.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" NER dataset compiled by T-NER library https://github.com/asahi417/tner/tree/master/tner """
|
2 |
+
import json
|
3 |
+
from itertools import chain
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
logger = datasets.logging.get_logger(__name__)
|
7 |
+
_DESCRIPTION = """[BioNLP2004 NER dataset](https://aclanthology.org/W04-1213.pdf)"""
|
8 |
+
_NAME = "bionlp"
|
9 |
+
_VERSION = "1.0.0"
|
10 |
+
_CITATION = """
|
11 |
+
@inproceedings{collier-kim-2004-introduction,
|
12 |
+
title = "Introduction to the Bio-entity Recognition Task at {JNLPBA}",
|
13 |
+
author = "Collier, Nigel and
|
14 |
+
Kim, Jin-Dong",
|
15 |
+
booktitle = "Proceedings of the International Joint Workshop on Natural Language Processing in Biomedicine and its Applications ({NLPBA}/{B}io{NLP})",
|
16 |
+
month = aug # " 28th and 29th",
|
17 |
+
year = "2004",
|
18 |
+
address = "Geneva, Switzerland",
|
19 |
+
publisher = "COLING",
|
20 |
+
url = "https://aclanthology.org/W04-1213",
|
21 |
+
pages = "73--78",
|
22 |
+
}
|
23 |
+
https://huggingface.co/datasets/chintagunta85/bionlp/raw/main/test_bionlp.json
|
24 |
+
"""
|
25 |
+
|
26 |
+
_HOME_PAGE = "https://huggingface.co/datasets/chintagunta85"
|
27 |
+
# https://huggingface.co/datasets/chintagunta85/bionlp/raw/main/train_bionlp.json
|
28 |
+
_URL = f'https://huggingface.co/datasets/chintagunta85/{_NAME}/raw/main'
|
29 |
+
_URLS = {
|
30 |
+
str(datasets.Split.TEST): [f'{_URL}/test_bionlp.json'],
|
31 |
+
str(datasets.Split.TRAIN): [f'{_URL}/train_bionlp.json'],
|
32 |
+
str(datasets.Split.VALIDATION): [f'{_URL}/valid_bionlp.json'],
|
33 |
+
}
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
def map_ner_tags(tlist):
|
38 |
+
nlist=[]
|
39 |
+
for indx in tlist:
|
40 |
+
#if(inv_map[indx]):
|
41 |
+
# print(inv_map[indx], custom_names.index(inv_map[indx]), indx)
|
42 |
+
nlist.append(custom_names.index(inv_map[indx]))
|
43 |
+
return nlist
|
44 |
+
|
45 |
+
class BioNLP2004Config(datasets.BuilderConfig):
|
46 |
+
"""BuilderConfig"""
|
47 |
+
|
48 |
+
def __init__(self, **kwargs):
|
49 |
+
"""BuilderConfig.
|
50 |
+
|
51 |
+
Args:
|
52 |
+
**kwargs: keyword arguments forwarded to super.
|
53 |
+
"""
|
54 |
+
super(BioNLP2004Config, self).__init__(**kwargs)
|
55 |
+
|
56 |
+
|
57 |
+
class BioNLP2004(datasets.GeneratorBasedBuilder):
|
58 |
+
"""Dataset."""
|
59 |
+
|
60 |
+
BUILDER_CONFIGS = [
|
61 |
+
BioNLP2004Config(name=_NAME, version=datasets.Version(_VERSION), description=_DESCRIPTION),
|
62 |
+
]
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
def _split_generators(self, dl_manager):
|
67 |
+
downloaded_file = dl_manager.download_and_extract(_URLS)
|
68 |
+
return [datasets.SplitGenerator(name=i, gen_kwargs={"filepaths": downloaded_file[str(i)]})
|
69 |
+
for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]
|
70 |
+
|
71 |
+
def _generate_examples(self, filepaths):
|
72 |
+
custom_names = ['O','B-GENE','I-GENE','B-CHEMICAL','I-CHEMICAL','B-DISEASE','I-DISEASE',
|
73 |
+
'B-DNA', 'I-DNA', 'B-RNA', 'I-RNA', 'B-CELL_LINE', 'I-CELL_LINE', 'B-CELL_TYPE', 'I-CELL_TYPE',
|
74 |
+
'B-PROTEIN', 'I-PROTEIN', 'B-SPECIES', 'I-SPECIES']
|
75 |
+
|
76 |
+
pre_def = {"O": 0, "B-DNA": 1, "I-DNA": 2, "B-PROTEIN": 3, "I-PROTEIN": 4,
|
77 |
+
"B-CELL_TYPE": 5, "I-CELL_TYPE": 6, "B-CELL_LINE": 7, "I-CELL_LINE": 8,
|
78 |
+
"B-RNA": 9, "I-RNA": 10}
|
79 |
+
inv_map = {0: 'O', 1: 'B-DNA', 2: 'I-DNA', 3: 'B-PROTEIN', 4: 'I-PROTEIN',
|
80 |
+
5: 'B-CELL_TYPE', 6: 'I-CELL_TYPE', 7: 'B-CELL_LINE', 8: 'I-CELL_LINE', 9: 'B-RNA', 10: 'I-RNA'}
|
81 |
+
|
82 |
+
_key = 0
|
83 |
+
for filepath in filepaths:
|
84 |
+
logger.info(f"generating examples from = {filepath}")
|
85 |
+
with open(filepath, encoding="utf-8") as f:
|
86 |
+
_list = [i for i in f.read().split('\n') if len(i) > 0]
|
87 |
+
for i in _list:
|
88 |
+
data = json.loads(i)
|
89 |
+
#print(data)
|
90 |
+
|
91 |
+
nlist = []
|
92 |
+
for indx in data["ner_tags"]:
|
93 |
+
nlist.append(custom_names.index(inv_map[indx]))
|
94 |
+
#data['ner_tags'] = map_ner_tags(data['ner_tags'])
|
95 |
+
data["ner_tags"]=nlist
|
96 |
+
yield _key, data
|
97 |
+
_key += 1
|
98 |
+
|
99 |
+
def _info(self):
|
100 |
+
custom_names = ['O','B-GENE','I-GENE','B-CHEMICAL','I-CHEMICAL','B-DISEASE','I-DISEASE',
|
101 |
+
'B-DNA', 'I-DNA', 'B-RNA', 'I-RNA', 'B-CELL_LINE', 'I-CELL_LINE', 'B-CELL_TYPE', 'I-CELL_TYPE',
|
102 |
+
'B-PROTEIN', 'I-PROTEIN', 'B-SPECIES', 'I-SPECIES']
|
103 |
+
return datasets.DatasetInfo(
|
104 |
+
description=_DESCRIPTION,
|
105 |
+
features=datasets.Features(
|
106 |
+
{
|
107 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
108 |
+
"tags": datasets.Sequence(datasets.Value("int32")),
|
109 |
+
"ner_tags": datasets.Sequence(
|
110 |
+
datasets.features.ClassLabel(
|
111 |
+
names=custom_names
|
112 |
+
)
|
113 |
+
),
|
114 |
+
}
|
115 |
+
),
|
116 |
+
supervised_keys=None,
|
117 |
+
homepage=_HOME_PAGE,
|
118 |
+
citation=_CITATION,
|
119 |
+
)
|