From 9da48e54a1e787065dbd3fb8b8bf6c0f73dbc633 Mon Sep 17 00:00:00 2001 From: francescalb Date: Fri, 3 Sep 2021 16:47:57 +0200 Subject: [PATCH 1/3] Added function new_entitiy to ontology --- emmo/ontology.py | 11 +++++++++++ emmo/tests/test_basic.py | 15 +++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/emmo/ontology.py b/emmo/ontology.py index 35e3494ab..ede404cdc 100644 --- a/emmo/ontology.py +++ b/emmo/ontology.py @@ -14,6 +14,7 @@ import warnings import uuid import tempfile +import types from collections import defaultdict import rdflib @@ -1014,3 +1015,13 @@ def get_wu_palmer_measure(self, cls1, cls2): 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): + ''' + Makes a new entity in the ontology with given parent. + Return the new entity + ''' + with self: + e = types.new_class(name, (parent, )) + return e + diff --git a/emmo/tests/test_basic.py b/emmo/tests/test_basic.py index 19a03662f..14861d11a 100755 --- a/emmo/tests/test_basic.py +++ b/emmo/tests/test_basic.py @@ -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 @@ -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()): From 4051131578cafe73e79796933a6a09410102d135 Mon Sep 17 00:00:00 2001 From: francescalb Date: Fri, 3 Sep 2021 16:58:46 +0200 Subject: [PATCH 2/3] Empty line --- emmo/ontology.py | 1 - 1 file changed, 1 deletion(-) diff --git a/emmo/ontology.py b/emmo/ontology.py index ede404cdc..48c4a58b2 100644 --- a/emmo/ontology.py +++ b/emmo/ontology.py @@ -1024,4 +1024,3 @@ def new_entity(self, name, parent): with self: e = types.new_class(name, (parent, )) return e - From 01d4b18055fe8265f1a24f9afe4c5d15d8888335 Mon Sep 17 00:00:00 2001 From: francescalb Date: Tue, 7 Sep 2021 08:43:55 +0200 Subject: [PATCH 3/3] Docstring according to pep8 --- emmo/ontology.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/emmo/ontology.py b/emmo/ontology.py index 48c4a58b2..44a5a761f 100644 --- a/emmo/ontology.py +++ b/emmo/ontology.py @@ -1004,12 +1004,13 @@ 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) @@ -1017,10 +1018,11 @@ def get_wu_palmer_measure(self, cls1, cls2): 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