Ontology Taxonomy
Extracting the taxonomy from an ontology often comes in handy for graph-based machine learning techniques. Here we provide a basic Taxonomy
class built upon networkx.DiGraph
where nodes represent entities and edges represent subsumptions. We then provide the OntologyTaxonomy
class that extends the basic Taxonomy
. It utilises the simple structural reasoner to enrich the ontology subsumptions beyond asserted ones, and build the taxonomy over the expanded subsumptions. Each node represents a named class and has a label (rdfs:label
) attribute. The root node owl:Thing
is also specified for functions like counting the node depths, etc. Moreover, we provide the WordnetTaxonomy
class that wraps the WordNet knowledge graph for easier access.
Note
It is also possible to use OntologyProjector
to extract triples from the ontology as edges of the taxonomy. We will consider this feature in the future.
Taxonomy(edges, root_node=None)
Class for building the taxonomy over structured data.
Attributes:
Name | Type | Description |
---|---|---|
nodes |
list
|
A list of entity ids. |
edges |
list
|
A list of |
graph |
networkx.DiGraph
|
A directed graph that represents the taxonomy. |
root_node |
Optional[str]
|
Optional root node id. Defaults to |
Source code in src/deeponto/onto/taxonomy.py
43 44 45 46 47 |
|
get_node_attributes(entity_id)
Get the attributes of the given entity.
Source code in src/deeponto/onto/taxonomy.py
49 50 51 |
|
get_children(entity_id, apply_transitivity=False)
Get the set of children for a given entity.
Source code in src/deeponto/onto/taxonomy.py
53 54 55 56 57 58 |
|
get_parents(entity_id, apply_transitivity=False)
Get the set of parents for a given entity.
Source code in src/deeponto/onto/taxonomy.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
get_descendant_graph(entity_id)
Create a descendant graph (networkx.DiGraph
) for a given entity.
Source code in src/deeponto/onto/taxonomy.py
76 77 78 79 |
|
get_shortest_node_depth(entity_id)
Get the shortest depth of the given entity in the taxonomy.
Source code in src/deeponto/onto/taxonomy.py
81 82 83 84 85 |
|
get_longest_node_depth(entity_id)
Get the longest depth of the given entity in the taxonomy.
Source code in src/deeponto/onto/taxonomy.py
87 88 89 90 91 |
|
get_lowest_common_ancestor(entity_id1, entity_id2)
Get the lowest common ancestor of the given two entities.
Source code in src/deeponto/onto/taxonomy.py
93 94 95 |
|
OntologyTaxonomy(onto, reasoner_type='struct')
Bases: Taxonomy
Class for building the taxonomy (top-down subsumption graph) from an ontology.
The nodes of this graph are named classes only, but the hierarchy is enriched (beyond asserted axioms) by an ontology reasoner.
Attributes:
Name | Type | Description |
---|---|---|
onto |
Ontology
|
The input ontology to build the taxonomy. |
reasoner_type |
str
|
The type of reasoner used. Defaults to |
reasoner |
OntologyReasoner
|
An ontology reasoner used for completing the hierarchy.
If the |
root_node |
str
|
The root node that represents |
nodes |
list
|
A list of named class IRIs. |
edges |
list
|
A list of |
graph |
networkx.DiGraph
|
A directed subsumption graph. |
Source code in src/deeponto/onto/taxonomy.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
get_parents(class_iri, apply_transitivity=False)
Get the set of parents for a given class.
It is worth noting that this method with transitivity applied can be deemed as simple structural reasoning.
For more advanced logical reasoning, use the DL reasoner self.onto.reasoner
instead.
Source code in src/deeponto/onto/taxonomy.py
144 145 146 147 148 149 150 |
|
get_children(class_iri, apply_transitivity=False)
Get the set of children for a given class.
It is worth noting that this method with transitivity applied can be deemed as simple structural reasoning.
For more advanced logical reasoning, use the DL reasoner self.onto.reasoner
instead.
Source code in src/deeponto/onto/taxonomy.py
152 153 154 155 156 157 158 |
|
get_descendant_graph(class_iri)
Create a descendant graph (networkx.DiGraph
) for a given ontology class.
Source code in src/deeponto/onto/taxonomy.py
160 161 162 |
|
get_shortest_node_depth(class_iri)
Get the shortest depth of the given named class in the taxonomy.
Source code in src/deeponto/onto/taxonomy.py
164 165 166 |
|
get_longest_node_depth(class_iri)
Get the longest depth of the given named class in the taxonomy.
Source code in src/deeponto/onto/taxonomy.py
168 169 170 |
|
get_lowest_common_ancestor(class_iri1, class_iri2)
Get the lowest common ancestor of the given two named classes.
Source code in src/deeponto/onto/taxonomy.py
172 173 174 |
|
WordnetTaxonomy(pos='n', relation='subsumption')
Bases: Taxonomy
Class for the building the taxonomy (subsumption
, membership
, or part-of
relations) from wordnet.
Attributes:
Name | Type | Description |
---|---|---|
pos |
str
|
The pos-tag of entities to be extracted from wordnet. |
nodes |
list
|
A list of entity ids extracted from wordnet. |
edges |
list
|
A list of |
graph |
networkx.DiGraph
|
A directed hypernym graph. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos |
str
|
The pos-tag of entities to be extracted from wordnet. |
'n'
|
relation |
str
|
The hierarchical relation for this taxonomy. Options are |
'subsumption'
|
Source code in src/deeponto/onto/taxonomy.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
fetch_synsets(pos='n')
staticmethod
Get synsets of certain pos-tag from wordnet.
Source code in src/deeponto/onto/taxonomy.py
212 213 214 215 216 217 218 219 220 |
|
fetch_subsumptions(synsets)
staticmethod
Get subsumption (hypernym-hyponym) pairs from a given set of wordnet synsets.
Source code in src/deeponto/onto/taxonomy.py
222 223 224 225 226 227 228 229 230 |
|
fetch_memberships(synsets)
staticmethod
Get membership (instance hypernym-hyponym) pairs from a given set of wordnet synsets.
Source code in src/deeponto/onto/taxonomy.py
232 233 234 235 236 237 238 239 240 |
|
fetch_parts(synsets)
staticmethod
Get has-part (holonym-meronym) pairs from a given set of wordnet synsets.
Source code in src/deeponto/onto/taxonomy.py
242 243 244 245 246 247 248 249 250 |
|
TaxonomyNegativeSampler(taxonomy, entity_weights=None)
Class for the efficient negative sampling with buffer over the taxonomy.
Attributes:
Name | Type | Description |
---|---|---|
taxonomy |
str
|
The taxonomy for negative sampling. |
entity_weights |
Optional[dict]
|
A dictionary with the taxonomy entities as keys and their corresponding weights as values. Defaults to |
Source code in src/deeponto/onto/taxonomy.py
261 262 263 264 265 266 267 268 269 270 271 272 |
|
fill(buffer_size=None)
Buffer a large collection of entities sampled with replacement for faster negative sampling.
Source code in src/deeponto/onto/taxonomy.py
274 275 276 277 278 279 280 |
|
sample(entity_id, n_samples, buffer_size=None)
Sample N negative samples for a given entity with replacement.
Source code in src/deeponto/onto/taxonomy.py
282 283 284 285 286 287 288 289 290 291 |
|
Created: September 29, 2023