2022-05-29 01:19:14 +04:00
|
|
|
from typing import List, Dict
|
2022-01-25 16:08:20 +04:00
|
|
|
|
|
|
|
from owlready2 import get_ontology, Ontology
|
|
|
|
|
|
|
|
|
|
|
|
class MyOntology:
|
|
|
|
def __init__(self) -> None:
|
2022-01-27 23:15:40 +04:00
|
|
|
self.__onto: Ontology = get_ontology("file://./ontology.owl").load()
|
2022-01-25 16:08:20 +04:00
|
|
|
|
2022-01-27 23:15:40 +04:00
|
|
|
def __find_str_in_list(self, string: str, string_list: List[str]) -> int:
|
2022-01-25 16:08:20 +04:00
|
|
|
try:
|
|
|
|
string_list.index(string)
|
|
|
|
return 1
|
|
|
|
except ValueError:
|
|
|
|
return 0
|
|
|
|
|
2022-01-27 23:15:40 +04:00
|
|
|
def __get_property_value(self, values: [], instance):
|
2022-01-25 16:08:20 +04:00
|
|
|
if len(values) != 1:
|
|
|
|
raise ValueError(f'Wrong values in {instance.name}')
|
|
|
|
return values[0]
|
|
|
|
|
2022-01-27 23:15:40 +04:00
|
|
|
def __get_event_instance(self, instances: []):
|
2022-05-29 01:19:14 +04:00
|
|
|
events: List[Dict[int, str]] = []
|
2022-01-25 16:08:20 +04:00
|
|
|
for instance in instances:
|
2022-01-27 23:15:40 +04:00
|
|
|
event = self.__get_property_value(instance.hasEvent, instance)
|
|
|
|
priority = self.__get_property_value(event.hasPriority, event)
|
2022-05-29 01:19:14 +04:00
|
|
|
events.append({priority: self.__get_property_value(event.hasDescription, event)})
|
|
|
|
events.sort(key=lambda item: list(item.keys())[0], reverse=True)
|
|
|
|
return events
|
2022-01-25 16:08:20 +04:00
|
|
|
|
2022-01-27 23:15:40 +04:00
|
|
|
def __find_instances_by_terms(self, nouns: List[str]) -> []:
|
2022-01-25 16:08:20 +04:00
|
|
|
instances = []
|
2022-01-27 23:15:40 +04:00
|
|
|
for instance in self.__onto.Concept.instances():
|
2022-01-25 16:08:20 +04:00
|
|
|
terms = instance.hasTerm
|
|
|
|
match: int = 0
|
|
|
|
for term in terms:
|
2022-01-27 23:15:40 +04:00
|
|
|
match = match + self.__find_str_in_list(term.name, nouns)
|
2022-03-25 14:49:48 +04:00
|
|
|
if match >= 1:
|
2022-01-25 16:08:20 +04:00
|
|
|
instances.append(instance)
|
|
|
|
return instances
|
|
|
|
|
|
|
|
def get_event_description(self, nouns: List[str]) -> str:
|
2022-01-27 23:15:40 +04:00
|
|
|
instances: [] = self.__find_instances_by_terms(nouns)
|
2022-05-29 01:19:14 +04:00
|
|
|
events = self.__get_event_instance(instances)
|
|
|
|
return '\n'.join(list(map(lambda item: f'{list(item.keys())[0]}: {list(item.values())[0]}', events)))
|