我目前正在建設一個個人助理機器人是一個挑戰,而我在學校放假。我目前有麻煩了SpeechRecognition整合ChatterBot。當我在同一個文件都打電話,我沒有得到任何錯誤:Python的語音識別與聊天機器人
import speech_recognition as sr
from chatterbot import ChatBot
def setup():
chatbot = ChatBot(
'Ron Obvious',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)
# Train based on the english corpus
chatbot.train("chatterbot.corpus.english")
while True:
get_response(chatbot)
def get_response(chatbot):
# Get a response to an input statement
speech = return_audio()
print chatbot.get_response(speech)
def return_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
speech = r.recognize_google(audio)
try:
speech = str(speech)
print speech
return speech
except TypeError:
print "Error! Could not convert speech to string!"
except sr.UnknownValueError:
print "Error! Could not process that audio."
return "Error!"
except sr.RequestError as e:
print "Error! No internet connection to Google Sound Recognizer."
return "Error!"
setup()
但是,如果我把兩個兩個不同的文件,我得到一個AttributeError。我得到的程序有它拋出一個異常崩潰左右,但這個例外意味着機器人從來沒有真正與用戶交互。
貝婁是兩個文件: 聽
import speech_recognition as sr
from chatterbot import ChatBot
import Messanger_Alert as MA
import Weather
import Email_Alert
import Chat
import Run
def start():
bot = Chat.start()
MA_client = MA.login()
print "Hello Luke!"
print "I am ready for your command! :D"
while True:
speech = str(return_audio())
if not speech:
print "Error! Speech returned nonetype!"
continue
else:
try:
if any(word in speech for word in ("Jason", "jason")): #Old key words: "SysGen", "Sysgen", "System", "system" - will reimpliment if needed
if any(word in speech for word in ("Message", "message", "Messenger", "messenger", "Facebook", "facebook")):
if any(word in speech for word in ("Send", "send")):
MA.send_message(MA_client) #now getting an attribute error.
print "Ready for next command!"
elif any(word in speech for word in ("Search Friend", "search friend", "Seach friend", "search Friend", "friend", "Friend", "friends", "Friends")):
MA.friend_search(MA_client)
print "Ready for next command!"
elif any(word in speech for word in ("Check", "check")):
MA.check_message(MA_client)
print "Ready for next command!"
elif any(word in speech for word in ("Email", "email")):
if any(word in speech for word in ("Personal", "personal", "Home", "home")):
Email_Alert.login1()
print "Ready for next command!"
elif any(word in speech for word in ("School", "school", "Dorm", "dorm", "UCSD", "ucsd")):
Email_Alert.login2()
print "Ready for next command!"
elif any(word in speech for word in ("Weather", "weather", "Forecast", "forecast")):
Weather.decide()
elif any(word in speech for word in ("Goodnight", "goodnight", "Bye", "bye", "Goodbye", "goodbye")):
print "Goodnight Luke!"
exit()
else:
Chat.talk(bot, speech)
elif speech == "Error!":
return_audio()
else:
Chat.talk(bot, speech)
except sr.UnknownValueError:
print "Error! Could not process that audio."
except sr.RequestError as e:
print "Error! No internet connection to Google Sound Recognizer."
def return_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
speech = r.recognize_google(audio)
try:
speech = str(speech)
print speech
return speech
except TypeError:
print "Error! Could not convert speech to string!"
except sr.UnknownValueError:
print "Error! Could not process that audio."
return "Error!"
except sr.RequestError as e:
print "Error! No internet connection to Google Sound Recognizer."
return "Error!"
聊天:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os.path
import Listen
def start():
file_list = ["Jason_logs.txt", "Sample1.txt"]
chatbot = ChatBot('Jason', trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
for path in file_list:
if os.path.exists(path) == "True":
train_from_text(chatbot, path)
train_bot(chatbot)
def train_from_text(chatbot, path):
conversation = []
with open(path, 'r') as f:
while True:
line1 = f.readline()
line2 = f.readline()
if not line2:
break
else:
conversation.append(line1)
conversation.append(line2)
chatbot.set_trainer(ListTrainer)
chatbot.train(conversation)
def train_bot(chatbot):
# Train based on the english corpus
chatbot.train("chatterbot.corpus.english")
# Train based on english greetings corpus
chatbot.train("chatterbot.corpus.english.greetings")
# Train based on the english conversations corpus
chatbot.train("chatterbot.corpus.english.conversations")
def talk(chatbot, entry):
try:
response = chatbot.get_response(str(entry))
print response
with open("Jason_logs.txt", "a") as f:
f.write(entry)
f.write(response)
except TypeError:
print "Error! Could not convert speech to string!"
except AttributeError:
print "Error! Speech not accepted by Jason's Chatterbot libraries."
我不能完全肯定這是爲什麼,因爲他們都工作,如果他們是在同一個文件(第一大塊代碼)。但是,出於組織目的,如果可能的話,我寧願保持兩個文件分開。
感謝您的任何想法和解決方案您可能能夠給!
**編輯:**這裏是追蹤誤差(如果我在聊天註釋掉屬性錯誤,產生):
Traceback (most recent call last):
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 24, in <module>
startup()
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 13, in startup
voice()
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 19, in voice
call Jason
Listen.start()
File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 47, in start
Chat.talk(bot, speech)
File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Chat.py", line 39, in talk
response = chatbot.get_response(str(entry))
AttributeError: 'NoneType' object has no attribute 'get_response'
你應該包括你收到的實際錯誤(追溯具有非常重要的信息)。你也沒有比較蘋果和橘子,因爲你的代碼被分成兩個文件似乎沒有做到與單個文件版本完全相同的東西(這意味着問題可能是你引入的其他變化,而與被存儲的內容無關在兩個文件中)。 –
可以請你發佈錯誤的完整回溯。 –
發現回溯錯誤。感謝您的快速回復! – TobyTobyo