Skip to content

Instantly share code, notes, and snippets.

@analyticsindiamagazine
Created December 10, 2019 06:56
Show Gist options
  • Save analyticsindiamagazine/59b68d4ccda49210fdafb1d1f7a58891 to your computer and use it in GitHub Desktop.
Save analyticsindiamagazine/59b68d4ccda49210fdafb1d1f7a58891 to your computer and use it in GitHub Desktop.
#Importing the speech recognition module
import speech_recognition as sr
#A random default value for prediction -- not among the classes we are predicting
prediction = 10
#Initializing the speech recognizer
r = sr.Recognizer()
#Declaring the listener source
with sr.Microphone() as source:
print('-'*30)
print('-'*30)
print("\n\nHi !! Say something and I will try to understand what you are talking about !!\n")
print('-'*30)
print("\nListening :\n")
print('-'*30)
#Initializing the listener
audio = r.listen(source)
#Collecting Speech and Classifying
try:
text = r.recognize_google(audio)
print("\nYou said : {}".format(text))
prediction, _ = model.predict([text.strip()])
except:
print("\nSorry could not recognize what you said")
if int(prediction) == 0:
print('\nYou spoke about POLITICS')
elif int(prediction) == 1:
print('\nYou spoke about TECHNOLOGY')
elif int(prediction) == 2:
print('\nYou spoke about Entertainment')
elif int(prediction) == 3:
print('\nYou spoke about Business')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment