File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed 
Code-Sleep-Python/chatbot Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ import  speech_recognition  as  sr 
2+ import  os 
3+ import  subprocess 
4+ from  gtts  import  gTTS 
5+ 
6+ r  =  sr .Recognizer ()
7+ 
8+ with  sr .Microphone () as  source :
9+     r .adjust_for_ambient_noise (source )
10+     print ("Say something!" )
11+     audio  =  r .listen (source )
12+     print ("Got the speech. Now processing..." )
13+ 
14+ # recognize speech using Wit.ai 
15+ # export your wit.ai key to your environment before running this program 
16+ WIT_AI_KEY  =  os .getenv ("WITAIKEY" )
17+ if  not  WIT_AI_KEY :
18+     print ("API key not available" )
19+     exit ()
20+ 
21+ in_speech  =  "" 
22+ try :
23+     in_speech  =  r .recognize_wit (audio , key = WIT_AI_KEY )
24+     print ("You said "  +  in_speech )
25+ except  sr .UnknownValueError :
26+     print ("Wit.ai could not understand audio" )
27+ except  sr .RequestError  as  e :
28+     print ("Could not request results from Wit.ai service; {0}" .format (e ))
29+ 
30+ # in_speech contains the string of the input audio. 
31+ # It can be processed and actions can be performed based on it. 
32+ # TODO implement actions based on keywords 
33+ # Suggestions: Utilise wit.ai for proper NLP 
34+ 
35+ if  (in_speech ):
36+     tts  =  gTTS (text = in_speech , lang = "en" )
37+     tts .save ("sample.mp3" )
38+     subprocess .Popen (["mpg123" , "-q" , "sample.mp3" ]).wait ()
Original file line number Diff line number Diff line change 1+ gTTS == 2.0.0 
2+ gTTS-token == 1.1.1 
3+ SpeechRecognition == 3.8.1 
4+ 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments