[英]Python chatbot not working properly
我正在開發一種有兩種情況的聊天機器人。
1:當用戶鍵入問題時,如果該問題在培訓數據集中可用,它將從培訓數據集中為該問題選擇答案。
2:如果用戶鍵入的問題在數據集中不可用,則系統會從代碼中定義的默認答案中給出響應。
我的問題是,當系統收到不在訓練數據中的問題陳述時,它會從代碼中選擇一個隨機答案(精細)。 但是從那時起,無論我們問哪個問題,它都會開始提供默認答案。 盡管培訓數據中有特定問題及其答案,但它從未從培訓數據中選擇答案。
整個代碼太大,無法粘貼到此處。 我只寫發生問題的那些函數。 希望有人可以幫助我。
def response(sentence, userID='123', show_details=False):
results = classify(sentence)
# if we have a classification then find the matching intent tag
if results:
# loop as long as there are matches to process
while results:
#some if else statements to match the question
results.pop(0)
while not results:
pairs = (
(r'I need (.*)',
("Why do you need %1?",
"Would it really help you to get %1?",
"Are you sure you need %1?")),
(r'Why don\'t you (.*)',
("Do you really think I don't %1?",
"Perhaps eventually I will %1.",
"Do you really want me to %1?"))
)
aida_chatbot = Chat(pairs, reflections)
def aida_chat():
aida_chatbot.converse()
def demo():
aida_chat()
if __name__ == "__main__":
demo()
else:
response()
# sentence = sys.stdin.readline()
sys.stdout.write("> ")
sys.stdout.flush()
classify(sentence=sys.stdin.readline())
while True:
response(sentence=sys.stdin.readline())
當我將這些對放在while語句之外時(例如,在if語句關閉后的else語句中),程序永遠不會輸入else語句,並且這部分代碼永遠不會執行。 有人可以幫我嗎?
if __name__ == "__main__":
demo()
else:
response()
您沒有從此響應函數調用中傳遞問題文本。 只需通過此響應函數調用將您的問題作為參數傳遞即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.