1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-07-02 22:36:54 +02:00

Remove mp3 files, fuzzy recognition

This commit is contained in:
dQz6tMwk8rJqvDR
2020-06-24 02:12:41 +03:00
parent 32e808583b
commit 76e6ec9061
26 changed files with 45 additions and 26 deletions

View File

@ -15,6 +15,7 @@
# #
from abc import ABC, abstractmethod # for abstract class and methods from abc import ABC, abstractmethod # for abstract class and methods
from fuzzywuzzy import fuzz
class Command(ABC): class Command(ABC):
_list = [] # list of all commands _list = [] # list of all commands
@ -88,13 +89,13 @@ class Command(ABC):
chances[i] = 0 chances[i] = 0
for weight, words in obj.getKeywords().items(): for weight, words in obj.getKeywords().items():
for word in words: for word in words:
if word in string: chances[i] += fuzz.partial_ratio(word, string) * weight
chances[i] += weight
print(chances) print(chances)
if( sum( chances.values() ) ): if( sum( chances.values() ) ):
top = max( chances.values() ) / sum( chances.values() ) * 100 top = max( chances.values() ) / sum( chances.values() ) * 100
else: else:
return list[0] return list[0]
print(top)
for i, chance in chances.items(): for i, chance in chances.items():
if chance == max( chances.values() ): if chance == max( chances.values() ):
return list[i] return list[i]

View File

@ -28,7 +28,6 @@ keywords = {}
void = SmallTalk('Undefined', keywords) void = SmallTalk('Undefined', keywords)
void.setStart(method) void.setStart(method)
################################################################################ ################################################################################
################################################################################
def method(): def method():
now = datetime.datetime.now() now = datetime.datetime.now()
hours = now.hour%12 hours = now.hour%12
@ -95,9 +94,9 @@ def method():
return f'Сейчас {get_str_num(hours%12, 0)} {str_hour}, {get_str_num(minutes, 1)} {str_minute}' return f'Сейчас {get_str_num(hours%12, 0)} {str_hour}, {get_str_num(minutes, 1)} {str_minute}'
keywords = { keywords = {
5: ['который час', 'сколько времени', 'время', 'часов'], 10: ['который час', 'сколько времени'],
2: ['текущее', 'сейчас', 'час'], 5: ['текущее', 'сейчас', 'час', 'время'],
0.5: ['сколько'] 1: ['сколько']
} }
ctime = SmallTalk('Current Time', keywords) ctime = SmallTalk('Current Time', keywords)
ctime.setStart(method) ctime.setStart(method)

View File

@ -2,23 +2,39 @@ from google.cloud import texttospeech
import os import os
import pygame import pygame
import time import time
import mmap
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "Text2Speech/archie-test-key.json" os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "Text2Speech/archie-test-key.json"
class speech: class Speech:
def __init__(this, text, voice, path): _list = []
this._text = text def __init__(this, text, voice, path, standart = False):
this._voice = voice this._text = text
this._path = path this._voice = voice
this._path = path
this._standart = standart
if(standart): Speech.append(this)
def speak(this): def speak(this):
print(f'Говорю: {this._text}') if( os.path.exists(this._path) ):
pygame.mixer.init() print(f'Говорю: {this._text}')
pygame.mixer.music.load(this._path) with open(this._path) as f:
pygame.mixer.music.play() with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as audio:
while pygame.mixer.music.get_busy() == True: pygame.mixer.init()
time.sleep(0.1) pygame.mixer.music.load(audio)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
if(not this._standart): os.remove(this._path)
class engine: @staticmethod
def append(obj):
Speech._list.append(obj)
@staticmethod
def getList():
return Speech._list
class Engine:
def __init__(this, name = 'ru-RU-Wavenet-B'): def __init__(this, name = 'ru-RU-Wavenet-B'):
# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "archie-test-key.json" # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "archie-test-key.json"
this._name = name this._name = name
@ -29,13 +45,13 @@ class engine:
name = name, name = name,
ssml_gender = texttospeech.SsmlVoiceGender.FEMALE) ssml_gender = texttospeech.SsmlVoiceGender.FEMALE)
def generate(this, text): def generate(this, text, standart = False):
dir = f'audio/{this._name}'
path = f'{dir}/{text}.mp3'
if( os.path.exists(path) ): return Speech(text, this._name, path, standart)
synthesis_input = texttospeech.SynthesisInput(text=text) synthesis_input = texttospeech.SynthesisInput(text=text)
response = this._client.synthesize_speech(input = synthesis_input, voice = this._voice, audio_config = this._audio_config) response = this._client.synthesize_speech(input = synthesis_input, voice = this._voice, audio_config = this._audio_config)
dir = f'audio/{this._name}/'
path = f'{dir}/{text}.mp3'
if not os.path.exists(dir): os.makedirs(dir) if not os.path.exists(dir): os.makedirs(dir)
if not os.path.exists(path): with open(path, 'wb') as out:
with open(path, 'wb') as out: out.write(response.audio_content)
out.write(response.audio_content) return Speech(text, this._name, path, standart)
return speech(text, this._name, path)

Binary file not shown.

Binary file not shown.

View File

@ -5,7 +5,10 @@ from Command import Command
import SmallTalk import SmallTalk
import Text2Speech import Text2Speech
archie = Text2Speech.engine() archie = Text2Speech.Engine()
archie.generate('Привет', True)
Text2Speech.Speech.getList()[0].speak()
while True: while True:
string = str(input('-> ')) string = str(input('-> '))