From 442c0fe483a362ca98abc25c3af7063cc3a1da76 Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Tue, 25 Jan 2022 16:08:20 +0400 Subject: [PATCH] Complete ontology support --- src/main.py | 28 +++++++++++++-------------- src/myontology.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/ontology.py | 9 --------- 3 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 src/myontology.py delete mode 100644 src/ontology.py diff --git a/src/main.py b/src/main.py index f72022e..35fc5a4 100644 --- a/src/main.py +++ b/src/main.py @@ -1,32 +1,30 @@ #!/usr/bin/env python3 import sys -import warnings +from typing import List +from src.myontology import MyOntology from src.nlp import NLP -from src.ontology import Ontology +from src.parse_tree.parse_tree import ParseTree +from src.speech import Speech from syntax import Syntax -if not sys.warnoptions: - warnings.simplefilter("ignore") - def _main(): if len(sys.argv) < 2: print(f'Usage: {sys.argv[0]} FILE') exit(1) - # wav_file = sys.argv[1] - # speech_server = 'http://vosk.athene.tech' - # text = Speech().run_recognition(wav_file, speech_server) - text = 'не могу оплатить из-за ограничений карты' + wav_file: str = sys.argv[1] + speech_server: str = 'http://vosk.athene.tech' + text: str = Speech().run_recognition(wav_file, speech_server) print(f'Text: {text}') - syntax_server = 'http://syntaxnet.athene.tech' - parse_tree = Syntax().get_parse_tree(text, syntax_server) + syntax_server: str = 'http://syntaxnet.athene.tech' + parse_tree: ParseTree = Syntax().get_parse_tree(text, syntax_server) print(f'Parse tree:\n{parse_tree}') - nouns = NLP().get_nouns(parse_tree) - print(f'Nouns:\n{nouns}') - onto = Ontology() - print(f'Test: {onto.get_event()}') + nouns: List[str] = NLP().get_nouns(parse_tree) + print(f'Nouns:\n{" ".join([noun for noun in nouns])}') + result: str = MyOntology().get_event_description(nouns) + print(f'Test: {result}') if __name__ == '__main__': diff --git a/src/myontology.py b/src/myontology.py new file mode 100644 index 0000000..82389d0 --- /dev/null +++ b/src/myontology.py @@ -0,0 +1,49 @@ +from typing import List, Optional + +from owlready2 import get_ontology, Ontology + + +class MyOntology: + def __init__(self) -> None: + self._onto: Ontology = get_ontology("file://./ontology.owl").load() + + def _find(self, string: str, string_list: List[str]) -> int: + try: + string_list.index(string) + 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: []): + max_instance: Optional = None + max_priority: int = 0 + 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 + + def _find_instances_by_terms(self, nouns: List[str]) -> []: + instances = [] + for instance in self._onto.Concept.instances(): + terms = instance.hasTerm + match: int = 0 + for term in terms: + match = match + self._find(term.name, nouns) + if match >= len(terms) * 0.5: + instances.append(instance) + return instances + + 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) diff --git a/src/ontology.py b/src/ontology.py deleted file mode 100644 index dc6fb0e..0000000 --- a/src/ontology.py +++ /dev/null @@ -1,9 +0,0 @@ -from owlready2 import get_ontology - - -class Ontology: - def __init__(self) -> None: - self._onto = get_ontology("file://./ontology.owl").load() - - def get_event(self) -> str: - return self._onto.Concept