Voice assistant Vitaly (school project)

Warning

This post was created solely to get feedback from specialists, so feel free to minus it so as not to clog the tape with school projects.





Also, I do not advise using the post as a guide, since I myself study and collect feedback and advice from knowledgeable people.





Introduction

My name is Gleb (8 cells) and I set out to make a good opener / closeer and a little talker for windows, in general a voice assistant in python.





At the time of this writing, the assistant has version alfa 4.0 and does not position itself as a serious product or, God forbid, a competitor to Alice or Siri.





For the assistant, a site was quickly thrown up where you can download an exe or a source in python, which is probably interesting to you. Github is also available.





The code

Since I have already attached the github , I propose to focus on interesting, controversial or incomprehensible moments for me.





Speech recognition

For the razrekavalka work, I chose speech recognition and, as a result, the following code was implemented (a composite hodgepodge of video tutorials):





import speech_recognition as sr

#  ,        ( -  " ")
recomend = (" ", "    . .", " ?", "  ?", "    495 544.", "   .")

# (  ,     ,   -      )
    rec1 = len(recomend) - 1
    rec2 = recomend[random.randint(0, rec1)]
    print('-------------------')
    r = sr.Recognizer()
    with sr.Microphone() as source:
	    print("  , :", rec2)
	    r.pause_threshold = 1
	    #r.adjust_for_ambient_noise(source, duration=1)
	    audio = r.listen(source)
    try:
	    #    an (answer)
      an = r.recognize_google(audio, language="ru-RU").lower()
	    print(" : " + an)
    except sr.UnknownValueError:
        t = "   ,  !"
        print("   . ")
      
      



I ask you to leave better analogs of speech recognition in the comments.





Speech synthesis

For speech synthesis, I chose the vokolizer voice, as well as the pyttsx3 library. In the code, it looks like this:





import pyttsx3

# ,     .
f = open("tts.txt", "r")
tts1 = int(f.read(1))
f.close()
text = ""
tts = pyttsx3.init()
speak_engine = pyttsx3.init()
voices = speak_engine.getProperty('voices')
speak_engine.setProperty('voice', voices[tts1].id)
#  
def run():
    tts.say(t)
    tts.runAndWait()
    print(":", t)
    
# 
t = " "
run()
      
      



SMALLTALK

, :





import os

#  smalltalk   2 : 1 -   , 2 -   
f = open("smalltalk.txt", "r", encoding="utf-8")
smalltalkdialog = f.read()

asksmalltalk = smalltalkdialog[len(": "):smalltalkdialog.find(" | [1]")].split(" | ")

answersmalltalk = smalltalkdialog[smalltalkdialog.find(": ") + len(": "):smalltalkdialog.find(" | [2]")].split(" | ")

#  
for word in range(len(asksmalltalk)):
    if asksmalltalk[word] in an:
        t = answersmalltalk[word]
        run()
        break
      
      



: .





Dialog Flove . .





,

import webbrowser
# error   , -     ,  ,  error = 0
while  True:
    #
    elif "" in an:
        error = 0
        if " " in an:
            t = "   " + an[an.find("")+3:]
            run()
            sear = an[an.find("")+3:]
            webbrowser.open("https://www.google.com/search?q=" + sear)

        elif "youtube" in an:
            sear = an[an.find("be")+2:]
            t = "    " + sear
            run()
            webbrowser.open("https://www.youtube.com/results?search_query=" + sear)

        else:
            t = "   ,    ,       ."
            run()
        continue
    #      2 ,   "listprogram"    ,   "listprogram2" .
    elif "" in an:
        listprogram = ("steam", "skype", "")
        listprogram2 = ("TASKKILL /IM steam.exe", "TASKKILL /IM skype.exe", "TASKKILL /IM chrome.exe")
        for net in range(len(listprogram)):
            if listprogram[net] in an:
                program = listprogram2[net]
                os.system(program)
                os.system('cls' if os.name == 'nt' else 'clear')
                t = " " + listprogram[net] 
                run()
                error = 0
        continue

    # 
    elif "" in an:
        error = 0
        t = "  ,   .      "
        run()
        t = ""
        while True:
            t = t + " " + str(input("   > "))
            if "" in t:
                break
                t = t[:t.find("")]
        run()
         

    #    2 ,   "fordefweb"    ,   "fordefweb" .
    fordefweb = ("youtube", "", "", "google", "", "", "", "facebook", "wifmedia", " ")
    fordefweb2 = ("https://www.youtube.com/", "https:/vk.com", "https://www.google.ru/", "https://www.google.ru/", "https://lenta.ru/", "https://okko.tv/", "https://habr.com/ru/feed/", "https://www.facebook.com/", "https://wifmedia.com/", "http://vitaliy.renderforestsites.com")
    for net in range(len(fordefweb)):
        if fordefweb[net] in an:
            web = fordefweb2[net]
            runweb()
            error = 0


    #    2 ,   "listprogram"    ,   "listprogram2" .
    listprogram = ("", "skype")
    listprogram2 = ("explorer.exe", "start skype.exe")
    for net in range(len(listprogram)):
        if listprogram[net] in an:
            program = listprogram2[net]
            os.system(program)
            t = " " + listprogram[net] 
            run()
            error = 0
      
      



elif, .





, .





( renderforest)
















All Articles