Skip to content

Commit

Permalink
Merge pull request #207 from emmo-repo/issue183_function_add_class
Browse files Browse the repository at this point in the history
Added function new_entity to ontology
  • Loading branch information
francescalb authored Sep 7, 2021
2 parents a251ddc + 01d4b18 commit c14e793
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
18 changes: 15 additions & 3 deletions emmo/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import warnings
import uuid
import tempfile
import types
from collections import defaultdict

import rdflib
Expand Down Expand Up @@ -1003,14 +1004,25 @@ def addancestors(e, n, s):
return ancestors

def get_wu_palmer_measure(self, cls1, cls2):
'''
Returns the Wu Palmer measure for semantic similarity between
""" Return Wu-Palmer measure for semantic similarity.
Returns Wu-Palmer measure for semantic similarity between
two concepts.
Wu, Palmer; ACL 94: Proceedings of the 32nd annual meeting on
Association for Computational Linguistics, June 1994.
'''
"""
cca = self.closest_common_ancestor(cls1, cls2)
ccadepth = self.number_of_generations(cca, self.Thing)
n1 = self.number_of_generations(cls1, cca)
n2 = self.number_of_generations(cls2, cca)
return 2 * ccadepth / (n1 + n2 + 2 * ccadepth)

def new_entity(self, name, parent):
"""Create and return new entity
Makes a new entity in the ontology with given parent.
Return the new entity
"""
with self:
e = types.new_class(name, (parent, ))
return e
15 changes: 7 additions & 8 deletions emmo/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
onto.imported_ontologies.append(emmo)
onto.base_iri = 'http://emmo.info/examples/test#'

with onto:
# Add entity directly
onto.new_entity('Hydrogen', emmo.Atom)

class Hydrogen(emmo.Atom):
pass
with onto:

# Add entity using python classes
class Oxygen(emmo.Atom):
pass

class H2O(emmo.Molecule):
"""Water molecule."""
emmo.hasSpatialDirectPart.exactly(2, Hydrogen)
emmo.hasSpatialDirectPart.exactly(2, onto.Hydrogen)
emmo.hasSpatialDirectPart.exactly(1, Oxygen)

# Create some
H1 = Hydrogen()
H2 = Hydrogen()
H1 = onto.Hydrogen()
H2 = onto.Hydrogen()
O = Oxygen() # noqa: E741
w = H2O()
w.hasSpatialDirectPart = [H1, H2, O]


onto.sync_attributes(name_policy='sequential', name_prefix='myonto_')
assert onto.base_iri + 'myonto_0' in onto
assert onto.base_iri + 'myonto_6' in onto
Expand All @@ -49,7 +49,6 @@ class H2O(emmo.Molecule):
assert w.name.startswith('onto_')
assert len(w.name) == 5 + 36


# Remove all traces of onto such that they do not mess up other tests
# when running pytest
for e in itertools.chain(onto.classes(), onto.individuals()):
Expand Down

0 comments on commit c14e793

Please sign in to comment.