-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (35 loc) · 1.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
from difflib import get_close_matches
def load_knowledge_base(file_path: str) -> dict:
with open(file_path, 'r') as file:
data: dict = json.load(file)
return data
def save_knowledge_base(file_path: str, data: dict):
with open(file_path, 'w') as file:
json.dump(data, file, indent=2)
def find_best_match(user_question: str, questions: list[str]) -> str | None:
matches: list = get_close_matches(user_question, questions, n=1, cutoff=0.6)
return matches[0] if matches else None
def get_answer_for_question(question: str, knowledge_base: dict) -> str | None:
for q in knowledge_base["questions"]:
if q["question"] == question:
return q["answer"]
def chat_bot():
knowledge_base: dict = load_knowledge_base('knowledge_base.json')
while True:
user_input: str = input('You: ')
if user_input.lower() == 'quit':
break
best_match: str | None = find_best_match(user_input, [q["question"] for q in knowledge_base["questions"]])
if best_match:
answer: str = get_answer_for_question(best_match, knowledge_base)
print(f'Bot: {answer}')
else:
print('Bot: I don\'t know the answer. Can you teach me?')
new_answer: str = input('Type the answer or "skip" to skip: ')
if new_answer.lower() != 'skip':
knowledge_base["questions"].append({"question": user_input, "answer": new_answer})
save_knowledge_base('knowledge_base.json', knowledge_base)
print('Bot: Thank you! I learned a new response!')
if __name__ == '__main__':
chat_bot()