Get multiple events sorted by priority from ontology

This commit is contained in:
Aleksey Filippov 2022-05-29 01:19:14 +04:00
parent bfe83ef160
commit 9164c0833c
2 changed files with 9 additions and 13 deletions

View File

@ -16,9 +16,9 @@ def _main(wav_file: str):
parse_tree: ParseTree = Syntax().get_parse_tree(text) parse_tree: ParseTree = Syntax().get_parse_tree(text)
print(f'Parse tree:\n{parse_tree}') print(f'Parse tree:\n{parse_tree}')
nouns: List[str] = NLP().get_nouns(parse_tree) nouns: List[str] = NLP().get_nouns(parse_tree)
print(f'Nouns:\n{" ".join([noun for noun in nouns])}') print(f'Nouns:\n{" ".join(nouns)}')
result: str = MyOntology().get_event_description(nouns) result: str = MyOntology().get_event_description(nouns)
print(f'Test: {result}') print(f'Test:\n{result}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,4 +1,4 @@
from typing import List, Optional from typing import List, Dict
from owlready2 import get_ontology, Ontology from owlready2 import get_ontology, Ontology
@ -20,15 +20,13 @@ class MyOntology:
return values[0] return values[0]
def __get_event_instance(self, instances: []): def __get_event_instance(self, instances: []):
max_instance: Optional = None events: List[Dict[int, str]] = []
max_priority: int = 0
for instance in instances: for instance in instances:
event = self.__get_property_value(instance.hasEvent, instance) event = self.__get_property_value(instance.hasEvent, instance)
priority = self.__get_property_value(event.hasPriority, event) priority = self.__get_property_value(event.hasPriority, event)
if priority > max_priority: events.append({priority: self.__get_property_value(event.hasDescription, event)})
max_instance = event events.sort(key=lambda item: list(item.keys())[0], reverse=True)
max_priority = priority return events
return max_instance
def __find_instances_by_terms(self, nouns: List[str]) -> []: def __find_instances_by_terms(self, nouns: List[str]) -> []:
instances = [] instances = []
@ -43,7 +41,5 @@ class MyOntology:
def get_event_description(self, nouns: List[str]) -> str: def get_event_description(self, nouns: List[str]) -> str:
instances: [] = self.__find_instances_by_terms(nouns) instances: [] = self.__find_instances_by_terms(nouns)
event = self.__get_event_instance(instances) events = self.__get_event_instance(instances)
if event is None: return '\n'.join(list(map(lambda item: f'{list(item.keys())[0]}: {list(item.values())[0]}', events)))
return ''
return self.__get_property_value(event.hasDescription, event)