forked from ARTISTART367/Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (71 loc) · 3.2 KB
/
main.py
File metadata and controls
90 lines (71 loc) · 3.2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import re
import long_responses as long
import tkinter as tk
def message_probability(user_message, recognised_words, single_response=False, required_words=[]):
message_certainty = 0
has_required_words = True
# Counts how many words are present in each predefined message
for word in user_message:
if word in recognised_words:
message_certainty += 1
# Calculates the percent of recognised words in a user message
percentage = float(message_certainty) / float(len(recognised_words))
# Checks that the required words are in the string
for word in required_words:
if word not in user_message:
has_required_words = False
break
# Must either have the required words, or be a single response
if has_required_words or single_response:
return int(percentage * 100)
else:
return 0
def check_all_messages(message):
highest_prob_list = {}
# Simplifies response creation / adds it to the dict
def response(bot_response, list_of_words, single_response=False, required_words=[]):
nonlocal highest_prob_list
highest_prob_list[bot_response] = message_probability(message, list_of_words, single_response, required_words)
# Responses -------------------------------------------------------------------------------------------------------
response('Hello!', ['hello', 'hi', 'hey', 'sup', 'heyo'], single_response=True)
response('See you!', ['bye', 'goodbye'], single_response=True)
response('I\'m doing fine, and you?', ['how', 'are', 'you', 'doing'], required_words=['how'])
response('You\'re welcome!', ['thank', 'thanks'], single_response=True)
response('Thank you!', ['i', 'love', 'code', 'palace'], required_words=['code', 'palace'])
# Longer responses
response(long.R_ADVICE, ['give', 'advice'], required_words=['advice'])
response(long.R_EATING, ['what', 'you', 'eat'], required_words=['you', 'eat'])
best_match = max(highest_prob_list, key=highest_prob_list.get)
return long.unknown() if highest_prob_list[best_match] < 1 else best_match
# Used to get the response
def get_response(user_input):
split_message = re.split(r'\s+|[,;?!.-]\s*', user_input.lower())
response = check_all_messages(split_message)
return response
def send_message(event=None):
user_input = entry.get()
if user_input.strip().lower() == 'quit':
root.quit()
else:
response = get_response(user_input)
chat_box.config(state=tk.NORMAL)
chat_box.insert(tk.END, f"You: {user_input}\n")
chat_box.insert(tk.END, f"Bot: {response}\n\n")
chat_box.config(state=tk.DISABLED)
entry.delete(0, tk.END)
# Create main window
root = tk.Tk()
root.title("Chatbot")
# Chat history display
chat_box = tk.Text(root, height=20, width=50)
chat_box.config(state=tk.DISABLED)
chat_box.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
# User input field
entry = tk.Entry(root, width=40)
entry.grid(row=1, column=0, padx=10, pady=10)
# Send button
send_button = tk.Button(root, text="Send", command=send_message)
send_button.grid(row=1, column=1, padx=10, pady=10)
# Bind <Return> key to send_message function
root.bind('<Return>', send_message)
root.mainloop()