Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法将python中的单词转换为语音识别库中的数字_Python_Python 3.x_Math_Speech Recognition_Speech To Text - Fatal编程技术网

有没有办法将python中的单词转换为语音识别库中的数字

有没有办法将python中的单词转换为语音识别库中的数字,python,python-3.x,math,speech-recognition,speech-to-text,Python,Python 3.x,Math,Speech Recognition,Speech To Text,我尝试了从1到10的数字,效果很好,但我需要它来处理所有的数字,为每个数字编写代码是不可行的。 我也需要它在句子中工作,这不是发生在我的代码。 帮帮我,伙计们。。。。 这是我的代码 import speech_recognition as sr import time t = ['one','two','three','four','five','six','seven','eight','nine','ten'] r = sr.Recognizer() with sr.Microphone()

我尝试了从1到10的数字,效果很好,但我需要它来处理所有的数字,为每个数字编写代码是不可行的。 我也需要它在句子中工作,这不是发生在我的代码。 帮帮我,伙计们。。。。 这是我的代码

import speech_recognition as sr
import time
t = ['one','two','three','four','five','six','seven','eight','nine','ten']
r = sr.Recognizer()
with sr.Microphone() as source:
    print('Speak anything: ')
    audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        print('You said : {} '.format(text))
        time.sleep(1)
        for i in range(0,10):
            if (t[i] == text):
                print('/n',i)
    except:
        print('Sorry could not recogonize your voice')

如果您不想接受Vivek Mehta的建议,也不想增加依赖项,那么可以使用普通字典

import speech_recognition as sr
import time
t = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
r = sr.Recognizer()
with sr.Microphone() as source:
    print('Speak anything: ')
    audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        print('You said : {0} {1} '.format(text, t[text]))
        time.sleep(1)
    except:
        print('Sorry could not recogonize your voice')

如果您不想接受Vivek Mehta的建议,也不想增加依赖项,那么可以使用普通字典

import speech_recognition as sr
import time
t = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
r = sr.Recognizer()
with sr.Microphone() as source:
    print('Speak anything: ')
    audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        print('You said : {0} {1} '.format(text, t[text]))
        time.sleep(1)
    except:
        print('Sorry could not recogonize your voice')

这里,我再次需要在代码中单独包含每个数字,我需要一种方法来包含所有数字,比如说,任何数字都被打印出来。这里,我再次需要在代码中单独包含每个数字,我需要一种方法来包含所有数字,比如说,任何数字都被打印出来。