如何让语音识别在听完一个单词后停止收听?

Anw*_*eez 6 speech-recognition translation speech-to-text python-3.x

我正在制作一个 PygLatin 口语翻译器。一个非常基本的至少可以告诉一个翻译的单词。

如果我说多个单词,它就会变得混乱,并把第一个单词中的第一个字符放在最后一个单词上,最后加上“ay”。

因此,我遇到了一些问题。有没有办法让它在只说出一个单词后就停止监听?是否有可能有一种方法可以将每个单词的第一个字母放在每个口头单词的末尾,后跟“ay”?

这是到目前为止我的代码示例:

import time
import speech_recognition as sr
from gtts import gTTS
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from pygame import mixer
mixer.init()
pyg = "ay" 
count = 3
while count > 0:
    while (True == True):
  # get audio input
        original = sr.Recognizer()
        with sr.Microphone() as source:
      # listen for 1 second and create the ambient noise energy level
            original.adjust_for_ambient_noise(source, duration = 1)
            print("Say ONE word. If you do not wish to use the translator, say 'stop' ")
        audio = original.listen(source,phrase_time_limit = 1)

  # speech recognition with Google
        try:
            response = original.recognize_google(audio)
            if response == "stop":
                print("Thanks for using the PygLatin Translator, Goodbye!")
                break
                pyg_word = response + pyg
                w = response + pyg
                pyg_word = w[1 : len(pyg_word)]
                print(pyg_word)
                tts = gTTS(text = str(pyg_word), lang='en')
                tts.save("response.mp3")
                mixer.music.load('response.mp3')
                mixer.music.play()


        except sr.UnknownValueError:
            print("Translator could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))
Run Code Online (Sandbox Code Playgroud)