Simplify main.py, change protected access modifiers to private
This commit is contained in:
parent
4cc180a326
commit
155c4b4f4f
17
src/main.py
17
src/main.py
@ -10,16 +10,10 @@ from src.speech import Speech
|
|||||||
from syntax import Syntax
|
from syntax import Syntax
|
||||||
|
|
||||||
|
|
||||||
def _main():
|
def _main(wav_file: str):
|
||||||
if len(sys.argv) < 2:
|
text: str = Speech().run_recognition(wav_file)
|
||||||
print(f'Usage: {sys.argv[0]} FILE')
|
|
||||||
exit(1)
|
|
||||||
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}')
|
print(f'Text: {text}')
|
||||||
syntax_server: str = 'http://syntaxnet.athene.tech'
|
parse_tree: ParseTree = Syntax().get_parse_tree(text)
|
||||||
parse_tree: ParseTree = Syntax().get_parse_tree(text, syntax_server)
|
|
||||||
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([noun for noun in nouns])}')
|
||||||
@ -28,4 +22,7 @@ def _main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
_main()
|
if len(sys.argv) < 2:
|
||||||
|
print(f'Usage: {sys.argv[0]} FILE')
|
||||||
|
exit(1)
|
||||||
|
_main(sys.argv[1])
|
||||||
|
@ -5,45 +5,45 @@ from owlready2 import get_ontology, Ontology
|
|||||||
|
|
||||||
class MyOntology:
|
class MyOntology:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._onto: Ontology = get_ontology("file://./ontology.owl").load()
|
self.__onto: Ontology = get_ontology("file://./ontology.owl").load()
|
||||||
|
|
||||||
def _find(self, string: str, string_list: List[str]) -> int:
|
def __find_str_in_list(self, string: str, string_list: List[str]) -> int:
|
||||||
try:
|
try:
|
||||||
string_list.index(string)
|
string_list.index(string)
|
||||||
return 1
|
return 1
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def _get_property_value(self, values: [], instance):
|
def __get_property_value(self, values: [], instance):
|
||||||
if len(values) != 1:
|
if len(values) != 1:
|
||||||
raise ValueError(f'Wrong values in {instance.name}')
|
raise ValueError(f'Wrong values in {instance.name}')
|
||||||
return values[0]
|
return values[0]
|
||||||
|
|
||||||
def _get_event_instance(self, instances: []):
|
def __get_event_instance(self, instances: []):
|
||||||
max_instance: Optional = None
|
max_instance: Optional = None
|
||||||
max_priority: int = 0
|
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:
|
if priority > max_priority:
|
||||||
max_instance = event
|
max_instance = event
|
||||||
max_priority = priority
|
max_priority = priority
|
||||||
return max_instance
|
return max_instance
|
||||||
|
|
||||||
def _find_instances_by_terms(self, nouns: List[str]) -> []:
|
def __find_instances_by_terms(self, nouns: List[str]) -> []:
|
||||||
instances = []
|
instances = []
|
||||||
for instance in self._onto.Concept.instances():
|
for instance in self.__onto.Concept.instances():
|
||||||
terms = instance.hasTerm
|
terms = instance.hasTerm
|
||||||
match: int = 0
|
match: int = 0
|
||||||
for term in terms:
|
for term in terms:
|
||||||
match = match + self._find(term.name, nouns)
|
match = match + self.__find_str_in_list(term.name, nouns)
|
||||||
if match >= len(terms) * 0.5:
|
if match >= len(terms) * 0.5:
|
||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
return instances
|
return instances
|
||||||
|
|
||||||
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)
|
event = self.__get_event_instance(instances)
|
||||||
if event is None:
|
if event is None:
|
||||||
return ''
|
return ''
|
||||||
return self._get_property_value(event.hasDescription, event)
|
return self.__get_property_value(event.hasDescription, event)
|
||||||
|
@ -8,8 +8,7 @@ from src.parse_tree.parse_tree_node import ParseTreeNode
|
|||||||
|
|
||||||
|
|
||||||
class NLP:
|
class NLP:
|
||||||
@staticmethod
|
def lemmatizer(self, text: str):
|
||||||
def _lemmatizer(text: str):
|
|
||||||
doc = ru_core_news_sm.load()(text)
|
doc = ru_core_news_sm.load()(text)
|
||||||
tokens = [token.lemma_ for token in doc]
|
tokens = [token.lemma_ for token in doc]
|
||||||
return ' '.join(tokens)
|
return ' '.join(tokens)
|
||||||
@ -20,4 +19,4 @@ class NLP:
|
|||||||
if node.upos != 'NOUN':
|
if node.upos != 'NOUN':
|
||||||
continue
|
continue
|
||||||
nouns.append(node)
|
nouns.append(node)
|
||||||
return list(set([self._lemmatizer(noun.lemma) for noun in nouns]))
|
return list(set([self.lemmatizer(noun.lemma) for noun in nouns]))
|
||||||
|
@ -8,7 +8,7 @@ from src.parse_tree.parse_tree_node import ParseTreeNode
|
|||||||
class ParseTree:
|
class ParseTree:
|
||||||
|
|
||||||
def __init__(self, raw_tree: str):
|
def __init__(self, raw_tree: str):
|
||||||
self._tree: ParseTreeNode = self.__create_tree(self.__create_nodes_array(raw_tree))
|
self.__tree: ParseTreeNode = self.__create_tree(self.__create_nodes_array(raw_tree))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __parse_raw_tree_line(raw_tree_line: str) -> Optional[ParseTreeNode]:
|
def __parse_raw_tree_line(raw_tree_line: str) -> Optional[ParseTreeNode]:
|
||||||
@ -45,8 +45,8 @@ class ParseTree:
|
|||||||
break
|
break
|
||||||
return root
|
return root
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return '\n'.join([f'{pre}{node}' for pre, fill, node in RenderTree(self._tree)])
|
|
||||||
|
|
||||||
def get_tree_root(self) -> ParseTreeNode:
|
def get_tree_root(self) -> ParseTreeNode:
|
||||||
return self._tree
|
return self.__tree
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return '\n'.join([f'{pre}{node}' for pre, fill, node in RenderTree(self.__tree)])
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
from typing import Final
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from scipy.io import wavfile
|
from scipy.io import wavfile
|
||||||
|
|
||||||
|
|
||||||
class Speech:
|
class Speech:
|
||||||
|
__server: Final[str] = 'http://vosk.athene.tech'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __check_wav(wav_file):
|
def __check_wav(wav_file):
|
||||||
@ -23,10 +26,9 @@ class Speech:
|
|||||||
result = file.read()
|
result = file.read()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
def __stt(self, wav_file):
|
||||||
def __stt(wav_file, server):
|
print(f'Connecting to \'{self.__server}\'...')
|
||||||
print(f'Connecting to \'{server}\'...')
|
response = requests.post(url=f'{self.__server}/stt',
|
||||||
response = requests.post(url=f'{server}/stt',
|
|
||||||
data=Speech.__load_wav(wav_file),
|
data=Speech.__load_wav(wav_file),
|
||||||
headers={'Content-Type': 'audio/wav'})
|
headers={'Content-Type': 'audio/wav'})
|
||||||
result = response.json()
|
result = response.json()
|
||||||
@ -36,5 +38,5 @@ class Speech:
|
|||||||
|
|
||||||
return result['text'] if not result['code'] else f'Server error: {result}'
|
return result['text'] if not result['code'] else f'Server error: {result}'
|
||||||
|
|
||||||
def run_recognition(self, wav_file: str, server: str) -> str:
|
def run_recognition(self, wav_file: str) -> str:
|
||||||
return self.__stt(wav_file, server)
|
return self.__stt(wav_file)
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
|
from typing import Final
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from src.parse_tree.parse_tree import ParseTree
|
from src.parse_tree.parse_tree import ParseTree
|
||||||
|
|
||||||
|
|
||||||
class Syntax:
|
class Syntax:
|
||||||
|
__server: Final[str] = 'http://syntaxnet.athene.tech'
|
||||||
|
|
||||||
@staticmethod
|
def __parsey(self, text):
|
||||||
def __parsey(text, server):
|
print(f'Connecting to \'{self.__server}\'...')
|
||||||
print(f'Connecting to \'{server}\'...')
|
response = requests.post(url=f'{self.__server}/v1/parsey-universal-full',
|
||||||
response = requests.post(url=f'{server}/v1/parsey-universal-full',
|
|
||||||
data=text.encode('utf-8'),
|
data=text.encode('utf-8'),
|
||||||
headers={
|
headers={
|
||||||
'Content-Type': 'text/plain; charset=utf-8',
|
'Content-Type': 'text/plain; charset=utf-8',
|
||||||
@ -20,5 +22,5 @@ class Syntax:
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_parse_tree(self, text: str, server: str) -> ParseTree:
|
def get_parse_tree(self, text: str) -> ParseTree:
|
||||||
return ParseTree(self.__parsey(text, server))
|
return ParseTree(self.__parsey(text))
|
||||||
|
Loading…
Reference in New Issue
Block a user