首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将文本输入添加到python

如何将文本输入添加到python
EN

Stack Overflow用户
提问于 2021-03-19 19:11:50
回答 2查看 219关注 0票数 0

我已经创建了一个python,有时由于我的口音/lisp,它无法理解我&当它不能理解我时,我想在命令中输入命令。我该怎么做?我希望允许键盘输入作为命令的源(我只想输入我的命令)。

代码如下:

代码语言:javascript
复制
# 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-19 19:25:29

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

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2021-03-19 19:40:51

可能使用布尔值控制输入表单,我试图不更改任何代码或函数调用。只要你想留下它,但是通过一个布尔值的实现,如果你想要一个键盘输入或者你的声音发出命令的话,你可以检查它。

代码片段:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66714322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档