lawhy commited on
Commit
e86e346
1 Parent(s): 249e6a1

Create ontolama.py

Browse files
Files changed (1) hide show
  1. ontolama.py +189 -0
ontolama.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """OntoLAMA Dataset Loading Script"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # TODO: Add BibTeX citation
26
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ @article{he2023language,
29
+ title={Language Model Analysis for Ontology Subsumption Inference},
30
+ author={He, Yuan and Chen, Jiaoyan and Jim{\'e}nez-Ruiz, Ernesto and Dong, Hang and Horrocks, Ian},
31
+ journal={arXiv preprint arXiv:2302.06761},
32
+ year={2023}
33
+ }
34
+ """
35
+
36
+ # TODO: Add description of the dataset here
37
+ # You can copy an official description
38
+ _DESCRIPTION = """\
39
+ OntoLAMA: LAnguage Model Analysis datasets for Ontology Subsumption Inference.
40
+ """
41
+
42
+ _URL = "https://huggingface.co/datasets/krr-oxford/OntoLAMA/resolve/main/data/"
43
+
44
+ # TODO: Add a link to an official homepage for the dataset here
45
+ _HOMEPAGE = "https://krr-oxford.github.io/DeepOnto/"
46
+
47
+ # TODO: Add the licence for the dataset here if you can find it
48
+ _LICENSE = "Apache License, Version 2.0"
49
+
50
+
51
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
52
+ class OntoLAMA(datasets.GeneratorBasedBuilder):
53
+ """TODO: Short description of my dataset.
54
+ """
55
+
56
+ VERSION = datasets.Version("1.0")
57
+
58
+ # This is an example of a dataset with multiple configurations.
59
+ # If you don't want/need to define several sub-sets in your dataset,
60
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
61
+
62
+ # If you need to make complex sub-parts in the datasets with configurable options
63
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
64
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
65
+
66
+ # You will be able to load one or the other configurations in the following list with
67
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
68
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
69
+ BUILDER_CONFIGS = [
70
+ datasets.BuilderConfig(
71
+ name="schemaorg-atomic-SI",
72
+ version=VERSION,
73
+ description="Atomic SI dataset created from the Schema.org Ontology."
74
+ ),
75
+ datasets.BuilderConfig(
76
+ name="doid-atomic-SI",
77
+ version=VERSION,
78
+ description="Atomic SI dataset created from the Disease Ontology."
79
+ ),
80
+ datasets.BuilderConfig(
81
+ name="foodon-atomic-SI",
82
+ version=VERSION,
83
+ description="Atomic SI dataset created from the Food Ontology."
84
+ ),
85
+ datasets.BuilderConfig(
86
+ name="go-atomic-SI",
87
+ version=VERSION,
88
+ description="Atomic SI dataset created from Gene Ontology."
89
+ ),
90
+ ]
91
+
92
+ def _info(self):
93
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
94
+ if "atomic-SI" in self.config.name: # This is the name of the configuration selected in BUILDER_CONFIGS above
95
+ features = datasets.Features(
96
+ {
97
+ "v_sub_concept": datasets.Value("string"),
98
+ "v_super_concept": datasets.Value("string"),
99
+ 'label': datasets.ClassLabel(num_classes=2, names=['negative_subsumption', 'positive_subsumption'], names_file=None, id=None),
100
+ "axiom": datasets.Value("string"),
101
+ # These are the features of your dataset like images, labels ...
102
+ }
103
+ )
104
+ elif "complex-SI" in self.config.name: # This is an example to show how to have different features for "first_domain" and "second_domain"
105
+ features = datasets.Features(
106
+ {
107
+ "v_sub_concept": datasets.Value("string"),
108
+ "v_super_concept": datasets.Value("string"),
109
+ 'label': datasets.ClassLabel(num_classes=2, names=['negative_subsumption', 'positive_subsumption'], names_file=None, id=None),
110
+ "axiom": datasets.Value("string"),
111
+ "anchor_axiom": datasets.Value("string") # the equivalence axiom used as anchor
112
+ # These are the features of your dataset like images, labels ...
113
+ }
114
+ )
115
+ return datasets.DatasetInfo(
116
+ # This is the description that will appear on the datasets page.
117
+ description=_DESCRIPTION,
118
+ # This defines the different columns of the dataset and their types
119
+ features=features, # Here we define them above because they are different between the two configurations
120
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
121
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
122
+ # supervised_keys=("sentence", "label"),
123
+ # Homepage of the dataset for documentation
124
+ homepage=_HOMEPAGE,
125
+ # License for the dataset if available
126
+ license=_LICENSE,
127
+ # Citation for the dataset
128
+ citation=_CITATION,
129
+ )
130
+
131
+ def _split_generators(self, dl_manager):
132
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
133
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
134
+
135
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
136
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
137
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
138
+ urls = os.path.join(_URL, self.config.name)
139
+ data_dir = dl_manager.download_and_extract(urls)
140
+ return [
141
+ datasets.SplitGenerator(
142
+ name=datasets.Split.TRAIN,
143
+ # These kwargs will be passed to _generate_examples
144
+ gen_kwargs={
145
+ "filepath": os.path.join(data_dir, "train.jsonl"),
146
+ "split": "train",
147
+ },
148
+ ),
149
+ datasets.SplitGenerator(
150
+ name=datasets.Split.VALIDATION,
151
+ # These kwargs will be passed to _generate_examples
152
+ gen_kwargs={
153
+ "filepath": os.path.join(data_dir, "dev.jsonl"),
154
+ "split": "dev",
155
+ },
156
+ ),
157
+ datasets.SplitGenerator(
158
+ name=datasets.Split.TEST,
159
+ # These kwargs will be passed to _generate_examples
160
+ gen_kwargs={
161
+ "filepath": os.path.join(data_dir, "test.jsonl"),
162
+ "split": "test"
163
+ },
164
+ ),
165
+ ]
166
+
167
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
168
+ def _generate_examples(self, filepath, split):
169
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
170
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
171
+ with open(filepath, encoding="utf-8") as f:
172
+ for key, row in enumerate(f):
173
+ data = json.loads(row)
174
+ if "atomic-SI" in self.config.name:
175
+ # Yields examples as (key, example) tuples
176
+ yield key, {
177
+ "v_sub_concept": data["v_sub_concept"],
178
+ "v_super_concept": data["v_super_concept"],
179
+ "label": data["label"],
180
+ "axiom": data["axiom"],
181
+ }
182
+ elif "complex-SI" in self.config.name:
183
+ yield key, {
184
+ "v_sub_concept": data["v_sub_concept"],
185
+ "v_super_concept": data["v_super_concept"],
186
+ "label": data["label"],
187
+ "axiom": data["axiom"],
188
+ "anchor_axiom": data["anchor_axiom"],
189
+ }