如何向python ai添加文本输入



我创建了一个python ai,有时因为我的口音/lisp&当它不能理解我时,我想手动输入命令。我该怎么做?我想允许键盘输入作为命令的来源(我只想能够输入我的命令(。

代码如下:

# H.I.V.E V.0.0.3 BETA: Home-Assistant Intergrated Virtual Environment
# #VIEW THE HIVE PROJECT AT HTTPS://natebrownprojects.github.io/TheHiveProject/
# Copyright: Nate Brown Projects 2021 / Nate Brown 2021 / TheHiveProjectNZ 2021
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
from datetime import timedelta
import wikipedia
import pyjokes
from pyttsx3 import Engine
listener = sr.Recognizer()
engine: Engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0]  .id)

def talk(text):
engine.say(text)
engine.runAndWait()
talk('Systems Loading, Welcome to the HIVE.')
talk('How, can i help, you Sir?')
def take_command():
try:
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'hive' in command:
command = command.replace('hive', '')
print(command)
except:
pass
return command

def run_hive():
command = take_command()
print(command)
if 'play' in command:
song = command.replace('play', '')
talk('playing ' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
talk('Current time is ' + time)
elif 'date' in command:
now = datetime.datetime.now()
talk("Current date and time : ")
talk(now.strftime("%d         %m                %Y"))
engine.setProperty("rate", 178)
elif 'who is' in command:
person = command.replace('who is', '')
info = wikipedia.summary(person, 1, auto_suggest=False)
print(info)
talk(info)
elif 'joke' in command:
talk(pyjokes.get_joke())
elif 'status report' in command:
talk('All Systems Operational Sir!')
elif 'hive' in command:
talk('Yes, sir?')
elif 'shut down' in command:
talk('Shutting all Hive Systems Down.')
talk('Thank you for using hive! Goodbye!')
print('Thank you for using H.I.V.E!')
exit()
elif 'exit' in command:
talk('Shutting all Hive Systems Down.')
talk('Thank you for using hive! Goodbye!')
print('Thank you for using H.I.V.E!')
exit()
elif 'awesome thanks' in command:
talk('Your, Welcome!')
elif 'thanks' in command:
talk('Ny Pleasure!')
elif 'thank you' in command:
talk('No Problem!')
elif 'awesome' in command:
talk('No Problem, is there anything i, can help you, with?')
elif ' no' in command:
talk('ok!')
elif 'yes' in command:
talk('Ok, what is it?')
elif 'how are you' in command:
talk('Im Great, How are you!?')
elif 'you still there' in command:
talk('Yes Sir, i am ready for your command!')
elif 'who are you' in command:
talk('My name is Hive. It stands for Home Assistant Intergrated Virtual Environment. I am here to help you with whatever i can')
elif 'hello' in command:
talk('hello, how are you today?')
else:
talk('Please say the command again.')

while True:
try:
run_hive()
except UnboundLocalError:
continue

只需稍微更改take_command函数,例如:

def take_command():
# ask the user if he wants to use the mike
opt = input('use the mike(y/n)?: ')
# if not, he would type his command
if opt.lower() == "n":
return input("Please type your command: ").lower()
try:
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'hive' in command:
command = command.replace('hive', '')
print(command)
except:
pass
return command

也许用布尔值控制输入表单,我尽量不更改任何代码或函数调用。只是让它按你想要的那样,但使用布尔值的实现,如果你想要键盘输入或只是你的声音发出命令,就会进行检查。

代码段:

KeyboardInterrupt = False

def take_command():
choice = input("Keyboard input?")
if(choice=="Yes" or choice=="yes"):
KeyboardInterrupt = True
else:
KeyboardInterrupt = False
try:
if not KeyboardInterrupt:

with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'hive' in command:
command = command.replace('hive', '')
print(command)
else:
command = input("Type your command: ")            
except:
pass
return command

最新更新