【问题标题】:Is there any way to convert words in to numbers of speech_recognition library in python有没有办法将单词转换为python中的speech_recognition库的数量
【发布时间】:2020-02-03 13:41:46
【问题描述】:

我尝试了从 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')

【问题讨论】:

标签: python python-3.x math speech-recognition speech-to-text


【解决方案1】:

如果您不想接受 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')

【讨论】:

  • 在这里,我再次必须在代码中单独包含每个数字,我需要一种方法来包含所有数字,例如打印任何所说的数字。
猜你喜欢
  • 2010-10-04
  • 2021-05-15
  • 1970-01-01
  • 2021-03-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
相关资源
最近更新 更多