1

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')
  • [Python:convert integers into words](https://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words) – Vivek Mehta Feb 03 '20 at 04:48

1 Answers1

0

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')
Kalpit
  • 891
  • 1
  • 8
  • 24
  • Here, I again have to include each number individually in the code, I need a way to include all the numbers, like, any number said is printed. – gopal prasath Feb 17 '20 at 05:29