Get multiple events sorted by priority from ontology
This commit is contained in:
parent
bfe83ef160
commit
9164c0833c
4
main.py
4
main.py
@ -16,9 +16,9 @@ def _main(wav_file: str):
|
||||
parse_tree: ParseTree = Syntax().get_parse_tree(text)
|
||||
print(f'Parse tree:\n{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)
|
||||
print(f'Test: {result}')
|
||||
print(f'Test:\n{result}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import List, Optional
|
||||
from typing import List, Dict
|
||||
|
||||
from owlready2 import get_ontology, Ontology
|
||||
|
||||
@ -20,15 +20,13 @@ class MyOntology:
|
||||
return values[0]
|
||||
|
||||
def __get_event_instance(self, instances: []):
|
||||
max_instance: Optional = None
|
||||
max_priority: int = 0
|
||||
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)
|
||||
if priority > max_priority:
|
||||
max_instance = event
|
||||
max_priority = priority
|
||||
return max_instance
|
||||
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, nouns: List[str]) -> []:
|
||||
instances = []
|
||||
@ -43,7 +41,5 @@ class MyOntology:
|
||||
|
||||
def get_event_description(self, nouns: List[str]) -> str:
|
||||
instances: [] = self.__find_instances_by_terms(nouns)
|
||||
event = self.__get_event_instance(instances)
|
||||
if event is None:
|
||||
return ''
|
||||
return self.__get_property_value(event.hasDescription, event)
|
||||
events = self.__get_event_instance(instances)
|
||||
return '\n'.join(list(map(lambda item: f'{list(item.keys())[0]}: {list(item.values())[0]}', events)))
|
||||
|
Loading…
Reference in New Issue
Block a user