helper/src/myontology.py

46 lines
1.7 KiB
Python

from typing import List, Dict
from owlready2 import get_ontology, Ontology
class MyOntology:
def __init__(self) -> None:
self.__onto: Ontology = get_ontology("file://./ontology.owl").load()
def __find_str_in_list(self, string: str, string_list: List[str]) -> int:
try:
string_list.index(string.replace("_", " "))
return 1
except ValueError:
return 0
def __get_property_value(self, values: [], instance):
if len(values) != 1:
raise ValueError(f'Wrong values in {instance.name}')
return values[0]
def __get_event_instance(self, instances: []):
events: List[Dict[int, str]] = []
for instance in instances:
event = self.__get_property_value(instance.hasEvent, instance)
priority = self.__get_property_value(event.hasPriority, event)
events.append({priority: self.__get_property_value(event.hasDescription, event)})
events.sort(key=lambda item: list(item.keys())[0], reverse=True)
return events
def __find_instances_by_terms(self, my_terms: List[str]) -> []:
instances = []
for instance in self.__onto.Concept.instances():
terms = instance.hasTerm
match: int = 0
for term in terms:
match = match + self.__find_str_in_list(term.name, my_terms)
if match >= 1:
instances.append(instance)
return instances
def get_event_description(self, terms: List[str]) -> str:
instances: [] = self.__find_instances_by_terms(terms)
events = self.__get_event_instance(instances)
return '\n'.join(list(map(lambda item: f'{list(item.keys())[0]}: {list(item.values())[0]}', events)))