chintagunta85 commited on
Commit
1c90f86
1 Parent(s): 346ae68

Upload pv_dataset.py

Browse files
Files changed (1) hide show
  1. pv_dataset.py +112 -0
pv_dataset.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import json
3
+
4
+ logger = datasets.logging.get_logger(__name__)
5
+
6
+
7
+ _HOMEPAGE = "https://www.google.com"
8
+ _TRAINING_FILE = "pv_train.tsv"
9
+ _DEV_FILE = "pv_val.tsv"
10
+ _TEST_FILE = "pv_test.tsv"
11
+
12
+
13
+ class PVDatasetConfig(datasets.BuilderConfig):
14
+ """BuilderConfig for Bc2gmCorpus"""
15
+
16
+ def __init__(self, **kwargs):
17
+ """BuilderConfig for Bc2gmCorpus.
18
+ Args:
19
+ **kwargs: keyword arguments forwarded to super.
20
+ """
21
+ super(PVDatasetConfig, self).__init__(**kwargs)
22
+
23
+ class PVDataset(datasets.GeneratorBasedBuilder):
24
+ """Bc2gmCorpus dataset."""
25
+
26
+ BUILDER_CONFIGS = [
27
+ PVDatasetConfig(name="PVDatasetCorpus", version=datasets.Version("1.0.0"), description="PVDataset"),
28
+ ]
29
+
30
+ def _info(self):
31
+
32
+ custom_names = ['O','B-GENE','I-GENE','B-CHEMICAL','I-CHEMICAL','B-DISEASE','I-DISEASE','B-SPECIES', 'I-SPECIES']
33
+
34
+ return datasets.DatasetInfo(
35
+ description='abhi',
36
+ features=datasets.Features(
37
+ {
38
+ "id": datasets.Value("string"),
39
+ "tokens": datasets.Sequence(datasets.Value("string")),
40
+ "ner_tags": datasets.Sequence(
41
+ datasets.features.ClassLabel(
42
+ names=custom_names
43
+ )
44
+ ),
45
+ }
46
+ ),
47
+ supervised_keys=None,
48
+ homepage=_HOMEPAGE,
49
+ citation='cite me',
50
+ )
51
+
52
+ def _split_generators(self, dl_manager):
53
+ """Returns SplitGenerators."""
54
+ urls_to_download = {
55
+ "train": f"{_TRAINING_FILE}",
56
+ "dev": f"{_DEV_FILE}",
57
+ "test": f"{_TEST_FILE}",
58
+ }
59
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
60
+
61
+ return [
62
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
63
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
64
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
65
+ ]
66
+
67
+ def _generate_examples(self, filepath):
68
+ shift = 0
69
+ logger.info("⏳ Generating examples from = %s", filepath)
70
+ with open(filepath, encoding="utf-8") as f:
71
+ guid = 0
72
+ tokens = []
73
+ ner_tags = []
74
+ for line in f:
75
+ if line == "" or line == "\n":
76
+ if tokens:
77
+ yield guid, {
78
+ "id": str(guid),
79
+ "tokens": tokens,
80
+ "ner_tags": ner_tags,
81
+ }
82
+ guid += 1
83
+ tokens = []
84
+ ner_tags = []
85
+ else:
86
+ # tokens are tab separated
87
+ splits = line.split("\t")
88
+ tokens.append(splits[0])
89
+ if(splits[1].rstrip()=="B"):
90
+ ner_tags.append("B-SPECIES")
91
+ elif(splits[1].rstrip()=="I"):
92
+ ner_tags.append("I-SPECIES")
93
+ elif(splits[1].rstrip()=="B-Chemical"):
94
+ ner_tags.append("B-CHEMICAL")
95
+ elif(splits[1].rstrip()=="I-Chemical"):
96
+ ner_tags.append("I-CHEMICAL")
97
+ elif(splits[1].rstrip()=="B-Disease"):
98
+ ner_tags.append("B-DISEASE")
99
+ elif(splits[1].rstrip()=="I-Disease"):
100
+ ner_tags.append("I-DISEASE")
101
+ else:
102
+ ner_tags.append(splits[1].rstrip())
103
+ # last example
104
+ yield guid, {
105
+ "id": str(guid),
106
+ "tokens": tokens,
107
+ "ner_tags": ner_tags,
108
+ }
109
+
110
+
111
+
112
+