Improve code for working with an ontology
This commit is contained in:
parent
8312150a62
commit
3eb01b064c
@ -1,58 +1,61 @@
|
|||||||
from typing import List
|
from typing import List, Set
|
||||||
|
|
||||||
from ordered_set import OrderedSet
|
|
||||||
from owlready2 import get_ontology, Ontology
|
from owlready2 import get_ontology, Ontology
|
||||||
|
|
||||||
|
|
||||||
class MyOntology:
|
class MyOntology:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.__onto: Ontology = get_ontology("file://./new-ontology.owx").load()
|
self.__onto: Ontology = get_ontology("file://./new-ontology.owx").load()
|
||||||
|
self.__concept = self.__onto.Concept
|
||||||
|
|
||||||
def __get_parent_with_hierarchy(self, root, parents):
|
def __get_concept_level(self, concept) -> int:
|
||||||
pdict = {}
|
level = 1
|
||||||
for parent in parents:
|
parent = concept.is_a[0]
|
||||||
level = 0
|
while parent != self.__concept:
|
||||||
subclass = parent.is_a[0]
|
|
||||||
if subclass == self.__onto.Concept and len(pdict.keys()) == 0:
|
|
||||||
pdict[1] = [parent]
|
|
||||||
while subclass != self.__onto.Concept:
|
|
||||||
level += 1
|
level += 1
|
||||||
if subclass == root:
|
parent = parent.is_a[0]
|
||||||
if pdict.get(level) is None:
|
return level
|
||||||
pdict[level] = []
|
|
||||||
pdict[level].append(parent)
|
|
||||||
subclass = subclass.is_a[0]
|
|
||||||
keys = sorted(pdict.keys())
|
|
||||||
if len(keys) == 0:
|
|
||||||
return None, None
|
|
||||||
return keys[-1], pdict[keys[-1]]
|
|
||||||
|
|
||||||
def __find_instance(self, root, level: int, terms: List[str]) -> OrderedSet[(int, [])]:
|
@staticmethod
|
||||||
level += 1
|
def __get_instances(root, term: str) -> Set:
|
||||||
instances: OrderedSet[(int, [])] = OrderedSet()
|
instances: Set = set()
|
||||||
for current_class in root.subclasses():
|
for instance in root.instances():
|
||||||
for instance in current_class.instances():
|
if instance.name == term:
|
||||||
if instance.name == terms[-1] is not None:
|
instances.add(instance)
|
||||||
plevel, parents = self.__get_parent_with_hierarchy(current_class, instance.is_instance_of)
|
|
||||||
filtered_terms = list(filter(lambda term: term != terms[-1], terms))
|
|
||||||
if parents is None:
|
|
||||||
plevel = level
|
|
||||||
parents = [current_class]
|
|
||||||
if len(filtered_terms) == 0:
|
|
||||||
instances.append((plevel, current_class))
|
|
||||||
return instances
|
return instances
|
||||||
for parent in parents:
|
|
||||||
result = self.__find_instance(parent, plevel, filtered_terms)
|
def __get_concepts(self, instances: Set) -> Set:
|
||||||
if len(result) == 0:
|
concepts: Set = set()
|
||||||
instances.append((plevel, parent))
|
for instance in instances:
|
||||||
|
for concept in instance.is_instance_of:
|
||||||
|
concepts.add((self.__get_concept_level(concept), concept))
|
||||||
|
return concepts
|
||||||
|
|
||||||
|
def make(self, root_class: [], terms: List[str], is_empty=True) -> Set:
|
||||||
|
if not isinstance(terms, list) or not isinstance(root_class, list):
|
||||||
|
return set()
|
||||||
|
if len(terms) == 0 or len(root_class) == 0:
|
||||||
|
return set()
|
||||||
|
concepts: Set = set()
|
||||||
|
for concept in root_class:
|
||||||
|
term: str = terms[-1]
|
||||||
|
my_terms: List[str] = list(filter(lambda item: item != term, terms))
|
||||||
|
instances: Set = self.__get_instances(concept, term)
|
||||||
|
result = self.__get_concepts(instances)
|
||||||
|
concepts.update(result)
|
||||||
|
new_root: [] = []
|
||||||
|
is_empty = is_empty and len(concepts) == 0
|
||||||
|
if len(result) == 0 and is_empty:
|
||||||
|
new_root.append(self.__concept)
|
||||||
else:
|
else:
|
||||||
[instances.append(item) for item in result]
|
new_root.extend(list(map(lambda item: item[1], result)))
|
||||||
return instances
|
concepts.update(self.make(new_root, my_terms, is_empty))
|
||||||
|
return concepts
|
||||||
|
|
||||||
def get_event_description(self, terms: List[str]) -> str:
|
def get_events(self, terms: List[str]) -> str:
|
||||||
instances = OrderedSet()
|
concepts: Set = set()
|
||||||
for term in terms:
|
for term in terms:
|
||||||
for item in self.__find_instance(self.__onto.Concept, 0, term.split(' ')):
|
concepts.update(self.make([self.__concept], term.split(' ')))
|
||||||
instances.append(item)
|
events = sorted(list(
|
||||||
instances = sorted(list(instances), reverse=True)
|
map(lambda concept: (concept[0], ' '.join(concept[1].hasDescription)), concepts)), reverse=True)
|
||||||
return '\n'.join(list(map(lambda instance: ' '.join(instance[1].hasDescription), instances)))
|
return '\n'.join(list(map(lambda event: event[1], events)))
|
||||||
|
Loading…
Reference in New Issue
Block a user