【问题标题】:How do i get him to stay awake until I say goodbye?在我说再见之前,我如何让他保持清醒?
【发布时间】:2021-12-15 02:16:06
【问题描述】:

构建我自己的语音助手。在给出每个命令并返回响应后,我必须再次唤醒他。我希望他对所有询问保持清醒。卡住了,请帮忙。抱歉,如果它看起来含糊不清。

WAKE = 'allen'
while True:
    text = mic_input()

    if text.count(WAKE) > 0:
        toSpeak.say("yes sir")
        toSpeak.runAndWait()
        text = mic_input()
        process_text(text)
        toSpeak.runAndWait()

【问题讨论】:

  • 你到底卡在哪里了?您的代码从不检查text 是否有"goodbye"。如果您制作了一个minimal reproducible example,其中包含完整但最少的代码、示例输入、预期输出和实际输出,将会有很大帮助。例如,暂时忘记语音方面,只使用input()print()。如果您想了解更多提示,请参阅How to Ask
  • 我最初是这样做的,但后来我的问题被删除了,因为我“没有特定问题”并且“不应该要求对代码进行故障排除”。另外,这与再见部分无关,问题出在我的循环上。出于某种原因,除非我再次醒来,否则它不会重复。我可以稍后再写再见部分,但如果你愿意的话,我想要在输入 killswitch 之前进行连续循环
  • 我提供的代码之前的所有其他内容都只是命令。即“打开excel”、“搜索google”、“计算数学”等
  • 不仅仅是命令。这些名称未定义:mic_input, toSpeak, process_text
  • 它们在代码的前面和各自的函数中定义。我认为我不需要发布每一行代码。我希望我听起来不粗鲁,但是当我之前问过这个问题并发布了包含所有定义的代码时,我被标记为广泛

标签: python python-3.x artificial-intelligence voice-recognition


【解决方案1】:

我不确定我是否理解问题,但我会使用变量True/False 来控制它是否唤醒

WAKE = 'allen'

awake = False

while True:
    text = mic_input()

    if not awake:
        if text.count(WAKE):
            awake = True
            toSpeak.say("yes sir")
            toSpeak.runAndWait()
    else:        
        if text.count('goodbye'):
            awake = False
            toSpeak.say("bye")
            toSpeak.runAndWait()
        elif text.count('time'): 
            process_text('it is time to eat something')
            toSpeak.runAndWait()
        else:
            process_text("I don't understand " + text)
            toSpeak.runAndWait()

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2010-09-08
    • 1970-01-01
    • 2013-12-04
    • 2021-11-16
    • 2020-03-12
    • 2019-08-27
    • 1970-01-01
    • 2014-03-29
    相关资源
    最近更新 更多