chat bot mini project (1)
chat bot mini project (1)
Aim:
To create a chatbot.
Procedure:
Features:
1. Self learn: As each and every question can be learned , we can teach the bot a lot of things
to remember.
2. Memory: Even after the session has ended , the memory is not wiped as it is stored inside
the JSON file.
Code:
import json
return data
def save_knowledge_base(file_path: str, data: dict):
"""Retrieves the answer for a specific question from the knowledge base."""
for q in knowledge_base["questions"]:
if q["question"] == question:
return q["answer"]
return None
def chat_bot():
knowledge_base = load_knowledge_base('knowledge_base.json')
while True:
if user_input.lower() == 'quit':
break
if best_match:
if answer:
print(answer)
else:
if new_answer.lower() != 'leave':
save_knowledge_base('knowledge_base.json', knowledge_base)
chat_bot()
Ouput:
Result: