Complete ontology support

This commit is contained in:
Aleksey Filippov 2022-01-25 16:08:20 +04:00
parent b18e17e961
commit 442c0fe483
3 changed files with 62 additions and 24 deletions

View File

@ -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__':

49
src/myontology.py Normal file
View File

@ -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)

View File

@ -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