简体   繁体   中英

Is there any way to convert words in to numbers of speech_recognition library in python

I have tried for numbers from 1 to 10 and it works well but i need it to work with all the numbers and it is not feasible to write the code for each number. I also need it to work in sentences too which is not happening in my code. Help me out guys, please.... This is my code....

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')

In case you don't want to take Vivek Mehta's suggestion and don't want an additional dependency, you can use a plain dictionary

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM