def render_func(entity):
    name = entity.label[0] if len(entity.label) == 1 else entity.name
    return "%s.%s" % (entity.namespace.name, name)

    # def render_using_label(entity):
    #     return entity.label.first() or entity.name

    set_render_func(render_func)
class DataRemapper:
    def __init__(self, values_dict):
        self.values_dict = values_dict

    def __call__(self, value):
        return self.values_dict[value]


_PT_UNCERTAIN = 'UNCERTAIN'
_PT_TRUE = 'TRUE'
_PT_FALSE = 'FALSE'

default_remapper = DataRemapper({_PT_TRUE: 1, _PT_FALSE: 0, _PT_UNCERTAIN: 0.5})
# sets the global render func
or2.set_render_func(render_colon)


class OntologyManager:
    def __init__(self, path):
        # loads an ontology and stores classes and individuals in two dictionaries
        _onto = or2.get_ontology(path).load()
        _classes = {str(c): c for c in _onto.classes()}
        _individuals = {str(i): i for i in _onto.individuals()}

        print("Loaded ontology:", _onto.name)
        print("Classes:", len(_classes.keys()))
        print("Individuals:", len(_individuals.keys()))

        # saves the variables in the object
        self.onto = _onto
Ejemplo n.º 3
0

# Improve default rendering of entities
def render_func(entity):
    if hasattr(entity, 'prefLabel') and entity.prefLabel:
        name = entity.prefLabel[0]
    elif hasattr(entity, 'label') and entity.label:
        name = entity.label[0]
    elif hasattr(entity, 'altLabel') and entity.altLabel:
        name = entity.altLabel[0]
    else:
        name = entity.name
    return "%s.%s" % (entity.namespace.name, name)


owlready2.set_render_func(render_func)


#
# Extending ThingClass (classes)
# ==============================
def get_preferred_label(self):
    """Returns the preferred label as a string (not list).

    The following heuristics is used:
      - if prefLabel annotation property exists, returns the first prefLabel
      - if label annotation property exists, returns the first label
      - otherwise return the name
    """
    if hasattr(self, 'prefLabel') and self.prefLabel:
        return self.prefLabel[0]
# import emmo
# from emmo import owldir


class NoSuchLabelError(LookupError):
    """Error raised when a label cannot be found."""
    pass


def render_func(entity):
    name = entity.label[0] if len(entity.label) == 1 else entity.name
    return "%s.%s" % (entity.namespace.name, name)


set_render_func(render_func)

# def render_using_label(entity):
#     return entity.label.first() or entity.name
# set_render_func(render_using_label)

# owl types
categories = (
    'annotation_properties',
    'data_properties',
    'object_properties',
    'classes',
    'individuals',
    # 'properties',
)
Ejemplo n.º 5
0
    ObjectProperty,
    DataProperty,
)

# noinspection PyUnresolvedReferences
from ipydex import IPS, activate_ips_on_exception

activate_ips_on_exception()


def render_using_label(entity):
    repr_str1 = entity.label.first() or entity.name
    return f"<{type(entity).name} '{repr_str1}'>"


set_render_func(render_using_label)


class Container(object):
    def __init__(self, arg=None, **data_dict):
        if isinstance(arg, dict) and not data_dict:
            data_dict = arg
        self.__dict__.update(data_dict)


class Ontology(object):
    def __init__(self, fpath, world=None):
        """

        :param fpath:   path of the yaml-file containing the ontology
        :param world:   owl2 world object holding all the RDF-data (default: None)