2016-02-18 69 views
-5

我試圖讓這個程序運行,但每次我嘗試運行它時,都會出現語法錯誤,下面是我在終端中出現的錯誤圖片:Image of error 下面是我使用的代碼:Python中的語音識別語法錯誤

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "copyright", "credits" or "license()" for more information. 
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. 
Visit http://www.python.org/download/mac/tcltk/ for current information. 
import speech_recognition as sr 
import pyttsx 


engine = pyttsx.init() 
engine.setProperty('rate', 70) 
voices = engine.getProperty('voices') 
engine.setProperty('voice', voices[10].id) 

r = sr.Recognizer() 
m = sr.Microphone() 

try: 
    print("A moment of silence, please...") 
    with m as source: r.adjust_for_ambient_noise(source) 
    print("Set minimum energy threshold to {}".format(r.energy_threshold)) 
    while True: 
     print("Say something!") 
     with m as source: audio = r.listen(source) 
     print("Got it! Now to recognize it...") 
     try: 
      # recognize speech using Google Speech Recognition 
      value = r.recognize_google(audio) 

      # we need some special handling here to correctly print unicode characters to standard output 
      if str is bytes: # this version of Python uses bytes for strings (Python 2) 
       print(u"You said {}".format(value).encode("utf-8")) 
       engine.say('How are you today?') 
       engine.runAndWait() 
      else: # this version of Python uses unicode for strings (Python 3+) 
       print("You said {}".format(value)) 
     except sr.UnknownValueError: 
      print("Oops! Didn't catch that") 
     except sr.RequestError as e: 
      print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e)) 

except KeyboardInterrupt: 
    pass 
+3

我不知道你這個複製,但是從Python的'3.4.3(V3。 4.3:9b73f1c3e601,2015年2月23日,02:52:03)'不是有效的代碼。 – SuperBiasedMan

回答

1

刪除該部分或將其註釋掉:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "copyright", "credits" or "license()" for more information. 
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. 
Visit http://www.python.org/download/mac/tcltk/ for current information. 
+0

嘿,我刪除了第一部分,現在我得到這個錯誤信息: '文件「speechrecognition.py」,1號線,在 進口speech_recognition爲SR 導入錯誤:沒有模塊名爲speech_recognition' – user3062391

+0

你有沒有確保speech_recognition實際存在? – Mickey