forked from MJTech46/PY-rule-based-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (54 loc) · 2.24 KB
/
main.py
File metadata and controls
67 lines (54 loc) · 2.24 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
from difflib import get_close_matches
#loading the json file named knowledge_base.json
def load_knowledge_base(file_path):
with open(file_path,'r') as file:
data = json.load(file)
return data
#saving new data to json file named knowledge_base.json
def save_knowledge_base(file_path,data):
with open(file_path,'w') as file:
json.dump(data, file, indent=2)
#here we use a mach for the question in the knowledge_base.json
#when a mach occurs the "question" will be returned
def find_best_mach(user_question,question):
matches = get_close_matches(
user_question,
question,
n=1, #for repetition
cutoff=0.5 #for the % of mach(0.7 = 70%)
)
return matches[0] if matches else None
# checks for the question in the given DB/file and return the answer
def get_answer_for_question(question, knowledge_base):
for q in knowledge_base["questions"]:
if q["question"] == question:
return q["answer"]
# This is for added a new response; means adding data into the knowledge_base.json file
def add_new_response(user_input, knowledge_base):
print(f"Bot: I don't know the answer. So can you teach me the answer?")
new_answer= input('Type your answer or "__skip" to skip:')
if new_answer and 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!")
#this is the main function
def chat():
knowledge_base = load_knowledge_base('knowledge_base.json')
user_input = input("\nYou: ")
if not user_input:
print("Bot: Please provide a query!\n")
return
elif user_input.lower() == '__quit':
print("Bye")
exit()
best_mach = find_best_mach(user_input, [q["question"] for q in knowledge_base["questions"]])
if best_mach:
answer = get_answer_for_question(best_mach, knowledge_base)
print("Bot:", answer)
else:
add_new_response(user_input, knowledge_base)
if __name__ == '__main__':
print("Bot: Hello, I'm a basic rule-based chatbot. How can I help you?\n'__quit' for exit\n")
while True:
chat()